Working on FieldLabeled locator

This commit is contained in:
Bryan Helmkamp 2008-11-30 00:12:46 -05:00
parent 27626ea389
commit 53979cbb52
3 changed files with 19 additions and 17 deletions

View File

@ -63,18 +63,6 @@ module Webrat
possible_fields.detect { |possible_field| possible_field.matches_name?(name) } possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
end 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) def matches_id?(id)
Webrat::XML.attribute(@element, "id") == id.to_s Webrat::XML.attribute(@element, "id") == id.to_s
end end

View File

@ -1,6 +1,8 @@
module Webrat module Webrat
class Label #:nodoc: class Label #:nodoc:
attr_reader :element
def self.xpath_search def self.xpath_search
".//label" ".//label"
end end
@ -10,10 +12,6 @@ module Webrat
@element = element @element = element
end end
def matches_text?(label_text)
text =~ /^\W*#{Regexp.escape(label_text.to_s)}\b/i
end
def text def text
str = Webrat::XML.all_inner_text(@element) str = Webrat::XML.all_inner_text(@element)
str.gsub!("\n","") str.gsub!("\n","")

View File

@ -9,7 +9,15 @@ module Webrat
def locate def locate
# TODO - Convert to using elements # TODO - Convert to using elements
@scope.send(:forms).detect_mapped do |form| @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
end end
@ -17,6 +25,14 @@ module Webrat
"Could not find field labeled #{@value.inspect}" "Could not find field labeled #{@value.inspect}"
end end
def text(label)
str = Webrat::XML.all_inner_text(label.element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")
str
end
end end
def field_labeled(label, *field_types) def field_labeled(label, *field_types)