Starting to split up locator strategies

This commit is contained in:
Bryan Helmkamp 2008-11-05 19:26:06 -05:00
parent 39e1b98897
commit 979b84bb48
2 changed files with 56 additions and 21 deletions

View File

@ -12,11 +12,9 @@ module Webrat
end end
def find_field(id_or_name_or_label, *field_types) def find_field(id_or_name_or_label, *field_types)
possible_fields = fields_by_type(field_types) find_field_by_id(id_or_name_or_label, *field_types) ||
find_field_by_name(id_or_name_or_label, *field_types) ||
find_field_by_id(possible_fields, id_or_name_or_label) || find_field_by_label(id_or_name_or_label, *field_types) ||
find_field_by_name(possible_fields, id_or_name_or_label) ||
find_field_by_label(possible_fields, id_or_name_or_label) ||
nil nil
end end
@ -50,23 +48,26 @@ module Webrat
@session.request_page(form_action, form_method, params) @session.request_page(form_action, form_method, params)
end end
protected def find_field_by_id(id, *field_types)
possible_fields = fields_by_type(field_types)
def find_field_by_id(possible_fields, id)
possible_fields.detect { |possible_field| possible_field.matches_id?(id) } possible_fields.detect { |possible_field| possible_field.matches_id?(id) }
end end
def find_field_by_name(possible_fields, name) def find_field_by_name(name, *field_types)
possible_fields = fields_by_type(field_types)
possible_fields.detect { |possible_field| possible_field.matches_name?(name) } possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
end end
def find_field_by_label(possible_fields, label) def find_field_by_label(label, *field_types)
possible_fields = fields_by_type(field_types)
matching_fields = possible_fields.select do |possible_field| matching_fields = possible_fields.select do |possible_field|
possible_field.matches_label?(label) possible_field.matches_label?(label)
end end
matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length } matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length }
end end
protected
def fields_by_type(field_types) def fields_by_type(field_types)
fields.select { |f| field_types.include?(f.class) } fields.select { |f| field_types.include?(f.class) }
end end

View File

@ -1,8 +1,51 @@
module Webrat module Webrat
module Locators module Locators
def field_labeled(label) def find_field(*args)
find_field(label, TextField, TextareaField, CheckboxField, RadioField, HiddenField) # This is the default locator strategy
field_with_id(*args) ||
field_with_name(*args) ||
field_labeled(*args)
end
def field_labeled(label, *field_types)
if field_types.empty?
field_types = [TextField, TextareaField, CheckboxField, RadioField, HiddenField]
end
forms.each do |form|
result = form.find_field_by_label(label, *field_types)
return result if result
end
flunk("Could not find #{field_types.inspect}: #{label.inspect}")
end
def field_with_id(id, *field_types)
if field_types.empty?
field_types = [TextField, TextareaField, CheckboxField, RadioField, HiddenField]
end
forms.each do |form|
result = form.find_field_by_id(id, *field_types)
return result if result
end
return nil
end
def field_with_name(name, *field_types)
if field_types.empty?
field_types = [TextField, TextareaField, CheckboxField, RadioField, HiddenField]
end
forms.each do |form|
result = form.find_field_by_name(name, *field_types)
return result if result
end
return nil
end end
def find_select_option(option_text, id_or_name_or_label) def find_select_option(option_text, id_or_name_or_label)
@ -43,14 +86,5 @@ module Webrat
end end
end end
def find_field(id_or_name_or_label, *field_types)
forms.each do |form|
result = form.find_field(id_or_name_or_label, *field_types)
return result if result
end
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
end
end end
end end