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 nil
end end
def field_by_element(element) def field_by_element(element, *field_types)
fields.detect { |possible_field| possible_field.path == element.path } fields_by_type(field_types).detect { |possible_field| possible_field.path == element.path }
end end
def find_select_option(option_text) def find_select_option(option_text)

View File

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

View File

@ -132,7 +132,7 @@ module Webrat
date_to_select : Date.parse(date_to_select) date_to_select : Date.parse(date_to_select)
id_prefix = locate_id_prefix(options) do 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/ raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/
$1 $1
end end
@ -166,7 +166,7 @@ module Webrat
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select)
id_prefix = locate_id_prefix(options) do 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/ raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/
$1 $1
end end
@ -189,7 +189,7 @@ module Webrat
def select_datetime(time_to_select, options ={}) def select_datetime(time_to_select, options ={})
time = time_to_select.is_a?(Time) ? time_to_select : Time.parse(time_to_select) 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_date time, options
select_time time, options select_time time, options