diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index b9c8b9e..f194477 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -2,7 +2,7 @@ require "webrat/core_extensions/detect_mapped" module Webrat module Locators - + def field_by_xpath(xpath) field_by_element(Webrat::XML.xpath_at(dom, xpath)) end diff --git a/lib/webrat/core/locators/area_locator.rb b/lib/webrat/core/locators/area_locator.rb index 124884e..301a53d 100644 --- a/lib/webrat/core/locators/area_locator.rb +++ b/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) diff --git a/lib/webrat/core/locators/button_locator.rb b/lib/webrat/core/locators/button_locator.rb index 6586363..c8bcbf8 100644 --- a/lib/webrat/core/locators/button_locator.rb +++ b/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) diff --git a/lib/webrat/core/locators/field_by_id_locator.rb b/lib/webrat/core/locators/field_by_id_locator.rb index 8193719..1d920dc 100644 --- a/lib/webrat/core/locators/field_by_id_locator.rb +++ b/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) diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb index f891a9c..0b08bd5 100644 --- a/lib/webrat/core/locators/link_locator.rb +++ b/lib/webrat/core/locators/link_locator.rb @@ -1,24 +1,20 @@ -class LinkLocator - - def initialize(scope, value) - @scope = scope - @value = value - end +require "webrat/core/locators/locator" + +class LinkLocator < Locator def locate - # TODO - Convert to using elements - - matching_links = link_elements.select do |link_element| + @scope.link_by_element(link_element) + end + + 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) diff --git a/lib/webrat/core/locators/locator.rb b/lib/webrat/core/locators/locator.rb new file mode 100644 index 0000000..a2557d2 --- /dev/null +++ b/lib/webrat/core/locators/locator.rb @@ -0,0 +1,8 @@ +class Locator + + def initialize(scope, value) + @scope = scope + @value = value + end + +end \ No newline at end of file