Extracting LinkLocator object
This commit is contained in:
parent
0b6d9c28ea
commit
9254397807
@ -116,13 +116,14 @@ module Webrat
|
|||||||
|
|
||||||
def find_link(text_or_title_or_id) #:nodoc:
|
def find_link(text_or_title_or_id) #:nodoc:
|
||||||
# TODO - Convert to using elements
|
# 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|
|
if link = LinkLocator.new(self, text_or_title_or_id).locate
|
||||||
possible_link.matches_text?(text_or_title_or_id) || possible_link.matches_id?(text_or_title_or_id)
|
link
|
||||||
end
|
|
||||||
|
|
||||||
if matching_links.any?
|
|
||||||
matching_links.min { |a, b| a.text.length <=> b.text.length }
|
|
||||||
else
|
else
|
||||||
raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
|
raise NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}")
|
||||||
end
|
end
|
||||||
|
20
lib/webrat/core/locators/link_locator.rb
Normal file
20
lib/webrat/core/locators/link_locator.rb
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user