Skip to content

Commit

Permalink
Changing field_named and field_with_id to use XPath
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 25, 2008
1 parent 6a096dd commit 4a6c6fb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/webrat/core/form.rb
Expand Up @@ -18,8 +18,8 @@ def field(locator, *field_types)
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)
Expand Down
38 changes: 14 additions & 24 deletions lib/webrat/core/locators.rb
Expand Up @@ -3,52 +3,42 @@
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 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
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_with_id(id, *field_types) #:nodoc:
def find_field_labeled(label, *field_types) #:nodoc:
forms.detect_mapped do |form|
form.field_with_id(id, *field_types)
form.field_labeled(label, *field_types)
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/webrat/core/scope.rb
Expand Up @@ -132,7 +132,7 @@ def select_date(date_to_select, options ={})
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
Expand Down Expand Up @@ -166,7 +166,7 @@ def select_time(time_to_select, options ={})
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
Expand All @@ -189,7 +189,7 @@ def select_time(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)

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
Expand Down

0 comments on commit 4a6c6fb

Please sign in to comment.