More work on integrating locator strategies
This commit is contained in:
parent
da7017acd6
commit
4d3e7f785b
@ -173,7 +173,7 @@ module Webrat
|
|||||||
if collection_name?
|
if collection_name?
|
||||||
super
|
super
|
||||||
else
|
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?
|
if checkbox_with_same_name.to_param.blank?
|
||||||
super
|
super
|
||||||
|
@ -11,10 +11,10 @@ module Webrat
|
|||||||
@fields = nil
|
@fields = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_field(locator, *field_types)
|
def field(locator, *field_types)
|
||||||
field_by_id(locator, *field_types) ||
|
field_with_id(locator, *field_types) ||
|
||||||
field_by_name(locator, *field_types) ||
|
field_named(locator, *field_types) ||
|
||||||
field_by_label(locator, *field_types) ||
|
field_labeled(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 field_by_id(id, *field_types)
|
def field_with_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 field_by_name(name, *field_types)
|
def field_named(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 field_by_label(label, *field_types)
|
def field_labeled(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)
|
||||||
|
@ -1,69 +1,83 @@
|
|||||||
|
require "webrat/core_extensions/detect_mapped"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
module Locators
|
module Locators
|
||||||
|
|
||||||
def find_field(*args)
|
def field(*args)
|
||||||
# This is the default locator strategy
|
# This is the default locator strategy
|
||||||
|
find_field_with_id(*args) ||
|
||||||
field_with_id(*args) ||
|
find_field_named(*args) ||
|
||||||
field_with_name(*args) ||
|
field_labeled(*args) ||
|
||||||
field_labeled(*args)
|
flunk("Could not find field: #{args.inspect}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_labeled(label, *field_types)
|
def field_labeled(label, *field_types)
|
||||||
forms.each do |form|
|
find_field_labeled(label, *field_types) ||
|
||||||
result = form.field_by_label(label, *field_types)
|
flunk("Could not find field labeled #{label.inspect}")
|
||||||
return result if result
|
end
|
||||||
end
|
|
||||||
|
def field_named(name, *field_types)
|
||||||
flunk("Could not find #{field_types.inspect}: #{label.inspect}")
|
find_field_named(name, *field_types) ||
|
||||||
|
flunk("Could not find field named #{name.inspect}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_with_id(id, *field_types)
|
def field_with_id(id, *field_types)
|
||||||
forms.each do |form|
|
find_field_with_id(id, *field_types) ||
|
||||||
result = form.field_by_id(id, *field_types)
|
flunk("Could not find field with id #{id.inspect}")
|
||||||
return result if result
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_with_name(name, *field_types)
|
def find_field_labeled(label, *field_types)
|
||||||
forms.each do |form|
|
forms.detect_mapped do |form|
|
||||||
result = form.field_by_name(name, *field_types)
|
form.field_labeled(label, *field_types)
|
||||||
return result if result
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
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)
|
||||||
if 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)
|
return field.find_option(option_text)
|
||||||
else
|
else
|
||||||
forms.each do |form|
|
select_option = forms.detect_mapped do |form|
|
||||||
result = form.find_select_option(option_text)
|
form.find_select_option(option_text)
|
||||||
return result if result
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return select_option if select_option
|
||||||
end
|
end
|
||||||
|
|
||||||
flunk("Could not find option #{option_text.inspect}")
|
flunk("Could not find option #{option_text.inspect}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_button(value)
|
def find_button(value)
|
||||||
forms.each do |form|
|
button = forms.detect_mapped do |form|
|
||||||
button = form.find_button(value)
|
form.find_button(value)
|
||||||
return button if button
|
end
|
||||||
|
|
||||||
|
if button
|
||||||
|
return button
|
||||||
|
else
|
||||||
|
flunk("Could not find button #{value.inspect}")
|
||||||
end
|
end
|
||||||
flunk("Could not find button #{value.inspect}")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_area(area_name)
|
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
|
end
|
||||||
|
|
||||||
def find_link(text, selector = nil)
|
def find_link(text)
|
||||||
matching_links = links_within(selector).select do |possible_link|
|
matching_links = links.select do |possible_link|
|
||||||
possible_link.matches_text?(text)
|
possible_link.matches_text?(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -26,8 +26,8 @@ module Webrat
|
|||||||
# The field value is required, and must be specified in <tt>options[:with]</tt>.
|
# 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>)
|
# <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.
|
# 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 = {})
|
def fill_in(field_locator, options = {})
|
||||||
field = find_field(id_or_name_or_label, TextField, TextareaField, PasswordField)
|
field = locate_field(field_locator, TextField, TextareaField, PasswordField)
|
||||||
field.raise_error_if_disabled
|
field.raise_error_if_disabled
|
||||||
field.set(options[:with])
|
field.set(options[:with])
|
||||||
end
|
end
|
||||||
@ -39,8 +39,8 @@ module Webrat
|
|||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# check 'Remember Me'
|
# check 'Remember Me'
|
||||||
def check(id_or_name_or_label)
|
def check(field_locator)
|
||||||
find_field(id_or_name_or_label, CheckboxField).check
|
locate_field(field_locator, CheckboxField).check
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :checks, :check
|
alias_method :checks, :check
|
||||||
@ -50,8 +50,8 @@ module Webrat
|
|||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# uncheck 'Remember Me'
|
# uncheck 'Remember Me'
|
||||||
def uncheck(id_or_name_or_label)
|
def uncheck(field_locator)
|
||||||
find_field(id_or_name_or_label, CheckboxField).uncheck
|
locate_field(field_locator, CheckboxField).uncheck
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :unchecks, :uncheck
|
alias_method :unchecks, :uncheck
|
||||||
@ -61,8 +61,8 @@ module Webrat
|
|||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# choose 'First Option'
|
# choose 'First Option'
|
||||||
def choose(label)
|
def choose(field_locator)
|
||||||
find_field(label, RadioField).choose
|
locate_field(field_locator, RadioField).choose
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :chooses, :choose
|
alias_method :chooses, :choose
|
||||||
@ -89,8 +89,8 @@ module Webrat
|
|||||||
# Example:
|
# Example:
|
||||||
# attaches_file "Resume", "/path/to/the/resume.txt"
|
# attaches_file "Resume", "/path/to/the/resume.txt"
|
||||||
# attaches_file "Photo", "/path/to/the/image.png", "image/png"
|
# attaches_file "Photo", "/path/to/the/image.png", "image/png"
|
||||||
def attaches_file(id_or_name_or_label, path, content_type = nil)
|
def attaches_file(field_locator, path, content_type = nil)
|
||||||
find_field(id_or_name_or_label, FileField).set(path, content_type)
|
locate_field(field_locator, FileField).set(path, content_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
alias_method :attach_file, :attaches_file
|
alias_method :attach_file, :attaches_file
|
||||||
@ -146,6 +146,14 @@ module Webrat
|
|||||||
|
|
||||||
protected
|
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
|
def scoped_html
|
||||||
@scoped_html ||= begin
|
@scoped_html ||= begin
|
||||||
if @selector
|
if @selector
|
||||||
@ -162,8 +170,8 @@ module Webrat
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def links_within(selector)
|
def links
|
||||||
(dom / selector / "a[@href]").map do |link_element|
|
(dom / "a[@href]").map do |link_element|
|
||||||
Link.new(@session, link_element)
|
Link.new(@session, link_element)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
12
lib/webrat/core_extensions/detect_mapped.rb
Normal file
12
lib/webrat/core_extensions/detect_mapped.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
class Array
|
||||||
|
|
||||||
|
def detect_mapped
|
||||||
|
each do |element|
|
||||||
|
result = yield element
|
||||||
|
return result if result
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user