From 87a4ff91ca1a7c193933d0f947b7f774ae11cbc0 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 29 Nov 2008 23:14:43 -0500 Subject: [PATCH] Adding Locator#locate! method for error raising --- lib/webrat/core/locators.rb | 6 ------ lib/webrat/core/locators/area_locator.rb | 7 +++++-- lib/webrat/core/locators/button_locator.rb | 7 +++++-- .../core/locators/field_by_id_locator.rb | 9 ++++++--- .../core/locators/field_labeled_locator.rb | 7 +++++-- lib/webrat/core/locators/field_locator.rb | 10 +++++++++- .../core/locators/field_named_locator.rb | 7 +++++-- lib/webrat/core/locators/link_locator.rb | 7 +++++-- lib/webrat/core/locators/locator.rb | 4 ++++ .../core/locators/select_option_locator.rb | 18 +++++++++--------- 10 files changed, 53 insertions(+), 29 deletions(-) diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index e8510f3..16bab18 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -22,11 +22,5 @@ module Webrat @session.elements[Webrat::XML.xpath_to(element)] end - def field(*args) # :nodoc: - # This is the default locator strategy - FieldLocator.new(self, *args).locate || - raise(NotFoundError.new("Could not find field: #{args.inspect}")) - end - end end diff --git a/lib/webrat/core/locators/area_locator.rb b/lib/webrat/core/locators/area_locator.rb index 6e71758..fe464d5 100644 --- a/lib/webrat/core/locators/area_locator.rb +++ b/lib/webrat/core/locators/area_locator.rb @@ -24,11 +24,14 @@ module Webrat Webrat::XML.css_search(@scope.dom, "area") end + def error_message + "Could not find area with name #{@value}" + end + end def find_area(id_or_title) #:nodoc: - AreaLocator.new(self, id_or_title).locate || - raise(NotFoundError.new("Could not find area with name #{id_or_title}")) + AreaLocator.new(self, id_or_title).locate! end end diff --git a/lib/webrat/core/locators/button_locator.rb b/lib/webrat/core/locators/button_locator.rb index 2bcaac6..1f594fa 100644 --- a/lib/webrat/core/locators/button_locator.rb +++ b/lib/webrat/core/locators/button_locator.rb @@ -40,11 +40,14 @@ module Webrat Webrat::XML.xpath_search(@scope.dom, *ButtonField.xpath_search) end + def error_message + "Could not find button #{@value.inspect}" + end + end def find_button(value) #:nodoc: - ButtonLocator.new(self, value).locate || - raise(NotFoundError.new("Could not find button #{value.inspect}")) + ButtonLocator.new(self, value).locate! end end diff --git a/lib/webrat/core/locators/field_by_id_locator.rb b/lib/webrat/core/locators/field_by_id_locator.rb index c254589..b5cb647 100644 --- a/lib/webrat/core/locators/field_by_id_locator.rb +++ b/lib/webrat/core/locators/field_by_id_locator.rb @@ -22,12 +22,15 @@ module Webrat def field_elements Webrat::XML.xpath_search(@scope.dom, *Field.xpath_search) end - + + def error_message + "Could not find field with id #{@value.inspect}" + end + end def field_with_id(id, *field_types) - FieldByIdLocator.new(self, id, *field_types).locate || - raise(NotFoundError.new("Could not find field with id #{id.inspect}")) + FieldByIdLocator.new(self, id, *field_types).locate! end end diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index 369ea0f..738e096 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -12,11 +12,14 @@ module Webrat end end + def error_message + "Could not find field labeled #{@value.inspect}" + end + end def field_labeled(label, *field_types) - FieldLabeledLocator.new(self, label, *field_types).locate || - raise(NotFoundError.new("Could not find field labeled #{label.inspect}")) + FieldLabeledLocator.new(self, label, *field_types).locate! end end diff --git a/lib/webrat/core/locators/field_locator.rb b/lib/webrat/core/locators/field_locator.rb index cd4fa16..651de61 100644 --- a/lib/webrat/core/locators/field_locator.rb +++ b/lib/webrat/core/locators/field_locator.rb @@ -10,7 +10,15 @@ module Webrat FieldNamedLocator.new(@scope, @value, *@field_types).locate || FieldLabeledLocator.new(@scope, @value, *@field_types).locate end - + + def error_message + "Could not find field: #{@value.inspect}" + end + + end + + def field(*args) # :nodoc: + FieldLocator.new(self, *args).locate! end end diff --git a/lib/webrat/core/locators/field_named_locator.rb b/lib/webrat/core/locators/field_named_locator.rb index 95394b5..7ef19c4 100644 --- a/lib/webrat/core/locators/field_named_locator.rb +++ b/lib/webrat/core/locators/field_named_locator.rb @@ -27,11 +27,14 @@ module Webrat end end + def error_message + "Could not find field named #{@value.inspect}" + end + end def field_named(name, *field_types) - FieldNamedLocator.new(self, name, *field_types).locate || - raise(NotFoundError.new("Could not find field named #{name.inspect}")) + FieldNamedLocator.new(self, name, *field_types).locate! end end diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb index dee2bf4..b8ee687 100644 --- a/lib/webrat/core/locators/link_locator.rb +++ b/lib/webrat/core/locators/link_locator.rb @@ -52,11 +52,14 @@ module Webrat str.gsub(' ',' ').gsub(' ', ' ') end + def error_message + "Could not find link with text or title or id #{@value.inspect}" + end + end def find_link(text_or_title_or_id) #:nodoc: - 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}")) + LinkLocator.new(self, text_or_title_or_id).locate! end end diff --git a/lib/webrat/core/locators/locator.rb b/lib/webrat/core/locators/locator.rb index 84a78bd..a91a750 100644 --- a/lib/webrat/core/locators/locator.rb +++ b/lib/webrat/core/locators/locator.rb @@ -9,6 +9,10 @@ module Webrat @field_types = field_types end + def locate! + locate || raise(NotFoundError.new(error_message)) + end + end end diff --git a/lib/webrat/core/locators/select_option_locator.rb b/lib/webrat/core/locators/select_option_locator.rb index 768617f..63fdfc4 100644 --- a/lib/webrat/core/locators/select_option_locator.rb +++ b/lib/webrat/core/locators/select_option_locator.rb @@ -22,19 +22,19 @@ module Webrat end end end + + def error_message + if @id_or_name_or_label + "The '#{@option_text}' option was not found in the #{@id_or_name_or_label.inspect} select box" + else + "Could not find option #{@option_text.inspect}" + end + end end def find_select_option(option_text, id_or_name_or_label) #:nodoc: - option = SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate - return option if option - - if id_or_name_or_label - select_box_text = " in the #{id_or_name_or_label.inspect} select box" - raise NotFoundError.new("The '#{option_text}' option was not found#{select_box_text}") - else - raise NotFoundError.new("Could not find option #{option_text.inspect}") - end + SelectOptionLocator.new(self, option_text, id_or_name_or_label).locate! end end