2010-04-09 10:41:35 -04:00
|
|
|
require 'capybara/spec/test_app'
|
|
|
|
|
2009-11-04 17:00:05 -05:00
|
|
|
shared_examples_for 'driver' do
|
2009-11-04 18:34:11 -05:00
|
|
|
|
2009-11-04 17:32:35 -05:00
|
|
|
describe '#visit' do
|
|
|
|
it "should move to another page" do
|
2009-11-04 17:05:11 -05:00
|
|
|
@driver.visit('/')
|
2009-11-05 17:02:17 -05:00
|
|
|
@driver.body.should include('Hello world!')
|
2009-11-04 17:05:11 -05:00
|
|
|
@driver.visit('/foo')
|
2009-11-05 17:02:17 -05:00
|
|
|
@driver.body.should include('Another World')
|
2009-11-04 17:00:05 -05:00
|
|
|
end
|
2010-06-24 09:51:44 -04:00
|
|
|
|
2009-12-17 06:22:47 -05:00
|
|
|
it "should show the correct URL" do
|
2009-12-25 13:09:10 -05:00
|
|
|
@driver.visit('/foo')
|
|
|
|
@driver.current_url.should include('/foo')
|
2009-12-17 06:22:47 -05:00
|
|
|
end
|
2009-11-04 17:00:05 -05:00
|
|
|
end
|
2009-11-04 18:34:11 -05:00
|
|
|
|
2009-11-04 17:32:35 -05:00
|
|
|
describe '#body' do
|
|
|
|
it "should return text reponses" do
|
|
|
|
@driver.visit('/')
|
2009-11-05 17:02:17 -05:00
|
|
|
@driver.body.should include('Hello world!')
|
2009-11-04 17:32:35 -05:00
|
|
|
end
|
2009-11-04 18:34:11 -05:00
|
|
|
|
2009-11-04 17:32:35 -05:00
|
|
|
it "should return the full response html" do
|
|
|
|
@driver.visit('/with_simple_html')
|
2010-01-01 17:58:59 -05:00
|
|
|
@driver.body.should include('Bar')
|
2009-11-04 17:32:35 -05:00
|
|
|
end
|
|
|
|
end
|
2009-11-04 18:34:11 -05:00
|
|
|
|
|
|
|
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
|
2009-12-11 16:41:12 -05:00
|
|
|
|
2009-11-04 18:34:11 -05:00
|
|
|
it "should extract node attributes" do
|
2009-11-07 18:28:32 -05:00
|
|
|
@driver.find('//a')[0][:class].should == 'simple'
|
|
|
|
@driver.find('//a')[1][:id].should == 'foo'
|
2010-11-21 13:00:28 -05:00
|
|
|
@driver.find('//input')[0][:type].should == 'text'
|
2009-11-07 18:28:32 -05:00
|
|
|
end
|
|
|
|
|
2010-01-30 14:43:56 -05:00
|
|
|
it "should extract boolean node attributes" do
|
|
|
|
@driver.find('//input[@id="checked_field"]')[0][:checked].should be_true
|
|
|
|
end
|
|
|
|
|
2010-05-14 04:52:37 -04:00
|
|
|
it "should allow retrieval of the value" do
|
|
|
|
@driver.find('//textarea').first.value.should == 'banana'
|
|
|
|
end
|
|
|
|
|
2009-11-07 18:28:32 -05:00
|
|
|
it "should allow assignment of field value" do
|
2009-11-17 17:52:22 -05:00
|
|
|
@driver.find('//input').first.value.should == 'monkey'
|
2009-11-10 16:48:31 -05:00
|
|
|
@driver.find('//input').first.set('gorilla')
|
2009-11-17 17:52:22 -05:00
|
|
|
@driver.find('//input').first.value.should == 'gorilla'
|
2009-11-04 18:34:11 -05:00
|
|
|
end
|
2009-12-11 16:41:12 -05:00
|
|
|
|
2009-11-05 09:10:18 -05:00
|
|
|
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
|
2010-06-24 09:51:44 -04:00
|
|
|
|
2009-12-22 15:13:55 -05:00
|
|
|
it "should extract node visibility" do
|
|
|
|
@driver.find('//a')[0].should be_visible
|
2010-06-24 09:51:44 -04:00
|
|
|
|
2009-12-25 12:50:01 -05:00
|
|
|
@driver.find('//div[@id="hidden"]')[0].should_not be_visible
|
|
|
|
@driver.find('//div[@id="hidden_via_ancestor"]')[0].should_not be_visible
|
2009-12-22 15:13:55 -05:00
|
|
|
end
|
2010-12-23 13:38:44 -05:00
|
|
|
|
|
|
|
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
|
2009-11-04 18:34:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-11-05 11:35:45 -05:00
|
|
|
|
|
|
|
shared_examples_for "driver with javascript support" do
|
2009-12-11 16:41:12 -05:00
|
|
|
before { @driver.visit('/with_js') }
|
|
|
|
|
2009-11-05 11:35:45 -05:00
|
|
|
describe '#find' do
|
|
|
|
it "should find dynamically changed nodes" do
|
|
|
|
@driver.find('//p').first.text.should == 'I changed it'
|
|
|
|
end
|
|
|
|
end
|
2009-12-11 16:41:12 -05:00
|
|
|
|
2009-12-05 13:40:15 -05:00
|
|
|
describe '#drag_to' do
|
|
|
|
it "should drag and drop an object" do
|
2010-07-10 10:36:40 -04:00
|
|
|
pending "drag/drop is currently broken under celerity/culerity" if @driver.is_a?(Capybara::Driver::Celerity)
|
2009-12-05 13:40:15 -05:00
|
|
|
draggable = @driver.find('//div[@id="drag"]').first
|
|
|
|
droppable = @driver.find('//div[@id="drop"]').first
|
|
|
|
draggable.drag_to(droppable)
|
2010-07-10 10:36:40 -04:00
|
|
|
@driver.find('//div[contains(., "Dropped!")]').should_not be_empty
|
2009-12-05 13:40:15 -05:00
|
|
|
end
|
|
|
|
end
|
2009-12-11 16:41:12 -05:00
|
|
|
|
|
|
|
describe "#evaluate_script" do
|
|
|
|
it "should return the value of the executed script" do
|
|
|
|
@driver.evaluate_script('1+1').should == 2
|
|
|
|
end
|
|
|
|
end
|
2009-11-05 11:35:45 -05:00
|
|
|
end
|
2009-12-18 12:50:55 -05:00
|
|
|
|
|
|
|
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
|
2009-12-24 00:41:34 -05:00
|
|
|
|
2010-06-06 23:14:40 -04:00
|
|
|
shared_examples_for "driver with status code support" do
|
|
|
|
it "should make the status code available through status_code" do
|
2010-06-06 21:13:57 -04:00
|
|
|
@driver.visit('/with_simple_html')
|
2010-06-06 23:14:40 -04:00
|
|
|
@driver.status_code.should == 200
|
2010-06-06 21:13:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-06 23:14:40 -04:00
|
|
|
shared_examples_for "driver without status code support" do
|
|
|
|
it "should raise when trying to access the status code available through status_code" do
|
2010-06-06 21:13:57 -04:00
|
|
|
@driver.visit('/with_simple_html')
|
|
|
|
lambda {
|
2010-06-06 23:14:40 -04:00
|
|
|
@driver.status_code
|
2010-06-06 21:13:57 -04:00
|
|
|
}.should raise_error(Capybara::NotSupportedByDriverError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-28 19:03:13 -04:00
|
|
|
shared_examples_for "driver with frame support" do
|
|
|
|
describe '#within_frame' do
|
|
|
|
before(:each) do
|
|
|
|
@driver.visit('/within_frames')
|
2009-12-24 00:41:34 -05:00
|
|
|
end
|
|
|
|
|
2010-04-28 19:03:13 -04:00
|
|
|
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
|
2009-12-24 00:41:34 -05:00
|
|
|
end
|
2010-04-28 19:03:13 -04:00
|
|
|
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'
|
2010-08-27 15:00:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-28 12:57:45 -04:00
|
|
|
shared_examples_for "driver with support for window switching" do
|
2010-08-28 12:50:34 -04:00
|
|
|
describe '#within_window' do
|
2010-08-27 15:00:08 -04:00
|
|
|
before(:each) do
|
|
|
|
@driver.visit('/within_popups')
|
|
|
|
end
|
|
|
|
after(:each) do
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("firstPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@driver.evaluate_script('window.close()')
|
|
|
|
end
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("secondPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@driver.evaluate_script('window.close()')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should find the div in firstPopup" do
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("firstPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@driver.find("//*[@id='divInPopupOne']")[0].text.should eql 'This is the text of divInPopupOne'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
it "should find the div in secondPopup" do
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("secondPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@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
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("secondPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@driver.find("//*[@id='divInPopupTwo']")[0].text.should eql 'This is the text of divInPopupTwo'
|
|
|
|
end
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("firstPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@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
|
2010-08-28 12:50:34 -04:00
|
|
|
@driver.within_window("secondPopup") do
|
2010-08-27 15:00:08 -04:00
|
|
|
@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'
|
2009-12-24 00:41:34 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-06-24 09:51:44 -04:00
|
|
|
|
|
|
|
shared_examples_for "driver with cookies support" do
|
2010-07-29 09:25:45 -04:00
|
|
|
describe "#reset!" do
|
2010-06-24 09:51:44 -04:00
|
|
|
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')
|
|
|
|
|
2010-07-29 09:25:45 -04:00
|
|
|
@driver.reset!
|
2010-06-24 09:51:44 -04:00
|
|
|
@driver.visit('/get_cookie')
|
|
|
|
@driver.body.should_not include('test_cookie')
|
|
|
|
end
|
|
|
|
end
|
2010-06-29 17:11:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples_for "driver with infinite redirect detection" do
|
|
|
|
it "should follow 5 redirects" do
|
|
|
|
@driver.visit('/redirect/5/times')
|
|
|
|
@driver.body.should include('redirection complete')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not follow more than 5 redirects" do
|
|
|
|
running do
|
|
|
|
@driver.visit('/redirect/6/times')
|
|
|
|
end.should raise_error(Capybara::InfiniteRedirectError)
|
|
|
|
end
|
|
|
|
end
|