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 = nil
fields # preload fields # preload
labels # preload
end end
def find_select_option(option_text) def find_select_option(option_text)
@ -46,7 +47,11 @@ module Webrat
end end
def labels 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 end
def submit def submit

View File

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

View File

@ -7,11 +7,25 @@ module Webrat
class LabelLocator < Locator class LabelLocator < Locator
def locate def locate
# TODO - Convert to using elements @scope.element_to_webrat_element(label_element)
@scope.send(:forms).detect_mapped do |form|
form.label_matching(@value)
end 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 end