2011-10-27 23:34:14 +01:00
|
|
|
require 'spec_helper'
|
2011-10-28 19:11:00 +01:00
|
|
|
require 'capybara/spec/driver'
|
2011-10-31 23:04:02 +00:00
|
|
|
require 'image_size'
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
module Capybara::Poltergeist
|
|
|
|
describe Driver do
|
|
|
|
before do
|
|
|
|
@driver = TestSessions::Poltergeist.driver
|
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
it_should_behave_like "driver"
|
|
|
|
it_should_behave_like "driver with javascript support"
|
|
|
|
it_should_behave_like "driver with frame support"
|
|
|
|
it_should_behave_like "driver without status code support"
|
|
|
|
it_should_behave_like "driver with cookies support"
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'supports a custom phantomjs path' do
|
2011-10-31 23:04:02 +00:00
|
|
|
file = POLTERGEIST_ROOT + '/spec/support/custom_phantomjs_called'
|
|
|
|
path = POLTERGEIST_ROOT + '/spec/support/custom_phantomjs'
|
2011-10-28 18:52:06 +01:00
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
FileUtils.rm_f file
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
driver = Capybara::Poltergeist::Driver.new(nil, :phantomjs => path)
|
|
|
|
driver.browser
|
2011-10-27 23:34:14 +01:00
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
# If the correct custom path is called, it will touch the file. We allow at
|
2012-01-13 15:05:54 +00:00
|
|
|
# least 10 secs for this to happen before failing.
|
2011-10-28 18:52:06 +01:00
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
tries = 0
|
2012-01-13 15:05:54 +00:00
|
|
|
until File.exist?(file) || tries == 100
|
2011-10-30 22:48:56 +00:00
|
|
|
sleep 0.1
|
|
|
|
tries += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
File.exist?(file).should == true
|
2011-10-28 18:52:06 +01:00
|
|
|
end
|
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'raises an error and restart the client, if the client dies while executing a command' do
|
2011-10-30 22:48:56 +00:00
|
|
|
lambda { @driver.browser.command('exit') }.should raise_error(DeadClient)
|
|
|
|
@driver.visit('/')
|
|
|
|
@driver.body.should include('Hello world')
|
|
|
|
end
|
2011-10-31 22:23:52 +00:00
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'has a viewport size of 1024x768 by default' do
|
2011-10-31 22:23:52 +00:00
|
|
|
@driver.visit('/')
|
|
|
|
@driver.evaluate_script('[window.innerWidth, window.innerHeight]').should == [1024, 768]
|
|
|
|
end
|
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'allows the viewport to be resized' do
|
2011-10-31 23:04:02 +00:00
|
|
|
begin
|
|
|
|
@driver.visit('/')
|
|
|
|
@driver.resize(200, 400)
|
|
|
|
@driver.evaluate_script('[window.innerWidth, window.innerHeight]').should == [200, 400]
|
|
|
|
ensure
|
|
|
|
@driver.resize(1024, 768)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-04 15:38:55 +02:00
|
|
|
it 'supports specifying viewport size with an option' do
|
2012-06-08 10:26:18 +01:00
|
|
|
Capybara.register_driver :poltergeist_with_custom_window_size do |app|
|
|
|
|
Capybara::Poltergeist::Driver.new(
|
|
|
|
app,
|
|
|
|
:logger => TestSessions.logger,
|
|
|
|
:window_size => [800, 600]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
@driver = Capybara::Session.new(:poltergeist_with_custom_window_size, TestApp).driver
|
2012-06-04 15:38:55 +02:00
|
|
|
@driver.visit("/")
|
|
|
|
@driver.evaluate_script('[window.innerWidth, window.innerHeight]').should eq([800, 600])
|
|
|
|
end
|
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'supports rendering the page' do
|
2011-10-31 23:04:02 +00:00
|
|
|
file = POLTERGEIST_ROOT + '/spec/tmp/screenshot.png'
|
|
|
|
FileUtils.rm_f file
|
2011-10-31 22:23:52 +00:00
|
|
|
@driver.visit('/')
|
2011-10-31 23:04:02 +00:00
|
|
|
@driver.render(file)
|
|
|
|
File.exist?(file).should == true
|
|
|
|
end
|
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'supports rendering the whole of a page that goes outside the viewport' do
|
2011-10-31 23:04:02 +00:00
|
|
|
file = POLTERGEIST_ROOT + '/spec/tmp/screenshot.png'
|
|
|
|
@driver.visit('/poltergeist/long_page')
|
|
|
|
@driver.render(file)
|
|
|
|
|
|
|
|
File.open(file, 'rb') do |f|
|
|
|
|
ImageSize.new(f.read).size.should ==
|
|
|
|
@driver.evaluate_script('[window.innerWidth, window.innerHeight]')
|
|
|
|
end
|
|
|
|
|
|
|
|
@driver.render(file, :full => true)
|
|
|
|
|
|
|
|
File.open(file, 'rb') do |f|
|
|
|
|
ImageSize.new(f.read).size.should ==
|
|
|
|
@driver.evaluate_script('[document.documentElement.clientWidth, document.documentElement.clientHeight]')
|
|
|
|
end
|
2011-10-31 22:23:52 +00:00
|
|
|
end
|
2011-11-03 00:24:06 +00:00
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'supports executing multiple lines of javascript' do
|
2011-11-03 00:24:06 +00:00
|
|
|
@driver.execute_script <<-JS
|
|
|
|
var a = 1
|
|
|
|
var b = 2
|
|
|
|
window.result = a + b
|
|
|
|
JS
|
|
|
|
@driver.evaluate_script("result").should == 3
|
|
|
|
end
|
2012-01-13 13:10:30 +00:00
|
|
|
|
2012-06-04 14:21:09 +02:00
|
|
|
it 'operates a timeout when communicating with phantomjs' do
|
2012-01-13 13:10:30 +00:00
|
|
|
begin
|
2012-02-29 12:32:13 +00:00
|
|
|
prev_timeout = @driver.timeout
|
|
|
|
@driver.timeout = 0.001
|
2012-01-13 13:10:30 +00:00
|
|
|
lambda { @driver.browser.command 'noop' }.should raise_error(TimeoutError)
|
|
|
|
ensure
|
2012-02-29 12:32:13 +00:00
|
|
|
@driver.timeout = prev_timeout
|
2012-01-13 13:10:30 +00:00
|
|
|
end
|
|
|
|
end
|
2012-02-29 13:51:11 +00:00
|
|
|
|
|
|
|
it 'supports quitting the session' do
|
|
|
|
driver = Capybara::Poltergeist::Driver.new(nil)
|
|
|
|
pid = driver.client_pid
|
|
|
|
|
|
|
|
Process.kill(0, pid).should == 1
|
|
|
|
driver.quit
|
|
|
|
|
|
|
|
begin
|
|
|
|
Process.kill(0, pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
else
|
|
|
|
raise "process is still alive"
|
|
|
|
end
|
|
|
|
end
|
2012-02-29 20:38:01 +00:00
|
|
|
|
|
|
|
context 'javascript errors' do
|
2012-04-09 19:30:25 +01:00
|
|
|
it 'propagates a Javascript error inside Poltergeist to a ruby exception' do
|
2012-06-27 18:34:24 +01:00
|
|
|
expect { @driver.browser.command 'browser_error' }.to raise_error(BrowserError)
|
2012-02-29 20:38:01 +00:00
|
|
|
|
|
|
|
begin
|
2012-06-27 18:34:24 +01:00
|
|
|
@driver.browser.command 'browser_error'
|
2012-04-09 19:30:25 +01:00
|
|
|
rescue BrowserError => e
|
2012-06-27 18:34:24 +01:00
|
|
|
e.message.should include("Error: zomg")
|
|
|
|
e.message.should include("compiled/browser.js")
|
2012-04-09 19:30:25 +01:00
|
|
|
else
|
|
|
|
raise "BrowserError expected"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 18:34:24 +01:00
|
|
|
it 'propagates an asynchronous Javascript error on the page to a ruby exception' do
|
2012-04-09 19:30:25 +01:00
|
|
|
@driver.execute_script "setTimeout(function() { omg }, 0)"
|
|
|
|
sleep 0.01
|
|
|
|
expect { @driver.execute_script "" }.to raise_error(JavascriptError)
|
|
|
|
|
|
|
|
begin
|
|
|
|
@driver.execute_script "setTimeout(function() { omg }, 0)"
|
|
|
|
sleep 0.01
|
|
|
|
@driver.execute_script ""
|
2012-02-29 20:38:01 +00:00
|
|
|
rescue JavascriptError => e
|
|
|
|
e.message.should include("omg")
|
|
|
|
e.message.should include("ReferenceError")
|
2012-04-09 19:30:25 +01:00
|
|
|
else
|
|
|
|
raise "expected JavascriptError"
|
2012-02-29 20:38:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 18:34:24 +01:00
|
|
|
it 'propagates a synchronous Javascript error on the page to a ruby exception' do
|
|
|
|
expect { @driver.execute_script "omg" }.to raise_error(JavascriptError)
|
|
|
|
|
|
|
|
begin
|
|
|
|
@driver.execute_script "omg"
|
|
|
|
rescue JavascriptError => e
|
|
|
|
e.message.should include("omg")
|
|
|
|
e.message.should include("ReferenceError")
|
|
|
|
else
|
|
|
|
raise "expected JavascriptError"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-29 20:38:01 +00:00
|
|
|
it "doesn't re-raise a Javascript error if it's rescued" do
|
|
|
|
begin
|
2012-04-09 19:30:25 +01:00
|
|
|
@driver.execute_script "setTimeout(function() { omg }, 0)"
|
|
|
|
sleep 0.01
|
|
|
|
@driver.execute_script ""
|
2012-02-29 20:38:01 +00:00
|
|
|
rescue JavascriptError
|
2012-04-09 19:30:25 +01:00
|
|
|
else
|
|
|
|
raise "expected JavascriptError"
|
2012-02-29 20:38:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# should not raise again
|
|
|
|
@driver.evaluate_script("1+1").should == 2
|
|
|
|
end
|
2012-05-12 18:03:39 +01:00
|
|
|
|
|
|
|
it "doesn't propagate a Javascript error to ruby if error raising disabled" do
|
2012-05-31 13:36:31 +01:00
|
|
|
driver = Capybara::Poltergeist::Driver.new(nil, :js_errors => false)
|
2012-05-12 18:03:39 +01:00
|
|
|
driver.execute_script "setTimeout(function() { omg }, 0)"
|
|
|
|
sleep 0.01
|
|
|
|
expect { driver.execute_script "" }.to_not raise_error(JavascriptError)
|
|
|
|
end
|
2012-02-29 20:38:01 +00:00
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
end
|