Skip to content

Commit

Permalink
Namespace locators in Webrat::Locators
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 29, 2008
1 parent d7eec20 commit a472bbf
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 128 deletions.
38 changes: 22 additions & 16 deletions 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 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 matcher
/#{Regexp.escape(@value.to_s)}/i
end

def area_elements
Webrat::XML.css_search(@scope.dom, "area")
end
def area_elements
Webrat::XML.css_search(@scope.dom, "area")
end

end

end
end
74 changes: 40 additions & 34 deletions 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 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 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, *Webrat::ButtonField.xpath_search)
end

end
36 changes: 21 additions & 15 deletions 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 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 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
end
end

def field_elements
Webrat::XML.xpath_search(@scope.dom, *Webrat::Field.xpath_search)
end
def field_elements
Webrat::XML.xpath_search(@scope.dom, *Field.xpath_search)
end

end

end
end
42 changes: 24 additions & 18 deletions 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_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 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 xpath_searches
if @field_types.any?
@field_types.map { |field_type| field_type.xpath_search }.flatten
else
Array(Field.xpath_search)
end

end

end
84 changes: 45 additions & 39 deletions 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
end
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
end
end

def link_elements
Webrat::XML.css_search(@scope.dom, *Webrat::Link.css_search)
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(str)
str.gsub([0xA0].pack('U'), ' ')
end

def replace_nbsp_ref(str)
str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
end
def replace_nbsp_ref(str)
str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
end

end

end
end
18 changes: 12 additions & 6 deletions 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
end
class Locator

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

end

end
end

0 comments on commit a472bbf

Please sign in to comment.