From 0db1eb97677977922636e937c0e5c14d1a83df7c Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Mon, 4 Jun 2012 15:38:55 +0200 Subject: [PATCH] Allow passing desired browser size as an option --- README.md | 5 +++++ lib/capybara/poltergeist/client.rb | 8 ++++++-- lib/capybara/poltergeist/client/browser.coffee | 6 ++++-- lib/capybara/poltergeist/client/compiled/browser.js | 6 ++++-- lib/capybara/poltergeist/client/compiled/main.js | 6 +++--- lib/capybara/poltergeist/client/compiled/web_page.js | 6 +++--- lib/capybara/poltergeist/client/main.coffee | 6 +++--- lib/capybara/poltergeist/client/web_page.coffee | 4 ++-- lib/capybara/poltergeist/driver.rb | 5 ++++- spec/integration/driver_spec.rb | 6 ++++++ spec/unit/client_spec.rb | 9 +++++++++ spec/unit/driver_spec.rb | 12 ++++++++++++ 12 files changed, 61 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 43170b0..3aaa07d 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,8 @@ end is 30. * `:inspector` (Boolean, String) - See 'Remote Debugging', above. * `:js_errors` (Boolean) - When false, Javascript errors do not get re-raised in Ruby. +* `:window_size` (Array) - The dimensions of the browser window in which to test, expressed + as a 2-element array, e.g. [1024, 768]. Default: [1024, 768] ## Bugs ## @@ -188,6 +190,9 @@ makes debugging easier). Running `rake autocompile` will watch the * Added an option ":js_errors", allowing poltergeist to continue running after JS errors. (John Griffin & Tom Stuart) [Issue #62] [Issue #69] +* Added an option ":window_size", allowing users to specify + dimensions to which the browser window will be resized. + (Tom Stuart) [Issue #53] #### Bug fixes ### diff --git a/lib/capybara/poltergeist/client.rb b/lib/capybara/poltergeist/client.rb index a5734e0..04e00b5 100644 --- a/lib/capybara/poltergeist/client.rb +++ b/lib/capybara/poltergeist/client.rb @@ -10,12 +10,14 @@ module Capybara::Poltergeist client end - attr_reader :pid, :port, :path, :inspector + attr_reader :pid, :port, :path, :inspector, :width, :height - def initialize(port, inspector = nil, path = nil) + def initialize(port, inspector = nil, path = nil, width = 1024, height = 768) @port = port @inspector = inspector @path = path || PHANTOMJS_NAME + @width = width + @height = height pid = Process.pid at_exit { stop if Process.pid == pid } @@ -55,6 +57,8 @@ module Capybara::Poltergeist parts << PHANTOMJS_SCRIPT parts << port + parts << width + parts << height parts end end diff --git a/lib/capybara/poltergeist/client/browser.coffee b/lib/capybara/poltergeist/client/browser.coffee index b84c9cf..5e611c1 100644 --- a/lib/capybara/poltergeist/client/browser.coffee +++ b/lib/capybara/poltergeist/client/browser.coffee @@ -1,5 +1,7 @@ class Poltergeist.Browser - constructor: (@owner) -> + constructor: (@owner, width, height) -> + @width = width || 1024 + @height = height || 768 @state = 'default' @page_id = 0 @@ -7,7 +9,7 @@ class Poltergeist.Browser resetPage: -> @page.release() if @page? - @page = new Poltergeist.WebPage + @page = new Poltergeist.WebPage(@width, @height) @page.onLoadStarted = => @state = 'loading' if @state == 'clicked' diff --git a/lib/capybara/poltergeist/client/compiled/browser.js b/lib/capybara/poltergeist/client/compiled/browser.js index f11eedc..29060dd 100644 --- a/lib/capybara/poltergeist/client/compiled/browser.js +++ b/lib/capybara/poltergeist/client/compiled/browser.js @@ -2,8 +2,10 @@ var __slice = [].slice; Poltergeist.Browser = (function() { - function Browser(owner) { + function Browser(owner, width, height) { this.owner = owner; + this.width = width || 1024; + this.height = height || 768; this.state = 'default'; this.page_id = 0; this.resetPage(); @@ -14,7 +16,7 @@ Poltergeist.Browser = (function() { if (this.page != null) { this.page.release(); } - this.page = new Poltergeist.WebPage; + this.page = new Poltergeist.WebPage(this.width, this.height); this.page.onLoadStarted = function() { if (_this.state === 'clicked') { return _this.state = 'loading'; diff --git a/lib/capybara/poltergeist/client/compiled/main.js b/lib/capybara/poltergeist/client/compiled/main.js index 52c7412..56ddac5 100644 --- a/lib/capybara/poltergeist/client/compiled/main.js +++ b/lib/capybara/poltergeist/client/compiled/main.js @@ -5,11 +5,11 @@ var Poltergeist, Poltergeist = (function() { - function Poltergeist(port) { + function Poltergeist(port, width, height) { this.onError = __bind(this.onError, this); var that; - this.browser = new Poltergeist.Browser(this); + this.browser = new Poltergeist.Browser(this, width, height); this.connection = new Poltergeist.Connection(this, port); that = this; phantom.onError = function(message, stack) { @@ -153,4 +153,4 @@ phantom.injectJs("" + phantom.libraryPath + "/connection.js"); phantom.injectJs("" + phantom.libraryPath + "/browser.js"); -new Poltergeist(phantom.args[0]); +new Poltergeist(phantom.args[0], phantom.args[1], phantom.args[2]); diff --git a/lib/capybara/poltergeist/client/compiled/web_page.js b/lib/capybara/poltergeist/client/compiled/web_page.js index 4e11a1f..8369a80 100644 --- a/lib/capybara/poltergeist/client/compiled/web_page.js +++ b/lib/capybara/poltergeist/client/compiled/web_page.js @@ -10,14 +10,14 @@ Poltergeist.WebPage = (function() { WebPage.COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize']; - function WebPage() { + function WebPage(width, height) { var callback, _i, _len, _ref; this["native"] = require('webpage').create(); this._source = ""; this._errors = []; this.setViewportSize({ - width: 1024, - height: 768 + width: width, + height: height }); _ref = WebPage.CALLBACKS; for (_i = 0, _len = _ref.length; _i < _len; _i++) { diff --git a/lib/capybara/poltergeist/client/main.coffee b/lib/capybara/poltergeist/client/main.coffee index 25bf9c7..13a87bc 100644 --- a/lib/capybara/poltergeist/client/main.coffee +++ b/lib/capybara/poltergeist/client/main.coffee @@ -1,6 +1,6 @@ class Poltergeist - constructor: (port) -> - @browser = new Poltergeist.Browser(this) + constructor: (port, width, height) -> + @browser = new Poltergeist.Browser(this, width, height) @connection = new Poltergeist.Connection(this, port) # The QtWebKit bridge doesn't seem to like Function.prototype.bind @@ -84,4 +84,4 @@ phantom.injectJs("#{phantom.libraryPath}/node.js") phantom.injectJs("#{phantom.libraryPath}/connection.js") phantom.injectJs("#{phantom.libraryPath}/browser.js") -new Poltergeist(phantom.args[0]) +new Poltergeist(phantom.args[0], phantom.args[1], phantom.args[2]) diff --git a/lib/capybara/poltergeist/client/web_page.coffee b/lib/capybara/poltergeist/client/web_page.coffee index b07f605..8bd16e8 100644 --- a/lib/capybara/poltergeist/client/web_page.coffee +++ b/lib/capybara/poltergeist/client/web_page.coffee @@ -7,12 +7,12 @@ class Poltergeist.WebPage @COMMANDS = ['currentUrl', 'find', 'nodeCall', 'pushFrame', 'popFrame', 'documentSize'] - constructor: -> + constructor: (width, height) -> @native = require('webpage').create() @_source = "" @_errors = [] - this.setViewportSize(width: 1024, height: 768) + this.setViewportSize(width: width, height: height) for callback in WebPage.CALLBACKS this.bindCallback(callback) diff --git a/lib/capybara/poltergeist/driver.rb b/lib/capybara/poltergeist/driver.rb index bf62f71..ca91034 100644 --- a/lib/capybara/poltergeist/driver.rb +++ b/lib/capybara/poltergeist/driver.rb @@ -11,6 +11,9 @@ module Capybara::Poltergeist @inspector = nil @server = nil @client = nil + if @options[:window_size] + @width, @height = @options[:window_size] + end @app_server = Capybara::Server.new(app) @app_server.boot if Capybara.run_server @@ -29,7 +32,7 @@ module Capybara::Poltergeist end def client - @client ||= Client.start(server.port, inspector, options[:phantomjs]) + @client ||= Client.start(server.port, inspector, options[:phantomjs], @width, @height) end def client_pid diff --git a/spec/integration/driver_spec.rb b/spec/integration/driver_spec.rb index 92f3e33..e6c926b 100644 --- a/spec/integration/driver_spec.rb +++ b/spec/integration/driver_spec.rb @@ -56,6 +56,12 @@ module Capybara::Poltergeist end end + it 'supports specifying viewport size with an option' do + @driver = Capybara::Poltergeist::Driver.new(nil, :window_size => [800, 600]) + @driver.visit("/") + @driver.evaluate_script('[window.innerWidth, window.innerHeight]').should eq([800, 600]) + end + it 'supports rendering the page' do file = POLTERGEIST_ROOT + '/spec/tmp/screenshot.png' FileUtils.rm_f file diff --git a/spec/unit/client_spec.rb b/spec/unit/client_spec.rb index f1e35c7..f252a1a 100644 --- a/spec/unit/client_spec.rb +++ b/spec/unit/client_spec.rb @@ -30,5 +30,14 @@ module Capybara::Poltergeist e.message.should include('42') end end + + context "with width and height specified" do + subject { Client.new(6000, nil, nil, 800, 600) } + + it "starts phantomjs, passing the width and height through" do + Spawn.should_receive(:spawn).with("phantomjs", Client::PHANTOMJS_SCRIPT, 6000, 800, 600) + subject.start + end + end end end diff --git a/spec/unit/driver_spec.rb b/spec/unit/driver_spec.rb index 95d1cd7..64eff8a 100644 --- a/spec/unit/driver_spec.rb +++ b/spec/unit/driver_spec.rb @@ -49,5 +49,17 @@ module Capybara::Poltergeist subject.server.should == server end end + + context 'with a :window_size option' do + subject { Driver.new(nil, :window_size => [800, 600]) } + + it "creates a client with the desired width and height settings" do + server = stub + server.stub(:port).and_return(64297) + Server.should_receive(:new).and_return(server) + Client.should_receive(:start).with(64297, nil, nil, 800, 600) + subject.client + end + end end end