Changing field_named and field_with_id to use XPath

This commit is contained in:
Bryan Helmkamp 2008-11-24 21:59:56 -05:00
parent 6a096dd434
commit 4a6c6fb2fc
3 changed files with 19 additions and 29 deletions

View File

@ -18,8 +18,8 @@ module Webrat
nil
end
def field_by_element(element)
fields.detect { |possible_field| possible_field.path == element.path }
def field_by_element(element, *field_types)
fields_by_type(field_types).detect { |possible_field| possible_field.path == element.path }
end
def find_select_option(option_text)

View File

@ -3,20 +3,14 @@ require "webrat/core_extensions/detect_mapped"
module Webrat
module Locators
def field_by_xpath(xpath)
def field_by_xpath(xpath, *field_types)
element = dom.at(xpath)
forms.detect_mapped do |form|
form.field_by_element(element)
end
end
return nil unless element
def field(*args) # :nodoc:
# This is the default locator strategy
find_field_with_id(*args) ||
find_field_named(*args) ||
field_labeled(*args) ||
raise(NotFoundError.new("Could not find field: #{args.inspect}"))
forms.detect_mapped do |form|
form.field_by_element(element, *field_types)
end
end
def field_labeled(label, *field_types)
@ -25,33 +19,29 @@ module Webrat
end
def field_named(name, *field_types)
find_field_named(name, *field_types) ||
field_by_xpath("//*[@name='#{id}']", *field_types) ||
raise(NotFoundError.new("Could not find field named #{name.inspect}"))
end
def field_with_id(id, *field_types)
find_field_with_id(id, *field_types) ||
field_by_xpath("//*[@id='#{id}']", *field_types) ||
raise(NotFoundError.new("Could not find field with id #{id.inspect}"))
end
def field(id, *field_types) # :nodoc:
# This is the default locator strategy
field_by_xpath("//*[@id='#{id}']", *field_types) ||
field_by_xpath("//*[@name='#{id}']", *field_types) ||
field_labeled(id, *field_types) ||
raise(NotFoundError.new("Could not find field: #{args.inspect}"))
end
def find_field_labeled(label, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_labeled(label, *field_types)
end
end
def find_field_named(name, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_named(name, *field_types)
end
end
def find_field_with_id(id, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_with_id(id, *field_types)
end
end
def find_select_option(option_text, id_or_name_or_label) #:nodoc:
if id_or_name_or_label
field = field(id_or_name_or_label, SelectField)

View File

@ -132,7 +132,7 @@ module Webrat
date_to_select : Date.parse(date_to_select)
id_prefix = locate_id_prefix(options) do
year_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/)
year_field = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:year]}')]")
raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/
$1
end
@ -166,7 +166,7 @@ module Webrat
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
id_prefix = locate_id_prefix(options) do
hour_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/)
hour_field = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:hour]}')]")
raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/
$1
end
@ -189,7 +189,7 @@ module Webrat
def select_datetime(time_to_select, options ={})
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
options[:id_prefix] ||= (options[:from] ? find_field_with_id(options[:from]) : nil)
options[:id_prefix] ||= (options[:from] ? field_by_xpath("//*[@id='#{options[:from]}']") : nil)
select_date time, options
select_time time, options