From a472bbfbde844fb5a45e99b160fb714e82c47921 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 29 Nov 2008 02:00:57 -0500 Subject: [PATCH] Namespace locators in Webrat::Locators --- lib/webrat/core/locators/area_locator.rb | 40 +++++---- lib/webrat/core/locators/button_locator.rb | 72 ++++++++------- .../core/locators/field_by_id_locator.rb | 38 ++++---- .../core/locators/field_named_locator.rb | 46 +++++----- lib/webrat/core/locators/link_locator.rb | 88 ++++++++++--------- lib/webrat/core/locators/locator.rb | 18 ++-- 6 files changed, 169 insertions(+), 133 deletions(-) diff --git a/lib/webrat/core/locators/area_locator.rb b/lib/webrat/core/locators/area_locator.rb index 301a53d..35f3992 100644 --- a/lib/webrat/core/locators/area_locator.rb +++ b/lib/webrat/core/locators/area_locator.rb @@ -1,24 +1,30 @@ require "webrat/core/locators/locator" -class AreaLocator < Locator +module Webrat + module Locators + + class AreaLocator < Locator - def locate - @scope.area_by_element(area_element) - end + def locate + @scope.area_by_element(area_element) + end + + def area_element + area_elements.detect do |area_element| + Webrat::XML.attribute(area_element, "title") =~ matcher || + Webrat::XML.attribute(area_element, "id") =~ matcher + end + end + + def matcher + /#{Regexp.escape(@value.to_s)}/i + end + + def area_elements + Webrat::XML.css_search(@scope.dom, "area") + end - def area_element - area_elements.detect do |area_element| - Webrat::XML.attribute(area_element, "title") =~ matcher || - Webrat::XML.attribute(area_element, "id") =~ matcher end + end - - def matcher - /#{Regexp.escape(@value.to_s)}/i - end - - def area_elements - Webrat::XML.css_search(@scope.dom, "area") - end - end \ No newline at end of file diff --git a/lib/webrat/core/locators/button_locator.rb b/lib/webrat/core/locators/button_locator.rb index c8bcbf8..80aa7f0 100644 --- a/lib/webrat/core/locators/button_locator.rb +++ b/lib/webrat/core/locators/button_locator.rb @@ -1,40 +1,46 @@ require "webrat/core/locators/locator" -class ButtonLocator < Locator +module Webrat + module Locators + + class ButtonLocator < Locator - def locate - @scope.field_by_element(button_element) - end + def locate + @scope.field_by_element(button_element) + end + + def button_element + button_elements.detect do |element| + @value.nil? || + matches_id?(element) || + matches_value?(element) || + matches_html?(element) || + matches_alt?(element) + end + end + + def matches_id?(element) + (@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) || + (!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s) + end + + def matches_value?(element) + Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i + end + + def matches_html?(element) + Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i + end + + def matches_alt?(element) + Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i + end + + def button_elements + Webrat::XML.xpath_search(@scope.dom, *ButtonField.xpath_search) + end - def button_element - button_elements.detect do |element| - @value.nil? || - matches_id?(element) || - matches_value?(element) || - matches_html?(element) || - matches_alt?(element) end + end - - def matches_id?(element) - (@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) || - (!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s) - end - - def matches_value?(element) - Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i - end - - def matches_html?(element) - Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i - end - - def matches_alt?(element) - Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i - end - - def button_elements - Webrat::XML.xpath_search(@scope.dom, *Webrat::ButtonField.xpath_search) - end - end \ No newline at end of file diff --git a/lib/webrat/core/locators/field_by_id_locator.rb b/lib/webrat/core/locators/field_by_id_locator.rb index 1d920dc..f30794c 100644 --- a/lib/webrat/core/locators/field_by_id_locator.rb +++ b/lib/webrat/core/locators/field_by_id_locator.rb @@ -1,23 +1,29 @@ require "webrat/core/locators/locator" -class FieldByIdLocator < Locator +module Webrat + module Locators + + class FieldByIdLocator < Locator - def locate - @scope.field_by_element(field_element) - end - - def field_element - field_elements.detect do |field_element| - if @value.is_a?(Regexp) - Webrat::XML.attribute(field_element, "id") =~ @value - else - Webrat::XML.attribute(field_element, "id") == @value.to_s + def locate + @scope.field_by_element(field_element) end + + def field_element + field_elements.detect do |field_element| + if @value.is_a?(Regexp) + Webrat::XML.attribute(field_element, "id") =~ @value + else + Webrat::XML.attribute(field_element, "id") == @value.to_s + end + end + end + + def field_elements + Webrat::XML.xpath_search(@scope.dom, *Field.xpath_search) + end + end + end - - def field_elements - Webrat::XML.xpath_search(@scope.dom, *Webrat::Field.xpath_search) - end - end \ No newline at end of file diff --git a/lib/webrat/core/locators/field_named_locator.rb b/lib/webrat/core/locators/field_named_locator.rb index f18a2fc..1e26819 100644 --- a/lib/webrat/core/locators/field_named_locator.rb +++ b/lib/webrat/core/locators/field_named_locator.rb @@ -1,27 +1,33 @@ require "webrat/core/locators/locator" -class FieldNamedLocator < Locator +module Webrat + module Locators + + class FieldNamedLocator < Locator - def locate - @scope.field_by_element(field_element) - end + def locate + @scope.field_by_element(field_element) + end + + def field_element + field_elements.detect do |field_element| + Webrat::XML.attribute(field_element, "name") == @value.to_s + end + end + + def field_elements + Webrat::XML.xpath_search(@scope.dom, *xpath_searches) + end + + def xpath_searches + if @field_types.any? + @field_types.map { |field_type| field_type.xpath_search }.flatten + else + Array(Field.xpath_search) + end + end - def field_element - field_elements.detect do |field_element| - Webrat::XML.attribute(field_element, "name") == @value.to_s end + end - - def field_elements - Webrat::XML.xpath_search(@scope.dom, *xpath_searches) - end - - def xpath_searches - if @field_types.any? - @field_types.map { |field_type| field_type.xpath_search }.flatten - else - Array(Field.xpath_search) - end - end - end \ No newline at end of file diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb index 0b08bd5..2097bc1 100644 --- a/lib/webrat/core/locators/link_locator.rb +++ b/lib/webrat/core/locators/link_locator.rb @@ -1,52 +1,58 @@ require "webrat/core/locators/locator" -class LinkLocator < Locator +module Webrat + module Locators + + class LinkLocator < Locator - def locate - @scope.link_by_element(link_element) - end + def locate + @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 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 - end + def matching_links + @matching_links ||= link_elements.select do |link_element| + matches_text?(link_element) || + matches_id?(link_element) + end + end - def matches_text?(link) - if @value.is_a?(Regexp) - matcher = @value - else - matcher = /#{Regexp.escape(@value.to_s)}/i - end + def matches_text?(link) + if @value.is_a?(Regexp) + matcher = @value + else + matcher = /#{Regexp.escape(@value.to_s)}/i + end - replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher || - replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || - Webrat::XML.attribute(link, "title")=~ matcher - end + replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher || + replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || + Webrat::XML.attribute(link, "title")=~ matcher + end - def matches_id?(link) - if @value.is_a?(Regexp) - (Webrat::XML.attribute(link, "id") =~ @value) ? true : false - else - (Webrat::XML.attribute(link, "id") == @value) ? true : false + def matches_id?(link) + if @value.is_a?(Regexp) + (Webrat::XML.attribute(link, "id") =~ @value) ? true : false + else + (Webrat::XML.attribute(link, "id") == @value) ? true : false + end + end + + def link_elements + Webrat::XML.css_search(@scope.dom, *Link.css_search) + end + + def replace_nbsp(str) + str.gsub([0xA0].pack('U'), ' ') + end + + def replace_nbsp_ref(str) + str.gsub(' ',' ').gsub(' ', ' ') + end + end + end - - def link_elements - Webrat::XML.css_search(@scope.dom, *Webrat::Link.css_search) - end - - def replace_nbsp(str) - str.gsub([0xA0].pack('U'), ' ') - end - - def replace_nbsp_ref(str) - str.gsub(' ',' ').gsub(' ', ' ') - end - end \ No newline at end of file diff --git a/lib/webrat/core/locators/locator.rb b/lib/webrat/core/locators/locator.rb index e2bf815..84a78bd 100644 --- a/lib/webrat/core/locators/locator.rb +++ b/lib/webrat/core/locators/locator.rb @@ -1,9 +1,15 @@ -class Locator +module Webrat + module Locators - def initialize(scope, value, *field_types) - @scope = scope - @value = value - @field_types = field_types + class Locator + + def initialize(scope, value, *field_types) + @scope = scope + @value = value + @field_types = field_types + end + + end + end - end \ No newline at end of file