Return nil if no links or buttons found

This commit is contained in:
Jonas Nicklas 2009-12-09 16:37:28 +01:00
parent 6cc0b350f4
commit 2d4f8b0bee
2 changed files with 13 additions and 17 deletions

View File

@ -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

View File

@ -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