diff --git a/lib/webrat/core/elements/label.rb b/lib/webrat/core/elements/label.rb index c2426c5..9e7f29b 100644 --- a/lib/webrat/core/elements/label.rb +++ b/lib/webrat/core/elements/label.rb @@ -8,14 +8,6 @@ module Webrat def self.xpath_search ".//label" end - - def text - str = Webrat::XML.all_inner_text(@element) - str.gsub!("\n","") - str.strip! - str.squeeze!(" ") - str - end def for_id Webrat::XML.attribute(@element, "for") @@ -25,6 +17,8 @@ module Webrat Field.load(@session, field_element) end + protected + def field_element if for_id.blank? Webrat::XML.xpath_at(@element, *Field.xpath_search) diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index c07096c..76846cc 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -7,15 +7,13 @@ module Webrat class FieldLabeledLocator < Locator def locate - matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } - end - - def matching_fields - matching_labels.map(&:field).compact.uniq + matching_labels.any? && matching_labels.first.field end def matching_labels - matching_label_elements.map do |label_element| + matching_label_elements.sort_by do |label_element| + text(label_element).length + end.map do |label_element| Label.load(@scope.session, label_element) end end