diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 54893314..33abbead 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -37,11 +37,15 @@ module Capybara end def click_link(locator) - find_link(locator).click + link = find_link(locator) + raise Capybara::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link + link.click end def click_button(locator) - find_button(locator).click + button = find_button(locator) + raise Capybara::ElementNotFound, "no button with value or id or text '#{locator}' found" unless button + button.click end def fill_in(locator, options={}) @@ -132,16 +136,12 @@ module Capybara alias_method :field_labeled, :find_field def find_link(locator) - link = find("//a[@id='#{locator}' or contains(.,'#{locator}') or @title='#{locator}']").first - raise Capybara::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link - link + find("//a[@id='#{locator}' or contains(.,'#{locator}') or @title='#{locator}']").first end def find_button(locator) - button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first \ - || find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]").first - raise Capybara::ElementNotFound, "no button with value or id or text '#{locator}' found" unless button - button + button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first + button || find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]").first end private diff --git a/spec/session_spec.rb b/spec/session_spec.rb index 495deedc..7c9e73d3 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -554,10 +554,8 @@ shared_examples_for "session" do @session.find_link('labore')[:href].should == "/with_simple_html" end - it "should raise an error if the field doesn't exist" do - running { - @session.find_link('Does not exist') - }.should raise_error(Capybara::ElementNotFound) + it "should return nil if the field doesn't exist" do + @session.find_link('Does not exist').should be_nil end end @@ -571,10 +569,8 @@ shared_examples_for "session" do @session.find_button('crap321').value.should == "crappy" end - it "should raise an error if the field doesn't exist" do - running { - @session.find_button('Does not exist') - }.should raise_error(Capybara::ElementNotFound) + it "should return nil if the field doesn't exist" do + @session.find_button('Does not exist').should be_nil end end