1
0
Fork 0
mirror of https://github.com/teampoltergeist/poltergeist.git synced 2022-11-09 12:05:00 -05:00

Allow passing desired browser size as an option

This commit is contained in:
Tom Stuart 2012-06-04 15:38:55 +02:00
parent 1ed041ab42
commit 0db1eb9767
12 changed files with 61 additions and 18 deletions

View file

@ -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 ###

View file

@ -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

View file

@ -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'

View file

@ -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';

View file

@ -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]);

View file

@ -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++) {

View file

@ -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])

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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