diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index e7db3fc..dde4507 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -116,13 +116,14 @@ module Webrat def find_link(text_or_title_or_id) #:nodoc: # TODO - Convert to using elements + # + # matching_links = links.select do |possible_link| + # possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id) + # end + require "webrat/core/locators/link_locator" - matching_links = links.select do |possible_link| - possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id) - end - - if matching_links.any? - matching_links.min { |a, b| a.text.length <=> b.text.length } + if link = LinkLocator.new(self, text_or_title_or_id).locate + link else raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}") end diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb new file mode 100644 index 0000000..702df31 --- /dev/null +++ b/lib/webrat/core/locators/link_locator.rb @@ -0,0 +1,20 @@ +class LinkLocator + + def initialize(scope, value) + @scope = scope + @value = value + end + + def locate + matching_links = @scope.send(:links).select do |possible_link| + possible_link.matches_text?(@value) || possible_link.matches_id?(@value) + end + + if matching_links.any? + matching_links.min { |a, b| a.text.length <=> b.text.length } + else + nil + end + end + +end \ No newline at end of file