Namespace locators in Webrat::Locators

This commit is contained in:
Bryan Helmkamp 2008-11-29 02:00:57 -05:00
parent d7eec20950
commit a472bbfbde
6 changed files with 169 additions and 133 deletions

View File

@ -1,24 +1,30 @@
require "webrat/core/locators/locator" require "webrat/core/locators/locator"
class AreaLocator < Locator module Webrat
module Locators
class AreaLocator < Locator
def locate def locate
@scope.area_by_element(area_element) @scope.area_by_element(area_element)
end 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
end end
def matcher
/#{Regexp.escape(@value.to_s)}/i
end
def area_elements
Webrat::XML.css_search(@scope.dom, "area")
end
end end

View File

@ -1,40 +1,46 @@
require "webrat/core/locators/locator" require "webrat/core/locators/locator"
class ButtonLocator < Locator module Webrat
module Locators
class ButtonLocator < Locator
def locate def locate
@scope.field_by_element(button_element) @scope.field_by_element(button_element)
end 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
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 end

View File

@ -1,23 +1,29 @@
require "webrat/core/locators/locator" require "webrat/core/locators/locator"
class FieldByIdLocator < Locator module Webrat
module Locators
class FieldByIdLocator < Locator
def locate def locate
@scope.field_by_element(field_element) @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
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
end end
def field_elements
Webrat::XML.xpath_search(@scope.dom, *Webrat::Field.xpath_search)
end
end end

View File

@ -1,27 +1,33 @@
require "webrat/core/locators/locator" require "webrat/core/locators/locator"
class FieldNamedLocator < Locator module Webrat
module Locators
class FieldNamedLocator < Locator
def locate def locate
@scope.field_by_element(field_element) @scope.field_by_element(field_element)
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
def field_element
field_elements.detect do |field_element|
Webrat::XML.attribute(field_element, "name") == @value.to_s
end end
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 end

View File

@ -1,52 +1,58 @@
require "webrat/core/locators/locator" require "webrat/core/locators/locator"
class LinkLocator < Locator module Webrat
module Locators
class LinkLocator < Locator
def locate def locate
@scope.link_by_element(link_element) @scope.link_by_element(link_element)
end end
def link_element def link_element
matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length } matching_links.min { |a, b| Webrat::XML.inner_text(a).length <=> Webrat::XML.inner_text(b).length }
end end
def matching_links def matching_links
@matching_links ||= link_elements.select do |link_element| @matching_links ||= link_elements.select do |link_element|
matches_text?(link_element) || matches_text?(link_element) ||
matches_id?(link_element) matches_id?(link_element)
end end
end end
def matches_text?(link) def matches_text?(link)
if @value.is_a?(Regexp) if @value.is_a?(Regexp)
matcher = @value matcher = @value
else else
matcher = /#{Regexp.escape(@value.to_s)}/i matcher = /#{Regexp.escape(@value.to_s)}/i
end end
replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher || replace_nbsp(Webrat::XML.inner_text(link)) =~ matcher ||
replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
Webrat::XML.attribute(link, "title")=~ matcher Webrat::XML.attribute(link, "title")=~ matcher
end end
def matches_id?(link) def matches_id?(link)
if @value.is_a?(Regexp) if @value.is_a?(Regexp)
(Webrat::XML.attribute(link, "id") =~ @value) ? true : false (Webrat::XML.attribute(link, "id") =~ @value) ? true : false
else else
(Webrat::XML.attribute(link, "id") == @value) ? true : false (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('&#xA0;',' ').gsub('&nbsp;', ' ')
end
end 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('&#xA0;',' ').gsub('&nbsp;', ' ')
end
end end

View File

@ -1,9 +1,15 @@
class Locator module Webrat
module Locators
def initialize(scope, value, *field_types) class Locator
@scope = scope
@value = value def initialize(scope, value, *field_types)
@field_types = field_types @scope = scope
@value = value
@field_types = field_types
end
end
end end
end end