Revert "Changing field_named and field_with_id to use XPath"

This reverts commit 4a6c6fb2fc.

Conflicts:

	lib/webrat/core/form.rb
This commit is contained in:
Bryan Helmkamp 2008-11-26 21:37:56 -05:00
parent 8770ccb563
commit 8a4a7a328f
2 changed files with 27 additions and 17 deletions

View File

@ -3,45 +3,55 @@ require "webrat/core_extensions/detect_mapped"
module Webrat module Webrat
module Locators module Locators
def field_by_xpath(xpath, *field_types) def field_by_xpath(xpath)
element = dom.at(xpath) element = dom.at(xpath)
return nil unless element
forms.detect_mapped do |form| forms.detect_mapped do |form|
form.field_by_element(element, *field_types) form.field_by_element(element)
end end
end end
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}"))
end
def field_labeled(label, *field_types) def field_labeled(label, *field_types)
find_field_labeled(label, *field_types) || find_field_labeled(label, *field_types) ||
raise(NotFoundError.new("Could not find field labeled #{label.inspect}")) raise(NotFoundError.new("Could not find field labeled #{label.inspect}"))
end end
def field_named(name, *field_types) def field_named(name, *field_types)
field_by_xpath("//*[@name='#{id}']", *field_types) || find_field_named(name, *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)
field_by_xpath("//*[@id='#{id}']", *field_types) || find_field_with_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

@ -134,7 +134,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 = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:year]}')]") year_field = find_field_with_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
@ -168,7 +168,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 = field_by_xpath("//*[contains(@id, '_#{DATE_TIME_SUFFIXES[:hour]}')]") hour_field = find_field_with_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
@ -190,7 +190,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] ? field_by_xpath("//*[@id='#{options[:from]}']") : nil) options[:id_prefix] ||= (options[:from] ? find_field_with_id(options[:from]) : nil)
select_date time, options select_date time, options
select_time time, options select_time time, options