Skip to content

Commit

Permalink
Refactoring locators
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent 04959ae commit eb95f6c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/webrat/core/locators.rb
Expand Up @@ -2,7 +2,7 @@

module Webrat
module Locators

def field_by_xpath(xpath)
field_by_element(Webrat::XML.xpath_at(dom, xpath))
end
Expand Down
9 changes: 3 additions & 6 deletions lib/webrat/core/locators/area_locator.rb
@@ -1,9 +1,6 @@
class AreaLocator

def initialize(scope, value)
@scope = scope
@value = value
end
require "webrat/core/locators/locator"

class AreaLocator < Locator

def locate
@scope.area_by_element(area_element)
Expand Down
9 changes: 3 additions & 6 deletions lib/webrat/core/locators/button_locator.rb
@@ -1,9 +1,6 @@
class ButtonLocator

def initialize(scope, value)
@scope = scope
@value = value
end
require "webrat/core/locators/locator"

class ButtonLocator < Locator

def locate
@scope.field_by_element(button_element)
Expand Down
9 changes: 3 additions & 6 deletions lib/webrat/core/locators/field_by_id_locator.rb
@@ -1,9 +1,6 @@
class FieldByIdLocator

def initialize(scope, value)
@scope = scope
@value = value
end
require "webrat/core/locators/locator"

class FieldByIdLocator < Locator

def locate
@scope.field_by_element(field_element)
Expand Down
26 changes: 11 additions & 15 deletions lib/webrat/core/locators/link_locator.rb
@@ -1,24 +1,20 @@
class LinkLocator
require "webrat/core/locators/locator"

class LinkLocator < Locator

def initialize(scope, value)
@scope = scope
@value = value
def locate
@scope.link_by_element(link_element)
end

def locate
# TODO - Convert to using elements

matching_links = link_elements.select do |link_element|
def link_element
matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length }
end

def matching_links
@matching_links ||= link_elements.select do |link_element|
matches_text?(link_element) ||
matches_id?(link_element)
end

if matching_links.any?
link_element = matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length }
@scope.link_by_element(link_element)
else
nil
end
end

def matches_text?(link)
Expand Down
8 changes: 8 additions & 0 deletions lib/webrat/core/locators/locator.rb
@@ -0,0 +1,8 @@
class Locator

def initialize(scope, value)
@scope = scope
@value = value
end

end

0 comments on commit eb95f6c

Please sign in to comment.