Refactoring locator methods

This commit is contained in:
Bryan Helmkamp 2008-11-05 20:19:13 -05:00
parent 979b84bb48
commit da7017acd6
2 changed files with 15 additions and 23 deletions

View File

@ -11,10 +11,10 @@ module Webrat
@fields = nil @fields = nil
end end
def find_field(id_or_name_or_label, *field_types) def find_field(locator, *field_types)
find_field_by_id(id_or_name_or_label, *field_types) || field_by_id(locator, *field_types) ||
find_field_by_name(id_or_name_or_label, *field_types) || field_by_name(locator, *field_types) ||
find_field_by_label(id_or_name_or_label, *field_types) || field_by_label(locator, *field_types) ||
nil nil
end end
@ -48,17 +48,17 @@ module Webrat
@session.request_page(form_action, form_method, params) @session.request_page(form_action, form_method, params)
end end
def find_field_by_id(id, *field_types) def field_by_id(id, *field_types)
possible_fields = fields_by_type(field_types) possible_fields = fields_by_type(field_types)
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(name, *field_types) def field_by_name(name, *field_types)
possible_fields = fields_by_type(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(label, *field_types) def field_by_label(label, *field_types)
possible_fields = fields_by_type(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)
@ -69,7 +69,11 @@ module Webrat
protected protected
def fields_by_type(field_types) def fields_by_type(field_types)
if field_types.any?
fields.select { |f| field_types.include?(f.class) } fields.select { |f| field_types.include?(f.class) }
else
fields
end
end end
def params def params

View File

@ -10,12 +10,8 @@ module Webrat
end end
def field_labeled(label, *field_types) def field_labeled(label, *field_types)
if field_types.empty?
field_types = [TextField, TextareaField, CheckboxField, RadioField, HiddenField]
end
forms.each do |form| forms.each do |form|
result = form.find_field_by_label(label, *field_types) result = form.field_by_label(label, *field_types)
return result if result return result if result
end end
@ -23,12 +19,8 @@ module Webrat
end end
def field_with_id(id, *field_types) def field_with_id(id, *field_types)
if field_types.empty?
field_types = [TextField, TextareaField, CheckboxField, RadioField, HiddenField]
end
forms.each do |form| forms.each do |form|
result = form.find_field_by_id(id, *field_types) result = form.field_by_id(id, *field_types)
return result if result return result if result
end end
@ -36,12 +28,8 @@ module Webrat
end end
def field_with_name(name, *field_types) def field_with_name(name, *field_types)
if field_types.empty?
field_types = [TextField, TextareaField, CheckboxField, RadioField, HiddenField]
end
forms.each do |form| forms.each do |form|
result = form.find_field_by_name(name, *field_types) result = form.field_by_name(name, *field_types)
return result if result return result if result
end end