From d54ae99e2717dc3be2ee90a60e1d79e9e50268b5 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Thu, 6 Nov 2008 22:19:02 -0500 Subject: [PATCH] Switching #within to use a stack of scopes --- lib/webrat/core/session.rb | 18 +++++++++++++++--- spec/api/within_spec.rb | 14 +++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index b1a89ea..f126233 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -87,7 +87,8 @@ module Webrat save_and_open_page if exception_caught? flunk("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? - @scope = nil + @_scopes = nil + @_page_scope = nil @current_url = url @http_method = http_method @data = data @@ -104,7 +105,7 @@ module Webrat end def current_scope - @scope ||= Scope.new(self, response_body) + scopes.last || page_scope end # Reloads the last page requested. Note that this will resubmit forms @@ -132,7 +133,10 @@ module Webrat alias_method :clicks_link_within, :click_link_within def within(selector) - yield Scope.new(self, response_body, selector) + scopes.push(Scope.new(self, response_body, selector)) + ret = yield(current_scope) + scopes.pop + return ret end # Issues a GET request for a page, follows any redirects, and verifies the final page @@ -159,6 +163,14 @@ module Webrat def formatted_error response_body end + + def scopes + @_scopes ||= [] + end + + def page_scope + @_page_scope ||= Scope.new(self, response_body) + end def_delegators :current_scope, :fill_in, :fills_in def_delegators :current_scope, :check, :checks diff --git a/spec/api/within_spec.rb b/spec/api/within_spec.rb index c80ba8e..d9db9dc 100644 --- a/spec/api/within_spec.rb +++ b/spec/api/within_spec.rb @@ -14,8 +14,8 @@ describe "within" do EOS @session.should_receive(:get).with("/page2", {}) - @session.within "#container" do |scope| - scope.click_link "Link" + @session.within "#container" do + @session.click_link "Link" end end @@ -32,9 +32,9 @@ describe "within" do EOS @session.should_receive(:get).with("/form2", "email" => "test@example.com") - @session.within "#form2" do |scope| - scope.fill_in "Email", :with => "test@example.com" - scope.click_button + @session.within "#form2" do + @session.fill_in "Email", :with => "test@example.com" + @session.click_button end end @@ -47,9 +47,9 @@ describe "within" do EOS - @session.within "#form2" do |scope| + @session.within "#form2" do lambda { - scope.click_button + @session.click_button }.should raise_error end end