Skip to content

Commit

Permalink
Starting to split up locator strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 6, 2008
1 parent 39e1b98 commit 979b84b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
21 changes: 11 additions & 10 deletions lib/webrat/core/form.rb
Expand Up @@ -12,11 +12,9 @@ def initialize(session, element)
end

def find_field(id_or_name_or_label, *field_types)
possible_fields = fields_by_type(field_types)

find_field_by_id(possible_fields, id_or_name_or_label) ||
find_field_by_name(possible_fields, id_or_name_or_label) ||
find_field_by_label(possible_fields, id_or_name_or_label) ||
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_label(id_or_name_or_label, *field_types) ||
nil
end

Expand Down Expand Up @@ -50,22 +48,25 @@ def submit
@session.request_page(form_action, form_method, params)
end

protected

def find_field_by_id(possible_fields, id)
def find_field_by_id(id, *field_types)
possible_fields = fields_by_type(field_types)
possible_fields.detect { |possible_field| possible_field.matches_id?(id) }
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) }
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|
possible_field.matches_label?(label)
end
matching_fields.min { |a, b| a.label_text.length <=> b.label_text.length }
end

protected

def fields_by_type(field_types)
fields.select { |f| field_types.include?(f.class) }
Expand Down
56 changes: 45 additions & 11 deletions lib/webrat/core/locators.rb
@@ -1,8 +1,51 @@
module Webrat
module Locators

def find_field(*args)
# 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_labeled(label)
find_field(label, TextField, TextareaField, CheckboxField, RadioField, HiddenField)
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

def find_select_option(option_text, id_or_name_or_label)
Expand Down Expand Up @@ -43,14 +86,5 @@ def find_link(text, selector = nil)
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

0 comments on commit 979b84b

Please sign in to comment.