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? save_and_open_page if exception_caught?
flunk("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? 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 @current_url = url
@http_method = http_method @http_method = http_method
@data = data @data = data
@ -104,7 +105,7 @@ module Webrat
end end
def current_scope def current_scope
@scope ||= Scope.new(self, response_body) scopes.last || page_scope
end end
# Reloads the last page requested. Note that this will resubmit forms # 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 alias_method :clicks_link_within, :click_link_within
def within(selector) 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 end
# Issues a GET request for a page, follows any redirects, and verifies the final page # Issues a GET request for a page, follows any redirects, and verifies the final page
@ -160,6 +164,14 @@ module Webrat
response_body response_body
end 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, :fill_in, :fills_in
def_delegators :current_scope, :check, :checks def_delegators :current_scope, :check, :checks
def_delegators :current_scope, :uncheck, :unchecks def_delegators :current_scope, :uncheck, :unchecks

View File

@ -14,8 +14,8 @@ describe "within" do
EOS EOS
@session.should_receive(:get).with("/page2", {}) @session.should_receive(:get).with("/page2", {})
@session.within "#container" do |scope| @session.within "#container" do
scope.click_link "Link" @session.click_link "Link"
end end
end end
@ -32,9 +32,9 @@ describe "within" do
EOS EOS
@session.should_receive(:get).with("/form2", "email" => "test@example.com") @session.should_receive(:get).with("/form2", "email" => "test@example.com")
@session.within "#form2" do |scope| @session.within "#form2" do
scope.fill_in "Email", :with => "test@example.com" @session.fill_in "Email", :with => "test@example.com"
scope.click_button @session.click_button
end end
end end
@ -47,9 +47,9 @@ describe "within" do
</form> </form>
EOS EOS
@session.within "#form2" do |scope| @session.within "#form2" do
lambda { lambda {
scope.click_button @session.click_button
}.should raise_error }.should raise_error
end end
end end