From 3e3d8e3659333dfc15effb6fcfe093e501ca4d84 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sat, 17 Jan 2009 13:27:32 -0500 Subject: [PATCH] Raise a Webrat::NotFoundError if the scope passed to #within doesn't exist [#90 state:resolved] --- History.txt | 1 + lib/webrat/core/scope.rb | 10 +++++++++- spec/public/within_spec.rb | 12 ++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/History.txt b/History.txt index 03e9ad5..ce024d0 100644 --- a/History.txt +++ b/History.txt @@ -65,6 +65,7 @@ * Bug fixes + * Raise a Webrat::NotFoundError if the scope passed to #within doesn't exist [#90] * Match against link _text_ which decodes character references. Useful for multibyte languages like Japanese (moronatural@gmail.com) * Fix params hash generation for Mechanize when Merb is not defined [#62] diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index df92c5a..093a391 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -30,6 +30,10 @@ module Webrat def initialize(session, &block) #:nodoc: @session = session instance_eval(&block) if block_given? + + if @selector && scoped_element.nil? + raise Webrat::NotFoundError.new("The scope was not found on the page: #{@selector.inspect}") + end end # Verifies an input field or textarea exists on the current page, and stores a value for @@ -291,7 +295,7 @@ module Webrat end def scoped_dom #:nodoc: - source = Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + source = Webrat::XML.to_html(scoped_element) if @session.xml_content_type? Webrat::XML.xml_document(source) @@ -300,6 +304,10 @@ module Webrat end end + def scoped_element + Webrat::XML.css_at(@scope.dom, @selector) + end + def locate_field(field_locator, *field_types) #:nodoc: if field_locator.is_a?(Field) field_locator diff --git a/spec/public/within_spec.rb b/spec/public/within_spec.rb index ec8bfee..90db20e 100644 --- a/spec/public/within_spec.rb +++ b/spec/public/within_spec.rb @@ -97,4 +97,16 @@ describe "within" do }.should raise_error(Webrat::NotFoundError) end end + + it "should raise a Webrat::NotFounderror error when the scope doesn't exist" do + with_html <<-HTML + + + HTML + + lambda { + within "#form2" do + end + }.should raise_error(Webrat::NotFoundError) + end end