2010-07-19 14:18:16 -04:00
|
|
|
shared_examples_for "find" do
|
2010-01-18 14:33:22 -05:00
|
|
|
describe '#find' do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_html')
|
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
it "should find the first element using the given locator" do
|
|
|
|
@session.find('//h1').text.should == 'This is a test'
|
|
|
|
@session.find("//input[@id='test_field']")[:value].should == 'monkey'
|
|
|
|
end
|
|
|
|
|
2010-07-19 14:18:16 -04:00
|
|
|
it "should be aliased as locate for backward compatibility" do
|
|
|
|
@session.locate('//h1').text.should == 'This is a test'
|
|
|
|
@session.locate("//input[@id='test_field']")[:value].should == 'monkey'
|
|
|
|
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')[:title].should == 'twas a fine link'
|
|
|
|
end
|
|
|
|
|
2010-07-10 06:24:04 -04:00
|
|
|
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
|
2010-07-09 19:19:09 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
context "with css selectors" do
|
2009-12-15 14:58:51 -05:00
|
|
|
it "should find the first element using the given locator" do
|
2010-01-18 14:33:22 -05:00
|
|
|
@session.find(:css, 'h1').text.should == 'This is a test'
|
|
|
|
@session.find(:css, "input[id='test_field']")[:value].should == 'monkey'
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2010-08-20 08:46:56 -04:00
|
|
|
context "with id selectors" do
|
|
|
|
it "should find the first element using the given locator" do
|
|
|
|
@session.find(:id, 'john_monkey').text.should == 'Monkey John'
|
|
|
|
@session.find(:id, 'red').text.should == 'Redirect'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
context "with xpath selectors" do
|
|
|
|
it "should find the first element using the given locator" do
|
|
|
|
@session.find(:xpath, '//h1').text.should == 'This is a test'
|
|
|
|
@session.find(:xpath, "//input[@id='test_field']")[:value].should == 'monkey'
|
2010-01-17 12:41:38 -05:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2010-01-17 12:41:38 -05:00
|
|
|
|
2010-08-17 21:22:54 -04:00
|
|
|
context "with custom selector" do
|
|
|
|
it "should use the custom selector" do
|
|
|
|
Capybara.add_selector(:monkey) { |name| ".//*[@id='#{name}_monkey']" }
|
|
|
|
@session.find(:monkey, 'john').text.should == 'Monkey John'
|
|
|
|
@session.find(:monkey, 'paul').text.should == 'Monkey Paul'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
context "with css as default selector" do
|
|
|
|
before { Capybara.default_selector = :css }
|
|
|
|
it "should find the first element using the given locator" do
|
|
|
|
@session.find('h1').text.should == 'This is a test'
|
|
|
|
@session.find("input[id='test_field']")[:value].should == 'monkey'
|
2010-01-17 12:41:38 -05:00
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
after { Capybara.default_selector = :xpath }
|
|
|
|
end
|
2010-01-17 12:41:38 -05:00
|
|
|
|
2010-07-19 14:18:16 -04:00
|
|
|
it "should raise ElementNotFound with specified fail message if nothing was found" do
|
|
|
|
running do
|
|
|
|
@session.find(:xpath, '//div[@id="nosuchthing"]', :message => 'arghh').should be_nil
|
|
|
|
end.should raise_error(Capybara::ElementNotFound, "arghh")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise ElementNotFound with a useful default message if nothing was found" do
|
|
|
|
running do
|
|
|
|
@session.find(:xpath, '//div[@id="nosuchthing"]').should be_nil
|
|
|
|
end.should raise_error(Capybara::ElementNotFound, "Unable to find '//div[@id=\"nosuchthing\"]'")
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|
2010-01-17 12:41:38 -05:00
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
it "should accept an XPath instance and respect the order of paths" do
|
|
|
|
@session.visit('/form')
|
2010-08-14 12:35:46 -04:00
|
|
|
@xpath = XPath::HTML.fillable_field('Name')
|
2010-01-18 14:33:22 -05:00
|
|
|
@session.find(@xpath).value.should == 'John Smith'
|
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
context "within a scope" do
|
|
|
|
before do
|
|
|
|
@session.visit('/with_scope')
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
|
|
|
|
2010-01-18 14:33:22 -05:00
|
|
|
it "should find the first element using the given locator" do
|
|
|
|
@session.within(:xpath, "//div[@id='for_bar']") do
|
2010-07-09 19:19:09 -04:00
|
|
|
@session.find('.//li').text.should =~ /With Simple HTML/
|
2010-07-19 14:18:16 -04:00
|
|
|
end
|
2009-12-15 14:58:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-01-18 14:33:22 -05:00
|
|
|
end
|