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
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
it 'should support 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
|
|
|
|
|
2011-10-30 22:48:56 +00:00
|
|
|
it 'should raise an error and restart the client, if the client dies while executing a command' do
|
|
|
|
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
|
|
|
|
|
|
|
it 'should have a viewport size of 1024x768 by default' do
|
|
|
|
@driver.visit('/')
|
|
|
|
@driver.evaluate_script('[window.innerWidth, window.innerHeight]').should == [1024, 768]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should allow 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
|
|
|
|
|
|
|
|
it 'should support rendering the page' do
|
|
|
|
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
|
|
|
|
|
|
|
|
it 'should support rendering the whole of a page that goes outside the viewport' do
|
|
|
|
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
|
|
|
|
|
|
|
it 'should support executing multiple lines of javascript' do
|
|
|
|
@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
|
|
|
|
|
|
|
it 'should operate a timeout when communicating with phantomjs' do
|
|
|
|
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
|
|
|
|
it 'propagates an 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")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't re-raise a Javascript error if it's rescued" do
|
|
|
|
begin
|
|
|
|
@driver.execute_script("omg")
|
|
|
|
rescue JavascriptError
|
|
|
|
end
|
|
|
|
|
|
|
|
# should not raise again
|
|
|
|
@driver.evaluate_script("1+1").should == 2
|
|
|
|
end
|
|
|
|
end
|
2011-10-27 23:34:14 +01:00
|
|
|
end
|
|
|
|
end
|