Skip to content

Commit

Permalink
Raise Webrat::NotFoundErrors instead of RuntimeErrors to make error c…
Browse files Browse the repository at this point in the history
…atching easier
  • Loading branch information
brynary committed Nov 19, 2008
1 parent df9d817 commit 10d5d76
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
1 change: 0 additions & 1 deletion lib/webrat/core.rb
Expand Up @@ -2,7 +2,6 @@
require "webrat/core/xml"
require "webrat/core/nokogiri"
require "webrat/core/logging"
require "webrat/core/flunk"
require "webrat/core/form"
require "webrat/core/scope"
require "webrat/core/link"
Expand Down
7 changes: 0 additions & 7 deletions lib/webrat/core/flunk.rb

This file was deleted.

18 changes: 9 additions & 9 deletions lib/webrat/core/locators.rb
Expand Up @@ -8,22 +8,22 @@ def field(*args)
find_field_with_id(*args) ||
find_field_named(*args) ||
field_labeled(*args) ||
flunk("Could not find field: #{args.inspect}")
raise(NotFoundError.new("Could not find field: #{args.inspect}"))
end

def field_labeled(label, *field_types)
find_field_labeled(label, *field_types) ||
flunk("Could not find field labeled #{label.inspect}")
raise(NotFoundError.new("Could not find field labeled #{label.inspect}"))
end

def field_named(name, *field_types)
find_field_named(name, *field_types) ||
flunk("Could not find field named #{name.inspect}")
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) ||
flunk("Could not find field with id #{id.inspect}")
raise(NotFoundError.new("Could not find field with id #{id.inspect}"))
end

def find_field_labeled(label, *field_types) #:nodoc:
Expand Down Expand Up @@ -56,7 +56,7 @@ def find_select_option(option_text, id_or_name_or_label) #:nodoc:
return select_option if select_option
end

flunk("Could not find option #{option_text.inspect}")
raise NotFoundError.new("Could not find option #{option_text.inspect}")
end

def find_button(value) #:nodoc:
Expand All @@ -67,13 +67,13 @@ def find_button(value) #:nodoc:
if button
return button
else
flunk("Could not find button #{value.inspect}")
raise NotFoundError.new("Could not find button #{value.inspect}")
end
end

def find_area(area_name) #:nodoc:
areas.detect { |area| area.matches_text?(area_name) } ||
flunk("Could not find area with name #{area_name}")
raise(NotFoundError.new("Could not find area with name #{area_name}"))
end

def find_link(text_or_title_or_id) #:nodoc:
Expand All @@ -84,7 +84,7 @@ def find_link(text_or_title_or_id) #:nodoc:
if matching_links.any?
matching_links.min { |a, b| a.text.length <=> b.text.length }
else
flunk("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

Expand All @@ -93,7 +93,7 @@ def find_field_id_for_label(label_text)
if label
label.for_id
else
flunk("Could not find the label with text #{label_text}")
raise NotFoundError.new("Could not find the label with text #{label_text}")
end
end

Expand Down
10 changes: 6 additions & 4 deletions lib/webrat/core/scope.rb
Expand Up @@ -2,9 +2,11 @@
require "webrat/core/locators"

module Webrat
class NotFoundError < WebratError
end

class Scope
include Logging
include Flunk
include Locators

def self.from_page(session, response, response_body) #:nodoc:
Expand Down Expand Up @@ -96,7 +98,7 @@ def selects(option_text, options = {})
option.choose
else
select_box_text = options[:from] ? " in the '#{options[:from]}' select box" : ''
flunk("The '#{option_text}' option was not found#{select_box_text}")
raise NotFoundError.new("The '#{option_text}' option was not found#{select_box_text}")
end
end

Expand Down Expand Up @@ -131,7 +133,7 @@ def selects_date(date_to_select, options ={})

id_prefix = locate_id_prefix(options) do
year_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:year]}$/)
flunk("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
end

Expand Down Expand Up @@ -165,7 +167,7 @@ def selects_time(time_to_select, options ={})

id_prefix = locate_id_prefix(options) do
hour_field = find_field_with_id(/(.*?)_#{DATE_TIME_SUFFIXES[:hour]}$/)
flunk("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
end

Expand Down

0 comments on commit 10d5d76

Please sign in to comment.