From 4a6c6fb2fcea7e8de5cf4ecaa6e85f18d2b2ef78 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Mon, 24 Nov 2008 21:59:56 -0500 Subject: [PATCH] Changing field_named and field_with_id to use XPath --- lib/webrat/core/form.rb | 4 ++-- lib/webrat/core/locators.rb | 38 ++++++++++++++----------------------- lib/webrat/core/scope.rb | 6 +++--- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index 66b4918..1a9eea4 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -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) diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 3e4b96a..73602a0 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -3,55 +3,45 @@ 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) + return nil unless element + forms.detect_mapped do |form| - form.field_by_element(element) + form.field_by_element(element, *field_types) 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) find_field_labeled(label, *field_types) || raise(NotFoundError.new("Could not find field labeled #{label.inspect}")) 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) diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 5df67ee..6b995b2 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -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