Changed clicks_link to find the shortest matching link.

This commit is contained in:
Luke Melia 2008-03-13 18:01:00 -04:00
parent 9fe0ef33af
commit ed8b39cccc
2 changed files with 18 additions and 1 deletions

View File

@ -24,7 +24,8 @@ module ActionController
# Example: # Example:
# clicks_link "Sign up" # clicks_link "Sign up"
def clicks_link(link_text) def clicks_link(link_text)
link = links.detect { |el| el.innerHTML =~ /#{link_text}/i } link = find_shortest_matching_link(link_text)
return flunk("No link with text #{link_text.inspect} was found") if link.nil? return flunk("No link with text #{link_text.inspect} was found") if link.nil?
onclick = link.attributes["onclick"] onclick = link.attributes["onclick"]
@ -226,6 +227,11 @@ module ActionController
request_page(http_method, link.attributes["href"]) request_page(http_method, link.attributes["href"])
end end
def find_shortest_matching_link(link_text)
candidates = links.select { |el| el.innerHTML =~ /#{link_text}/i }
candidates.sort_by { |el| el.innerText.strip.size }.first
end
def find_field_by_name_or_label(name_or_label) # :nodoc: def find_field_by_name_or_label(name_or_label) # :nodoc:
input = find_field_by_name(name_or_label) input = find_field_by_name(name_or_label)
return input if input return input if input

View File

@ -159,4 +159,15 @@ class ClicksLinkTest < Test::Unit::TestCase
@session.expects(:get_via_redirect).with("/page1", {}) @session.expects(:get_via_redirect).with("/page1", {})
@session.clicks_link "Link text" @session.clicks_link "Link text"
end end
def test_should_choose_the_shortest_link_text_match
@response.stubs(:body).returns(<<-EOS)
<a href="/page1">Linkerama</a>
<a href="/page2">Link</a>
EOS
@session.expects(:get_via_redirect).with("/page2", {})
@session.clicks_link "Link"
end
end end