From 2d4f8b0bee4484c2b3bc65a92b0527a3262e69db Mon Sep 17 00:00:00 2001 From: Jonas Nicklas Date: Wed, 9 Dec 2009 16:37:28 +0100 Subject: [PATCH] Return nil if no links or buttons found --- lib/capybara/session.rb | 18 +++++++++--------- spec/session_spec.rb | 12 ++++-------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 548933148..33abbead0 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -37,11 +37,15 @@ def visit(path) 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 @@ def find_field(locator, *kinds) 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 495deedcc..7c9e73d35 100644 --- a/spec/session_spec.rb +++ b/spec/session_spec.rb @@ -554,10 +554,8 @@ def extract_results(session) @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 @@ def extract_results(session) @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