diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 13b3d7a..024adc4 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -63,18 +63,6 @@ module Webrat possible_fields.detect { |possible_field| possible_field.matches_name?(name) } end - def field_labeled(label, *field_types) - possible_fields = fields_by_type(field_types) - matching_fields = possible_fields.select do |possible_field| - possible_field.matches_label?(label) - end - matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } - end - - def label_matching(label_text) - labels.detect { |label| label.matches_text?(label_text) } - end - def matches_id?(id) Webrat::XML.attribute(@element, "id") == id.to_s end diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index f0de122..2508823 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -1,6 +1,8 @@ module Webrat class Label #:nodoc: + attr_reader :element + def self.xpath_search ".//label" end @@ -10,10 +12,6 @@ module Webrat @element = element end - def matches_text?(label_text) - text =~ /^\W*#{Regexp.escape(label_text.to_s)}\b/i - end - def text str = Webrat::XML.all_inner_text(@element) str.gsub!("\n","") diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index 7c0987e..1447f35 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -9,7 +9,15 @@ module Webrat def locate # TODO - Convert to using elements @scope.send(:forms).detect_mapped do |form| - form.field_labeled(@value, *@field_types) + possible_fields = form.send(:fields_by_type, @field_types) + + matching_fields = possible_fields.select do |possible_field| + possible_field.send(:labels).any? do |label| + text(label) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i + end + end + + matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } end end @@ -17,6 +25,14 @@ module Webrat "Could not find field labeled #{@value.inspect}" end + def text(label) + str = Webrat::XML.all_inner_text(label.element) + str.gsub!("\n","") + str.strip! + str.squeeze!(" ") + str + end + end def field_labeled(label, *field_types)