Switching #within to use a stack of scopes

This commit is contained in:
Bryan Helmkamp 2008-11-06 22:19:02 -05:00
parent cff0c1b74f
commit d54ae99e27
2 changed files with 22 additions and 10 deletions

View File

@ -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

View File

@ -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
</form>
EOS
@session.within "#form2" do |scope|
@session.within "#form2" do
lambda {
scope.click_button
@session.click_button
}.should raise_error
end
end