1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Fold driver specs into session specs

This commit is contained in:
Jonas Nicklas 2012-07-13 12:59:57 +02:00
parent 2e4eb7d17a
commit 3d1fe5e648
12 changed files with 290 additions and 410 deletions

View file

@ -1,317 +0,0 @@
require 'capybara/spec/test_app'
shared_examples_for 'driver' do
describe '#visit' do
it "should move to another page" do
@driver.visit('/')
@driver.body.should include('Hello world!')
@driver.visit('/foo')
@driver.body.should include('Another World')
end
it "should show the correct URL" do
@driver.visit('/foo')
@driver.current_url.should include('/foo')
end
end
describe '#body' do
it "should return text reponses" do
@driver.visit('/')
@driver.body.should include('Hello world!')
end
it "should return the full response html" do
@driver.visit('/with_simple_html')
@driver.body.should include('Bar')
end
if "".respond_to?(:encoding)
context "encoding of response between ascii and utf8" do
it "should be valid with html entities" do
@driver.visit('/with_html_entities')
lambda { @driver.body.encode!("UTF-8") }.should_not raise_error
end
it "should be valid without html entities" do
@driver.visit('/with_html')
lambda { @driver.body.encode!("UTF-8") }.should_not raise_error
end
end
end
end
describe '#find' do
context "with xpath selector" do
before do
@driver.visit('/with_html')
end
it "should extract node texts" do
@driver.find('//a')[0].text.should == 'labore'
@driver.find('//a')[1].text.should == 'ullamco'
end
it "should extract node attributes" do
@driver.find('//a')[0][:class].should == 'simple'
@driver.find('//a')[1][:id].should == 'foo'
@driver.find('//input')[0][:type].should == 'text'
end
it "should extract boolean node attributes" do
@driver.find('//input[@id="checked_field"]')[0][:checked].should be_true
end
it "should allow retrieval of the value" do
@driver.find('//textarea[@id="normal"]').first.value.should == 'banana'
end
it "should not swallow extra newlines in textarea" do
@driver.find('//textarea[@id="additional_newline"]').first.value.should == "\nbanana"
end
it "should allow assignment of field value" do
@driver.find('//input').first.value.should == 'monkey'
@driver.find('//input').first.set('gorilla')
@driver.find('//input').first.value.should == 'gorilla'
end
it "should extract node tag name" do
@driver.find('//a')[0].tag_name.should == 'a'
@driver.find('//a')[1].tag_name.should == 'a'
@driver.find('//p')[1].tag_name.should == 'p'
end
it "should extract node visibility" do
@driver.find('//a')[0].should be_visible
@driver.find('//div[@id="hidden"]')[0].should_not be_visible
@driver.find('//div[@id="hidden_via_ancestor"]')[0].should_not be_visible
end
it "should extract node checked state" do
@driver.visit('/form')
@driver.find('//input[@id="gender_female"]')[0].should be_checked
@driver.find('//input[@id="gender_male"]')[0].should_not be_checked
@driver.find('//h1')[0].should_not be_checked
end
it "should extract node selected state" do
@driver.visit('/form')
@driver.find('//option[@value="en"]')[0].should be_selected
@driver.find('//option[@value="sv"]')[0].should_not be_selected
@driver.find('//h1')[0].should_not be_selected
end
it "should return document text on /html selector" do
@driver.visit('/with_simple_html')
@driver.find('/html')[0].text.should == 'Bar'
end
end
end
end
shared_examples_for "driver with javascript support" do
before { @driver.visit('/with_js') }
describe '#find' do
it "should find dynamically changed nodes" do
@driver.find('//p').first.text.should == 'I changed it'
end
end
describe '#drag_to' do
it "should drag and drop an object" do
draggable = @driver.find('//div[@id="drag"]').first
droppable = @driver.find('//div[@id="drop"]').first
draggable.drag_to(droppable)
@driver.find('//div[contains(., "Dropped!")]').should_not be_empty
end
end
describe "#evaluate_script" do
it "should return the value of the executed script" do
@driver.evaluate_script('1+1').should == 2
end
end
describe '#set' do
describe 'on a pre-populated textfield with a reformatting onchange' do
it 'should only trigger onchange once' do
field = @driver.find('//input[@id="with_change_event"]').first
field.set('some value')
field.value.should == 'some value'
end
end
end
end
shared_examples_for "driver with header support" do
it "should make headers available through response_headers" do
@driver.visit('/with_simple_html')
@driver.response_headers['Content-Type'].should =~ /text\/html/
end
end
shared_examples_for "driver with status code support" do
it "should make the status code available through status_code" do
@driver.visit('/with_simple_html')
@driver.status_code.should == 200
end
end
shared_examples_for "driver without status code support" do
it "should raise when trying to access the status code available through status_code" do
@driver.visit('/with_simple_html')
lambda {
@driver.status_code
}.should raise_error(Capybara::NotSupportedByDriverError)
end
end
shared_examples_for "driver with frame support" do
describe '#within_frame' do
before(:each) do
@driver.visit('/within_frames')
end
it "should find the div in frameOne" do
@driver.within_frame("frameOne") do
@driver.find("//*[@id='divInFrameOne']")[0].text.should eql 'This is the text of divInFrameOne'
end
end
it "should find the div in FrameTwo" do
@driver.within_frame("frameTwo") do
@driver.find("//*[@id='divInFrameTwo']")[0].text.should eql 'This is the text of divInFrameTwo'
end
end
it "should find the text div in the main window after finding text in frameOne" do
@driver.within_frame("frameOne") do
@driver.find("//*[@id='divInFrameOne']")[0].text.should eql 'This is the text of divInFrameOne'
end
@driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow'
end
it "should find the text div in the main window after finding text in frameTwo" do
@driver.within_frame("frameTwo") do
@driver.find("//*[@id='divInFrameTwo']")[0].text.should eql 'This is the text of divInFrameTwo'
end
@driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow'
end
it "should return the result of executing the block" do
@driver.within_frame("frameOne") { "return value" }.should eql "return value"
end
end
end
shared_examples_for "driver with support for window switching" do
describe '#within_window' do
before(:each) do
@driver.visit('/within_popups')
end
after(:each) do
@driver.within_window("firstPopup") do
@driver.evaluate_script('window.close()')
end
@driver.within_window("secondPopup") do
@driver.evaluate_script('window.close()')
end
end
it "should find the div in firstPopup" do
@driver.within_window("firstPopup") do
@driver.find("//*[@id='divInPopupOne']")[0].text.should eql 'This is the text of divInPopupOne'
end
end
it "should find the div in secondPopup" do
@driver.within_window("secondPopup") do
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
end
end
it "should find the divs in both popups" do
@driver.within_window("secondPopup") do
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
end
@driver.within_window("firstPopup") do
@driver.find("//*[@id='divInPopupOne']")[0].text.should eql 'This is the text of divInPopupOne'
end
end
it "should find the div in the main window after finding a div in a popup" do
@driver.within_window("secondPopup") do
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
end
@driver.find("//*[@id='divInMainWindow']")[0].text.should eql 'This is the text for divInMainWindow'
end
end
end
shared_examples_for "driver with cookies support" do
describe "#reset!" do
it "should set and clean cookies" do
@driver.visit('/get_cookie')
@driver.body.should_not include('test_cookie')
@driver.visit('/set_cookie')
@driver.body.should include('Cookie set to test_cookie')
@driver.visit('/get_cookie')
@driver.body.should include('test_cookie')
@driver.reset!
@driver.visit('/get_cookie')
@driver.body.should_not include('test_cookie')
end
end
end
shared_examples_for "driver with referer support" do
before :each do
@driver.reset!
end
it "should send no referer when visiting a page" do
@driver.visit '/get_referer'
@driver.body.should include 'No referer'
end
it "should send no referer when visiting a second page" do
@driver.visit '/get_referer'
@driver.visit '/get_referer'
@driver.body.should include 'No referer'
end
it "should send a referer when following a link" do
@driver.visit '/referer_base'
@driver.find('//a[@href="/get_referer"]').first.click
@driver.body.should match %r{http://.*/referer_base}
end
it "should preserve the original referer URL when following a redirect" do
@driver.visit('/referer_base')
@driver.find('//a[@href="/redirect_to_get_referer"]').first.click
@driver.body.should match %r{http://.*/referer_base}
end
it "should send a referer when submitting a form" do
@driver.visit '/referer_base'
@driver.find('//input').first.click
@driver.body.should match %r{http://.*/referer_base}
end
end
shared_examples_for "driver with screenshot support" do
describe '#save_screenshot' do
let(:image_path) { File.join(Dir.tmpdir, 'capybara-screenshot.png') }
before do
@driver.visit '/'
@driver.save_screenshot image_path
end
it "should generate PNG file" do
magic = File.read(image_path, 4)
magic.should eq "\x89PNG"
end
end
end

View file

@ -12,58 +12,26 @@ shared_examples_for "session" do
@session.reset_session!
end
describe '#visit' do
it "should fetch a response from the driver with a relative url" do
@session.visit('/')
@session.body.should include('Hello world!')
@session.visit('/foo')
@session.body.should include('Another World')
end
it "should fetch a response from the driver with an absolute url with a port" do
# Preparation
@session.visit('/')
root_uri = URI.parse(@session.current_url)
@session.visit("http://#{root_uri.host}:#{root_uri.port}/")
@session.body.should include('Hello world!')
@session.visit("http://#{root_uri.host}:#{root_uri.port}/foo")
@session.body.should include('Another World')
end
context "when Capybara.always_include_port is true" do
let(:root_uri) do
@session.visit('/')
URI.parse(@session.current_url)
end
before(:each) do
Capybara.always_include_port = true
end
after(:each) do
Capybara.always_include_port = false
end
it "should fetch a response from the driver with an absolute url without a port" do
@session.visit("http://#{root_uri.host}/")
URI.parse(@session.current_url).port.should == root_uri.port
@session.body.should include('Hello world!')
@session.visit("http://#{root_uri.host}/foo")
URI.parse(@session.current_url).port.should == root_uri.port
@session.body.should include('Another World')
end
end
end
describe '#body' do
it "should return the unmodified page body" do
@session.visit('/')
@session.body.should include('Hello world!')
end
end
if "".respond_to?(:encoding)
context "encoding of response between ascii and utf8" do
it "should be valid with html entities" do
@session.visit('/with_html_entities')
lambda { @session.body.encode!("UTF-8") }.should_not raise_error
end
it "should be valid without html entities" do
@session.visit('/with_html')
lambda { @session.body.encode!("UTF-8") }.should_not raise_error
end
end
end
end
describe '#html' do
it "should return the unmodified page body" do
@ -116,6 +84,8 @@ shared_examples_for "session" do
end
end
it_should_behave_like "node"
it_should_behave_like "visit"
it_should_behave_like "all"
it_should_behave_like "first"
it_should_behave_like "attach_file"
@ -170,10 +140,8 @@ shared_examples_for "session" do
addresses[1]["city"].should == 'Mikolaiv'
addresses[1]["country"].should == 'Ukraine'
end
end
describe Capybara::Session do
context 'with non-existant driver' do
it "should raise an error" do

View file

@ -13,12 +13,6 @@ shared_examples_for "find" do
@session.find("//input[@id='test_field']")[:value].should == 'monkey'
end
it "preserve object identity", :focus => true do
(@session.find('//h1') == @session.find('//h1')).should be_true
(@session.find('//h1') === @session.find('//h1')).should be_true
(@session.find('//h1').eql? @session.find('//h1')).should be_false
end
it "should find the first element using the given locator and options" do
@session.find('//a', :text => 'Redirect')[:id].should == 'red'
@session.find(:css, 'a', :text => 'A link came first')[:title].should == 'twas a fine link'
@ -28,28 +22,6 @@ shared_examples_for "find" do
expect { @session.find('//a') }.to raise_error(Capybara::Ambiguous)
end
describe 'the returned node' do
it "should act like a session object" do
@session.visit('/form')
@form = @session.find(:css, '#get-form')
@form.should have_field('Middle Name')
@form.should have_no_field('Languages')
@form.fill_in('Middle Name', :with => 'Monkey')
@form.click_button('med')
extract_results(@session)['middle_name'].should == 'Monkey'
end
it "should scope CSS selectors" do
@session.find(:css, '#second').should have_no_css('h1')
end
it "should have a reference to its parent if there is one" do
@node = @session.find(:css, '#first')
@node.parent.should == @node.session.document
@node.find(:css, '#foo').parent.should == @node
end
end
context "with css selectors" do
it "should find the first element using the given locator" do
@session.find(:css, 'h1').text.should == 'This is a test'

View file

@ -181,6 +181,13 @@ shared_examples_for "session with javascript support" do
@session.click_link('Click me')
@session.fill_in('new_field', :with => 'Testing...')
end
context 'on a pre-populated textfield with a reformatting onchange' do
it 'should only trigger onchange once' do
@session.fill_in('with_change_event', :with => 'some value')
@session.find(:css, '#with_change_event').value.should == 'some value'
end
end
end
describe '#check' do

View file

@ -0,0 +1,115 @@
shared_examples_for "node" do
describe "node" do
before do
@session.visit('/with_html')
end
it "should act like a session object" do
@session.visit('/form')
@form = @session.find(:css, '#get-form')
@form.should have_field('Middle Name')
@form.should have_no_field('Languages')
@form.fill_in('Middle Name', :with => 'Monkey')
@form.click_button('med')
extract_results(@session)['middle_name'].should == 'Monkey'
end
it "should scope CSS selectors" do
@session.find(:css, '#second').should have_no_css('h1')
end
describe "#parent" do
it "should have a reference to its parent if there is one" do
@node = @session.find(:css, '#first')
@node.parent.should == @node.session.document
@node.find(:css, '#foo').parent.should == @node
end
end
describe "#text" do
it "should extract node texts" do
@session.all('//a')[0].text.should == 'labore'
@session.all('//a')[1].text.should == 'ullamco'
end
it "should return document text on /html selector" do
@session.visit('/with_simple_html')
@session.all('/html')[0].text.should == 'Bar'
end
end
describe "#[]" do
it "should extract node attributes" do
@session.all('//a')[0][:class].should == 'simple'
@session.all('//a')[1][:id].should == 'foo'
@session.all('//input')[0][:type].should == 'text'
end
it "should extract boolean node attributes" do
@session.find('//input[@id="checked_field"]')[:checked].should be_true
end
end
describe "#value" do
it "should allow retrieval of the value" do
@session.find('//textarea[@id="normal"]').value.should == 'banana'
end
it "should not swallow extra newlines in textarea" do
@session.find('//textarea[@id="additional_newline"]').value.should == "\nbanana"
end
end
describe "#set" do
it "should allow assignment of field value" do
@session.first('//input').value.should == 'monkey'
@session.first('//input').set('gorilla')
@session.first('//input').value.should == 'gorilla'
end
end
describe "#tag_name" do
it "should extract node tag name" do
@session.all('//a')[0].tag_name.should == 'a'
@session.all('//a')[1].tag_name.should == 'a'
@session.all('//p')[1].tag_name.should == 'p'
end
end
describe "#visible?" do
it "should extract node visibility" do
@session.first('//a').should be_visible
@session.find('//div[@id="hidden"]').should_not be_visible
@session.find('//div[@id="hidden_via_ancestor"]').should_not be_visible
end
end
describe "#checked?" do
it "should extract node checked state" do
@session.visit('/form')
@session.find('//input[@id="gender_female"]').should be_checked
@session.find('//input[@id="gender_male"]').should_not be_checked
@session.first('//h1').should_not be_checked
end
end
describe "#selected?" do
it "should extract node selected state" do
@session.visit('/form')
@session.find('//option[@value="en"]').should be_selected
@session.find('//option[@value="sv"]').should_not be_selected
@session.first('//h1').should_not be_selected
end
end
describe "#==" do
it "preserve object identity" do
(@session.find('//h1') == @session.find('//h1')).should be_true
(@session.find('//h1') === @session.find('//h1')).should be_true
(@session.find('//h1').eql? @session.find('//h1')).should be_false
end
end
end
end

View file

@ -0,0 +1,76 @@
shared_examples_for "visit" do
describe '#visit' do
it "should fetch a response from the driver with a relative url" do
@session.visit('/')
@session.body.should include('Hello world!')
@session.visit('/foo')
@session.body.should include('Another World')
end
it "should fetch a response from the driver with an absolute url with a port" do
# Preparation
@session.visit('/')
root_uri = URI.parse(@session.current_url)
@session.visit("http://#{root_uri.host}:#{root_uri.port}/")
@session.body.should include('Hello world!')
@session.visit("http://#{root_uri.host}:#{root_uri.port}/foo")
@session.body.should include('Another World')
end
context "when Capybara.always_include_port is true" do
let(:root_uri) do
@session.visit('/')
URI.parse(@session.current_url)
end
before(:each) do
Capybara.always_include_port = true
end
after(:each) do
Capybara.always_include_port = false
end
it "should fetch a response from the driver with an absolute url without a port" do
@session.visit("http://#{root_uri.host}/")
URI.parse(@session.current_url).port.should == root_uri.port
@session.body.should include('Hello world!')
@session.visit("http://#{root_uri.host}/foo")
URI.parse(@session.current_url).port.should == root_uri.port
@session.body.should include('Another World')
end
end
it "should send no referer when visiting a page" do
@session.visit '/get_referer'
@session.body.should include 'No referer'
end
it "should send no referer when visiting a second page" do
@session.visit '/get_referer'
@session.visit '/get_referer'
@session.body.should include 'No referer'
end
it "should send a referer when following a link" do
@session.visit '/referer_base'
@session.find('//a[@href="/get_referer"]').click
@session.body.should match %r{http://.*/referer_base}
end
it "should preserve the original referer URL when following a redirect" do
@session.visit('/referer_base')
@session.find('//a[@href="/redirect_to_get_referer"]').click
@session.body.should match %r{http://.*/referer_base}
end
it "should send a referer when submitting a form" do
@session.visit '/referer_base'
@session.find('//input').click
@session.body.should match %r{http://.*/referer_base}
end
end
end

View file

@ -0,0 +1,33 @@
shared_examples_for "session with frame support" do
describe '#within_frame' do
before(:each) do
@session.visit('/within_frames')
end
it "should find the div in frameOne" do
@session.within_frame("frameOne") do
@session.find("//*[@id='divInFrameOne']").text.should eql 'This is the text of divInFrameOne'
end
end
it "should find the div in FrameTwo" do
@session.within_frame("frameTwo") do
@session.find("//*[@id='divInFrameTwo']").text.should eql 'This is the text of divInFrameTwo'
end
end
it "should find the text div in the main window after finding text in frameOne" do
@session.within_frame("frameOne") do
@session.find("//*[@id='divInFrameOne']").text.should eql 'This is the text of divInFrameOne'
end
@session.find("//*[@id='divInMainWindow']").text.should eql 'This is the text for divInMainWindow'
end
it "should find the text div in the main window after finding text in frameTwo" do
@session.within_frame("frameTwo") do
@session.find("//*[@id='divInFrameTwo']").text.should eql 'This is the text of divInFrameTwo'
end
@session.find("//*[@id='divInMainWindow']").text.should eql 'This is the text for divInMainWindow'
end
it "should return the result of executing the block" do
@session.within_frame("frameOne") { "return value" }.should eql "return value"
end
end
end

View file

@ -0,0 +1,40 @@
shared_examples_for "session with window support" do
describe '#within_window' do
before(:each) do
@session.visit('/within_popups')
end
after(:each) do
@session.within_window("firstPopup") do
@session.evaluate_script('window.close()')
end
@session.within_window("secondPopup") do
@session.evaluate_script('window.close()')
end
end
it "should find the div in firstPopup" do
@session.within_window("firstPopup") do
@session.find("//*[@id='divInPopupOne']").text.should eql 'This is the text of divInPopupOne'
end
end
it "should find the div in secondPopup" do
@session.within_window("secondPopup") do
@session.find("//*[@id='divInPopupTwo']").text.should eql 'This is the text of divInPopupTwo'
end
end
it "should find the divs in both popups" do
@session.within_window("secondPopup") do
@session.find("//*[@id='divInPopupTwo']").text.should eql 'This is the text of divInPopupTwo'
end
@session.within_window("firstPopup") do
@session.find("//*[@id='divInPopupOne']").text.should eql 'This is the text of divInPopupOne'
end
end
it "should find the div in the main window after finding a div in a popup" do
@session.within_window("secondPopup") do
@session.find("//*[@id='divInPopupTwo']").text.should eql 'This is the text of divInPopupTwo'
end
@session.find("//*[@id='divInMainWindow']").text.should eql 'This is the text for divInMainWindow'
end
end
end

View file

@ -25,12 +25,6 @@ describe Capybara::RackTest::Driver do
end.should raise_error(ArgumentError)
end
it_should_behave_like "driver"
it_should_behave_like "driver with header support"
it_should_behave_like "driver with status code support"
it_should_behave_like "driver with cookies support"
it_should_behave_like "driver with referer support"
describe '#reset!' do
it { @driver.visit('/foo'); lambda { @driver.reset! }.should change(@driver, :current_url).to('') }

View file

@ -6,15 +6,6 @@ describe Capybara::Selenium::Driver do
@driver = TestSessions::Selenium.driver
end
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 with support for window switching"
it_should_behave_like "driver without status code support"
it_should_behave_like "driver with cookies support"
it_should_behave_like "driver with referer support"
it_should_behave_like "driver with screenshot support"
unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
it "should not interfere with forking child processes" do
# Launch a browser, which registers the at_exit hook

View file

@ -21,6 +21,8 @@ describe Capybara::Session do
it_should_behave_like "session"
it_should_behave_like "session with javascript support"
it_should_behave_like "session with screenshot support"
it_should_behave_like "session with frame support"
it_should_behave_like "session with window support"
it_should_behave_like "session without headers support"
it_should_behave_like "session without status code support"
end

View file

@ -33,7 +33,6 @@ end
# Required here instead of in rspec_spec to avoid RSpec deprecation warning
require 'capybara/rspec'
require 'capybara/spec/driver'
require 'capybara/spec/session'
alias :running :lambda