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

143 lines
4.1 KiB
Ruby
Raw Normal View History

2011-10-27 23:34:14 +01:00
require 'spec_helper'
2011-10-28 19:11:00 +01:00
require 'capybara/spec/driver'
require 'image_size'
2011-10-27 23:34:14 +01:00
module Capybara::Poltergeist
describe Driver do
before do
@driver = TestSessions::Poltergeist.driver
end
2011-10-27 23:34:14 +01: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
it 'should support a custom phantomjs path' do
file = POLTERGEIST_ROOT + '/spec/support/custom_phantomjs_called'
path = POLTERGEIST_ROOT + '/spec/support/custom_phantomjs'
FileUtils.rm_f file
2011-10-27 23:34:14 +01:00
driver = Capybara::Poltergeist::Driver.new(nil, :phantomjs => path)
driver.browser
2011-10-27 23:34:14 +01:00
# If the correct custom path is called, it will touch the file. We allow at
# least 10 secs for this to happen before failing.
tries = 0
until File.exist?(file) || tries == 100
sleep 0.1
tries += 1
end
File.exist?(file).should == true
end
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
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
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
@driver.visit('/')
@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
end
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
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
lambda { @driver.browser.command 'noop' }.should raise_error(TimeoutError)
ensure
2012-02-29 12:32:13 +00:00
@driver.timeout = prev_timeout
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
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