Return nil if no links or buttons found
This commit is contained in:
parent
6cc0b350f4
commit
2d4f8b0bee
|
@ -37,11 +37,15 @@ module Capybara
|
||||||
end
|
end
|
||||||
|
|
||||||
def click_link(locator)
|
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
|
end
|
||||||
|
|
||||||
def click_button(locator)
|
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
|
end
|
||||||
|
|
||||||
def fill_in(locator, options={})
|
def fill_in(locator, options={})
|
||||||
|
@ -132,16 +136,12 @@ module Capybara
|
||||||
alias_method :field_labeled, :find_field
|
alias_method :field_labeled, :find_field
|
||||||
|
|
||||||
def find_link(locator)
|
def find_link(locator)
|
||||||
link = find("//a[@id='#{locator}' or contains(.,'#{locator}') or @title='#{locator}']").first
|
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_button(locator)
|
def find_button(locator)
|
||||||
button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first \
|
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
|
button || 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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -554,10 +554,8 @@ shared_examples_for "session" do
|
||||||
@session.find_link('labore')[:href].should == "/with_simple_html"
|
@session.find_link('labore')[:href].should == "/with_simple_html"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an error if the field doesn't exist" do
|
it "should return nil if the field doesn't exist" do
|
||||||
running {
|
@session.find_link('Does not exist').should be_nil
|
||||||
@session.find_link('Does not exist')
|
|
||||||
}.should raise_error(Capybara::ElementNotFound)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -571,10 +569,8 @@ shared_examples_for "session" do
|
||||||
@session.find_button('crap321').value.should == "crappy"
|
@session.find_button('crap321').value.should == "crappy"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should raise an error if the field doesn't exist" do
|
it "should return nil if the field doesn't exist" do
|
||||||
running {
|
@session.find_button('Does not exist').should be_nil
|
||||||
@session.find_button('Does not exist')
|
|
||||||
}.should raise_error(Capybara::ElementNotFound)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue