Locator refactoring
This commit is contained in:
parent
4ae94af45c
commit
1647d6ec1e
@ -1,71 +1,50 @@
|
|||||||
require "webrat/core_extensions/detect_mapped"
|
require "webrat/core_extensions/detect_mapped"
|
||||||
|
|
||||||
|
require "webrat/core/locators/area_locator"
|
||||||
|
require "webrat/core/locators/button_locator"
|
||||||
|
require "webrat/core/locators/field_labeled_locator"
|
||||||
|
require "webrat/core/locators/label_locator"
|
||||||
|
require "webrat/core/locators/field_named_locator"
|
||||||
|
require "webrat/core/locators/field_by_id_locator"
|
||||||
|
require "webrat/core/locators/select_option_locator"
|
||||||
|
require "webrat/core/locators/link_locator"
|
||||||
|
|
||||||
module Webrat
|
module Webrat
|
||||||
module Locators
|
module Locators
|
||||||
|
|
||||||
def field_by_xpath(xpath)
|
def field_by_xpath(xpath)
|
||||||
field_by_element(Webrat::XML.xpath_at(dom, xpath))
|
element_to_webrat_element(Webrat::XML.xpath_at(dom, xpath))
|
||||||
|
end
|
||||||
|
|
||||||
|
def element_to_webrat_element(element)
|
||||||
|
return nil if element.nil?
|
||||||
|
@session.elements[Webrat::XML.xpath_to(element)]
|
||||||
end
|
end
|
||||||
|
|
||||||
def field(*args) # :nodoc:
|
def field(*args) # :nodoc:
|
||||||
# This is the default locator strategy
|
# This is the default locator strategy
|
||||||
find_field_with_id(*args) ||
|
FieldByIdLocator.new(self, args.first).locate ||
|
||||||
find_field_named(*args) ||
|
FieldNamedLocator.new(self, *args).locate ||
|
||||||
field_labeled(*args) ||
|
FieldLabeledLocator.new(self, *args).locate ||
|
||||||
raise(NotFoundError.new("Could not find field: #{args.inspect}"))
|
raise(NotFoundError.new("Could not find field: #{args.inspect}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_labeled(label, *field_types)
|
def field_labeled(label, *field_types)
|
||||||
find_field_labeled(label, *field_types) ||
|
FieldLabeledLocator.new(self, label, *field_types).locate ||
|
||||||
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)
|
||||||
find_field_named(name, *field_types) ||
|
FieldNamedLocator.new(self, name, *field_types).locate ||
|
||||||
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) ||
|
FieldByIdLocator.new(self, id, *field_types).locate ||
|
||||||
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 find_field_labeled(label, *field_types) #:nodoc:
|
|
||||||
require "webrat/core/locators/field_labeled_locator"
|
|
||||||
|
|
||||||
FieldLabeledLocator.new(self, label, *field_types).locate
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_field_named(name, *field_types) #:nodoc:
|
|
||||||
require "webrat/core/locators/field_named_locator"
|
|
||||||
|
|
||||||
FieldNamedLocator.new(self, name, *field_types).locate
|
|
||||||
end
|
|
||||||
|
|
||||||
def field_by_element(element)
|
|
||||||
return nil if element.nil?
|
|
||||||
@session.elements[Webrat::XML.xpath_to(element)]
|
|
||||||
end
|
|
||||||
|
|
||||||
def area_by_element(element)
|
|
||||||
return nil if element.nil?
|
|
||||||
@session.elements[Webrat::XML.xpath_to(element)]
|
|
||||||
end
|
|
||||||
|
|
||||||
def link_by_element(element)
|
|
||||||
return nil if element.nil?
|
|
||||||
@session.elements[Webrat::XML.xpath_to(element)]
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_field_with_id(id, *field_types) #:nodoc:
|
|
||||||
require "webrat/core/locators/field_by_id_locator"
|
|
||||||
|
|
||||||
FieldByIdLocator.new(self, id).locate
|
|
||||||
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:
|
||||||
require "webrat/core/locators/select_option_locator"
|
|
||||||
|
|
||||||
option = SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate
|
option = SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate
|
||||||
return option if option
|
return option if option
|
||||||
|
|
||||||
@ -78,35 +57,19 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def find_button(value) #:nodoc:
|
def find_button(value) #:nodoc:
|
||||||
require "webrat/core/locators/button_locator"
|
|
||||||
|
|
||||||
ButtonLocator.new(self, value).locate ||
|
ButtonLocator.new(self, value).locate ||
|
||||||
raise(NotFoundError.new("Could not find button #{value.inspect}"))
|
raise(NotFoundError.new("Could not find button #{value.inspect}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_area(id_or_title) #:nodoc:
|
def find_area(id_or_title) #:nodoc:
|
||||||
require "webrat/core/locators/area_locator"
|
|
||||||
|
|
||||||
AreaLocator.new(self, id_or_title).locate ||
|
AreaLocator.new(self, id_or_title).locate ||
|
||||||
raise(NotFoundError.new("Could not find area with name #{id_or_title}"))
|
raise(NotFoundError.new("Could not find area with name #{id_or_title}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_link(text_or_title_or_id) #:nodoc:
|
def find_link(text_or_title_or_id) #:nodoc:
|
||||||
require "webrat/core/locators/link_locator"
|
|
||||||
|
|
||||||
LinkLocator.new(self, text_or_title_or_id).locate ||
|
LinkLocator.new(self, text_or_title_or_id).locate ||
|
||||||
raise(NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}"))
|
raise(NotFoundError.new("Could not find link with text or title or id #{text_or_title_or_id.inspect}"))
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_field_id_for_label(label_text) #:nodoc:
|
|
||||||
require "webrat/core/locators/label_locator"
|
|
||||||
|
|
||||||
if (label = LabelLocator.new(self, label_text).locate)
|
|
||||||
label.for_id
|
|
||||||
else
|
|
||||||
raise NotFoundError.new("Could not find the label with text #{label_text}")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6,7 +6,7 @@ module Webrat
|
|||||||
class AreaLocator < Locator
|
class AreaLocator < Locator
|
||||||
|
|
||||||
def locate
|
def locate
|
||||||
@scope.area_by_element(area_element)
|
@scope.element_to_webrat_element(area_element)
|
||||||
end
|
end
|
||||||
|
|
||||||
def area_element
|
def area_element
|
||||||
|
@ -6,7 +6,7 @@ module Webrat
|
|||||||
class ButtonLocator < Locator
|
class ButtonLocator < Locator
|
||||||
|
|
||||||
def locate
|
def locate
|
||||||
@scope.field_by_element(button_element)
|
@scope.element_to_webrat_element(button_element)
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_element
|
def button_element
|
||||||
|
@ -6,7 +6,7 @@ module Webrat
|
|||||||
class FieldByIdLocator < Locator
|
class FieldByIdLocator < Locator
|
||||||
|
|
||||||
def locate
|
def locate
|
||||||
@scope.field_by_element(field_element)
|
@scope.element_to_webrat_element(field_element)
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_element
|
def field_element
|
||||||
|
@ -6,7 +6,7 @@ module Webrat
|
|||||||
class FieldNamedLocator < Locator
|
class FieldNamedLocator < Locator
|
||||||
|
|
||||||
def locate
|
def locate
|
||||||
@scope.field_by_element(field_element)
|
@scope.element_to_webrat_element(field_element)
|
||||||
end
|
end
|
||||||
|
|
||||||
def field_element
|
def field_element
|
||||||
|
@ -6,7 +6,7 @@ module Webrat
|
|||||||
class LinkLocator < Locator
|
class LinkLocator < Locator
|
||||||
|
|
||||||
def locate
|
def locate
|
||||||
@scope.link_by_element(link_element)
|
@scope.element_to_webrat_element(link_element)
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_element
|
def link_element
|
||||||
|
@ -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 = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/)
|
year_field = FieldByIdLocator.new(self, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate
|
||||||
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 = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/)
|
hour_field = FieldByIdLocator.new(self, /(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/).locate
|
||||||
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] ? find_field_with_id(options[:from]) : nil)
|
options[:id_prefix] ||= (options[:from] ? FieldByIdLocator.new(self, options[:from]).locate : nil)
|
||||||
|
|
||||||
select_date time, options
|
select_date time, options
|
||||||
select_time time, options
|
select_time time, options
|
||||||
@ -302,7 +302,16 @@ module Webrat
|
|||||||
|
|
||||||
def locate_id_prefix(options, &location_strategy) #:nodoc:
|
def locate_id_prefix(options, &location_strategy) #:nodoc:
|
||||||
return options[:id_prefix] if options[:id_prefix]
|
return options[:id_prefix] if options[:id_prefix]
|
||||||
id_prefix = options[:from] ? find_field_id_for_label(options[:from]) : yield
|
|
||||||
|
if options[:from]
|
||||||
|
if (label = LabelLocator.new(self, options[:from]).locate)
|
||||||
|
label.for_id
|
||||||
|
else
|
||||||
|
raise NotFoundError.new("Could not find the label with text #{options[:from]}")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
yield
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def areas #:nodoc:
|
def areas #:nodoc:
|
||||||
|
Loading…
Reference in New Issue
Block a user