diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 5667602..13b3d7a 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -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 diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index 7131bd0..f0de122 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -1,6 +1,10 @@ module Webrat class Label #:nodoc: + def self.xpath_search + ".//label" + end + def initialize(field, element) @field = field @element = element diff --git a/lib/webrat/core/locators/label_locator.rb b/lib/webrat/core/locators/label_locator.rb index f231449..ae313e8 100644 --- a/lib/webrat/core/locators/label_locator.rb +++ b/lib/webrat/core/locators/label_locator.rb @@ -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