From 1647d6ec1ee6e53a6da963648a4e19f329f78885 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 29 Nov 2008 23:03:17 -0500 Subject: [PATCH] Locator refactoring --- lib/webrat/core/locators.rb | 79 +++++-------------- lib/webrat/core/locators/area_locator.rb | 2 +- lib/webrat/core/locators/button_locator.rb | 2 +- .../core/locators/field_by_id_locator.rb | 2 +- .../core/locators/field_named_locator.rb | 2 +- lib/webrat/core/locators/link_locator.rb | 2 +- lib/webrat/core/scope.rb | 17 +++- 7 files changed, 39 insertions(+), 67 deletions(-) diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 0b52fa7..0501bd9 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -1,71 +1,50 @@ 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 Locators 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 def field(*args) # :nodoc: # This is the default locator strategy - find_field_with_id(*args) || - find_field_named(*args) || - field_labeled(*args) || + FieldByIdLocator.new(self, args.first).locate || + FieldNamedLocator.new(self, *args).locate || + FieldLabeledLocator.new(self, *args).locate || raise(NotFoundError.new("Could not find field: #{args.inspect}")) end 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}")) end 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}")) end 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}")) 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: - require "webrat/core/locators/select_option_locator" - option = SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate return option if option @@ -78,35 +57,19 @@ module Webrat end def find_button(value) #:nodoc: - require "webrat/core/locators/button_locator" - ButtonLocator.new(self, value).locate || raise(NotFoundError.new("Could not find button #{value.inspect}")) end def find_area(id_or_title) #:nodoc: - require "webrat/core/locators/area_locator" - AreaLocator.new(self, id_or_title).locate || raise(NotFoundError.new("Could not find area with name #{id_or_title}")) end def find_link(text_or_title_or_id) #:nodoc: - require "webrat/core/locators/link_locator" - 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}")) 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 diff --git a/lib/webrat/core/locators/area_locator.rb b/lib/webrat/core/locators/area_locator.rb index 35f3992..b4973e5 100644 --- a/lib/webrat/core/locators/area_locator.rb +++ b/lib/webrat/core/locators/area_locator.rb @@ -6,7 +6,7 @@ module Webrat class AreaLocator < Locator def locate - @scope.area_by_element(area_element) + @scope.element_to_webrat_element(area_element) end def area_element diff --git a/lib/webrat/core/locators/button_locator.rb b/lib/webrat/core/locators/button_locator.rb index 80aa7f0..74c236f 100644 --- a/lib/webrat/core/locators/button_locator.rb +++ b/lib/webrat/core/locators/button_locator.rb @@ -6,7 +6,7 @@ module Webrat class ButtonLocator < Locator def locate - @scope.field_by_element(button_element) + @scope.element_to_webrat_element(button_element) end def button_element diff --git a/lib/webrat/core/locators/field_by_id_locator.rb b/lib/webrat/core/locators/field_by_id_locator.rb index f30794c..d71b3eb 100644 --- a/lib/webrat/core/locators/field_by_id_locator.rb +++ b/lib/webrat/core/locators/field_by_id_locator.rb @@ -6,7 +6,7 @@ module Webrat class FieldByIdLocator < Locator def locate - @scope.field_by_element(field_element) + @scope.element_to_webrat_element(field_element) end def field_element diff --git a/lib/webrat/core/locators/field_named_locator.rb b/lib/webrat/core/locators/field_named_locator.rb index 1e26819..c13a8cd 100644 --- a/lib/webrat/core/locators/field_named_locator.rb +++ b/lib/webrat/core/locators/field_named_locator.rb @@ -6,7 +6,7 @@ module Webrat class FieldNamedLocator < Locator def locate - @scope.field_by_element(field_element) + @scope.element_to_webrat_element(field_element) end def field_element diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb index 6bbd1ff..d75c2aa 100644 --- a/lib/webrat/core/locators/link_locator.rb +++ b/lib/webrat/core/locators/link_locator.rb @@ -6,7 +6,7 @@ module Webrat class LinkLocator < Locator def locate - @scope.link_by_element(link_element) + @scope.element_to_webrat_element(link_element) end def link_element diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index a49c2a7..2129e90 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -134,7 +134,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 = FieldByIdLocator.new(self, /(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/).locate raise NotFoundError.new("No date fields were found") unless year_field && year_field.id =~ /(.*?)_1i/ $1 end @@ -168,7 +168,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 = FieldByIdLocator.new(self, /(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/).locate raise NotFoundError.new("No time fields were found") unless hour_field && hour_field.id =~ /(.*?)_4i/ $1 end @@ -190,7 +190,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] ? FieldByIdLocator.new(self, options[:from]).locate : nil) select_date time, options select_time time, options @@ -302,7 +302,16 @@ module Webrat def locate_id_prefix(options, &location_strategy) #:nodoc: 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 def areas #:nodoc: