Skip to content

Commit

Permalink
More work on integrating locator strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 6, 2008
1 parent da7017a commit 4d3e7f7
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/webrat/core/field.rb
Expand Up @@ -173,7 +173,7 @@ def to_param
if collection_name?
super
else
checkbox_with_same_name = @form.find_field(name, CheckboxField)
checkbox_with_same_name = @form.field(name, CheckboxField)

if checkbox_with_same_name.to_param.blank?
super
Expand Down
14 changes: 7 additions & 7 deletions lib/webrat/core/form.rb
Expand Up @@ -11,10 +11,10 @@ def initialize(session, element)
@fields = nil
end

def find_field(locator, *field_types)
field_by_id(locator, *field_types) ||
field_by_name(locator, *field_types) ||
field_by_label(locator, *field_types) ||
def field(locator, *field_types)
field_with_id(locator, *field_types) ||
field_named(locator, *field_types) ||
field_labeled(locator, *field_types) ||
nil
end

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

def field_by_id(id, *field_types)
def field_with_id(id, *field_types)
possible_fields = fields_by_type(field_types)
possible_fields.detect { |possible_field| possible_field.matches_id?(id) }
end

def field_by_name(name, *field_types)
def field_named(name, *field_types)
possible_fields = fields_by_type(field_types)
possible_fields.detect { |possible_field| possible_field.matches_name?(name) }
end

def field_by_label(label, *field_types)
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)
Expand Down
80 changes: 47 additions & 33 deletions lib/webrat/core/locators.rb
@@ -1,69 +1,83 @@
require "webrat/core_extensions/detect_mapped"

module Webrat
module Locators

def find_field(*args)
def field(*args)
# This is the default locator strategy

field_with_id(*args) ||
field_with_name(*args) ||
field_labeled(*args)
find_field_with_id(*args) ||
find_field_named(*args) ||
field_labeled(*args) ||
flunk("Could not find field: #{args.inspect}")
end

def field_labeled(label, *field_types)
forms.each do |form|
result = form.field_by_label(label, *field_types)
return result if result
end

flunk("Could not find #{field_types.inspect}: #{label.inspect}")
find_field_labeled(label, *field_types) ||
flunk("Could not find field labeled #{label.inspect}")
end

def field_named(name, *field_types)
find_field_named(name, *field_types) ||
flunk("Could not find field named #{name.inspect}")
end

def field_with_id(id, *field_types)
forms.each do |form|
result = form.field_by_id(id, *field_types)
return result if result
find_field_with_id(id, *field_types) ||
flunk("Could not find field with id #{id.inspect}")
end

def find_field_labeled(label, *field_types)
forms.detect_mapped do |form|
form.field_labeled(label, *field_types)
end

return nil
end

def field_with_name(name, *field_types)
forms.each do |form|
result = form.field_by_name(name, *field_types)
return result if result
def find_field_named(name, *field_types)
forms.detect_mapped do |form|
form.field_named(name, *field_types)
end
end

def find_field_with_id(id, *field_types)
forms.detect_mapped do |form|
form.field_with_id(id, *field_types)
end

return nil
end

def find_select_option(option_text, id_or_name_or_label)
if id_or_name_or_label
field = find_field(id_or_name_or_label, SelectField)
field = field(id_or_name_or_label, SelectField)
return field.find_option(option_text)
else
forms.each do |form|
result = form.find_select_option(option_text)
return result if result
select_option = forms.detect_mapped do |form|
form.find_select_option(option_text)
end

return select_option if select_option
end

flunk("Could not find option #{option_text.inspect}")
end

def find_button(value)
forms.each do |form|
button = form.find_button(value)
return button if button
button = forms.detect_mapped do |form|
form.find_button(value)
end

if button
return button
else
flunk("Could not find button #{value.inspect}")
end
flunk("Could not find button #{value.inspect}")
end

def find_area(area_name)
areas.select{|area| area.matches_text?(area_name)}.first || flunk("Could not find area with name #{area_name}")
areas.detect { |area| area.matches_text?(area_name) } ||
flunk("Could not find area with name #{area_name}")
end

def find_link(text, selector = nil)
matching_links = links_within(selector).select do |possible_link|
def find_link(text)
matching_links = links.select do |possible_link|
possible_link.matches_text?(text)
end

Expand Down
32 changes: 20 additions & 12 deletions lib/webrat/core/scope.rb
Expand Up @@ -26,8 +26,8 @@ def initialize(session, html, selector = nil)
# The field value is required, and must be specified in <tt>options[:with]</tt>.
# <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
# or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
def fill_in(id_or_name_or_label, options = {})
field = find_field(id_or_name_or_label, TextField, TextareaField, PasswordField)
def fill_in(field_locator, options = {})
field = locate_field(field_locator, TextField, TextareaField, PasswordField)
field.raise_error_if_disabled
field.set(options[:with])
end
Expand All @@ -39,8 +39,8 @@ def fill_in(id_or_name_or_label, options = {})
#
# Example:
# check 'Remember Me'
def check(id_or_name_or_label)
find_field(id_or_name_or_label, CheckboxField).check
def check(field_locator)
locate_field(field_locator, CheckboxField).check
end

alias_method :checks, :check
Expand All @@ -50,8 +50,8 @@ def check(id_or_name_or_label)
#
# Example:
# uncheck 'Remember Me'
def uncheck(id_or_name_or_label)
find_field(id_or_name_or_label, CheckboxField).uncheck
def uncheck(field_locator)
locate_field(field_locator, CheckboxField).uncheck
end

alias_method :unchecks, :uncheck
Expand All @@ -61,8 +61,8 @@ def uncheck(id_or_name_or_label)
#
# Example:
# choose 'First Option'
def choose(label)
find_field(label, RadioField).choose
def choose(field_locator)
locate_field(field_locator, RadioField).choose
end

alias_method :chooses, :choose
Expand All @@ -89,8 +89,8 @@ def selects(option_text, options = {})
# Example:
# attaches_file "Resume", "/path/to/the/resume.txt"
# attaches_file "Photo", "/path/to/the/image.png", "image/png"
def attaches_file(id_or_name_or_label, path, content_type = nil)
find_field(id_or_name_or_label, FileField).set(path, content_type)
def attaches_file(field_locator, path, content_type = nil)
locate_field(field_locator, FileField).set(path, content_type)
end

alias_method :attach_file, :attaches_file
Expand Down Expand Up @@ -146,6 +146,14 @@ def dom # :nodoc:

protected

def locate_field(field_locator, *field_types)
if field_locator.is_a?(Field)
field_locator
else
field(field_locator, *field_types)
end
end

def scoped_html
@scoped_html ||= begin
if @selector
Expand All @@ -162,8 +170,8 @@ def areas
end
end

def links_within(selector)
(dom / selector / "a[@href]").map do |link_element|
def links
(dom / "a[@href]").map do |link_element|
Link.new(@session, link_element)
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/webrat/core_extensions/detect_mapped.rb
@@ -0,0 +1,12 @@
class Array

def detect_mapped
each do |element|
result = yield element
return result if result
end

return nil
end

end

0 comments on commit 4d3e7f7

Please sign in to comment.