From ed8b39cccc659ebf99134bef85462027bf135aea Mon Sep 17 00:00:00 2001 From: Luke Melia Date: Thu, 13 Mar 2008 18:01:00 -0400 Subject: [PATCH] Changed clicks_link to find the shortest matching link. --- lib/webrat/session.rb | 8 +++++++- test/clicks_link_test.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/webrat/session.rb b/lib/webrat/session.rb index 1daeb78..4eeccaa 100644 --- a/lib/webrat/session.rb +++ b/lib/webrat/session.rb @@ -24,7 +24,8 @@ module ActionController # Example: # clicks_link "Sign up" 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? onclick = link.attributes["onclick"] @@ -226,6 +227,11 @@ module ActionController request_page(http_method, link.attributes["href"]) 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: input = find_field_by_name(name_or_label) return input if input diff --git a/test/clicks_link_test.rb b/test/clicks_link_test.rb index 5f63ada..4131a00 100644 --- a/test/clicks_link_test.rb +++ b/test/clicks_link_test.rb @@ -159,4 +159,15 @@ class ClicksLinkTest < Test::Unit::TestCase @session.expects(:get_via_redirect).with("/page1", {}) @session.clicks_link "Link text" end + + def test_should_choose_the_shortest_link_text_match + @response.stubs(:body).returns(<<-EOS) + Linkerama + Link + EOS + + @session.expects(:get_via_redirect).with("/page2", {}) + @session.clicks_link "Link" + end + end