Skip to content

Commit

Permalink
Return nil if no links or buttons found
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Dec 9, 2009
1 parent 6cc0b35 commit 2d4f8b0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
18 changes: 9 additions & 9 deletions lib/capybara/session.rb
Expand Up @@ -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={})
Expand Down Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions spec/session_spec.rb
Expand Up @@ -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

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

Expand Down

0 comments on commit 2d4f8b0

Please sign in to comment.