Refactoring label locating to use elements

This commit is contained in:
Bryan Helmkamp 2008-11-29 23:59:26 -05:00
parent 67dd6d7c58
commit 27626ea389
3 changed files with 28 additions and 5 deletions

View File

@ -15,6 +15,7 @@ module Webrat
@fields = nil
fields # preload
labels # preload
end
def find_select_option(option_text)
@ -46,7 +47,11 @@ module Webrat
end
def labels
@labels ||= Webrat::XML.css_search(element, "label").map { |element| Label.new(nil, element) }
@labels ||= Webrat::XML.css_search(element, "label").map do |element|
label = Label.new(nil, element)
@session.elements[Webrat::XML.xpath_to(element)] = label
label
end
end
def submit

View File

@ -1,6 +1,10 @@
module Webrat
class Label #:nodoc:
def self.xpath_search
".//label"
end
def initialize(field, element)
@field = field
@element = element

View File

@ -7,13 +7,27 @@ module Webrat
class LabelLocator < Locator
def locate
# TODO - Convert to using elements
@scope.send(:forms).detect_mapped do |form|
form.label_matching(@value)
@scope.element_to_webrat_element(label_element)
end
def label_element
label_elements.detect do |label_element|
text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i
end
end
def label_elements
Webrat::XML.xpath_search(@scope.dom, Label.xpath_search)
end
def text(label_element)
str = Webrat::XML.all_inner_text(label_element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
end
end