Delegate from Session to Scope instead of Page

This commit is contained in:
Bryan Helmkamp 2008-07-28 09:59:22 -04:00
parent cccc8a34f8
commit 9979a29524
3 changed files with 39 additions and 23 deletions

View File

@ -24,28 +24,13 @@ module Webrat
session.current_page = self session.current_page = self
end end
# Reloads the last page requested. Note that this will resubmit forms def http_method
# and their data. @method
#
# Example:
# reloads
def reloads
load_page
end end
alias_method :reload, :reloads
# Works like clicks_link, but only looks for the link text within a given selector def data
# @data
# Example:
# clicks_link_within "#user_12", "Vote"
def clicks_link_within(selector, link_text)
session.within(selector) do |scope|
scope.clicks_link(link_text)
end
end end
alias_method :click_link_within, :clicks_link_within
def_delegators :scope, :fill_in, :fills_in def_delegators :scope, :fill_in, :fills_in
def_delegators :scope, :check, :checks def_delegators :scope, :check, :checks
@ -59,6 +44,10 @@ module Webrat
def_delegators :scope, :click_post_link, :clicks_post_link def_delegators :scope, :click_post_link, :clicks_post_link
def_delegators :scope, :click_put_link, :clicks_put_link def_delegators :scope, :click_put_link, :clicks_put_link
def_delegators :scope, :click_button, :clicks_button def_delegators :scope, :click_button, :clicks_button
def scope
@scope ||= Scope.new(self, session.response_body)
end
protected protected
@ -71,9 +60,7 @@ module Webrat
@scope = nil @scope = nil
end end
def scope
@scope ||= Scope.new(self, session.response_body)
end
end end
end end

View File

@ -1,5 +1,6 @@
module Webrat module Webrat
class Scope class Scope
include Logging
include Flunk include Flunk
def initialize(page, html, selector = nil) def initialize(page, html, selector = nil)

View File

@ -46,10 +46,38 @@ module Webrat
@current_page ||= Page.new(self) @current_page ||= Page.new(self)
end end
def current_scope
current_page.scope
end
def current_page=(new_page) def current_page=(new_page)
@current_page = new_page @current_page = new_page
end end
# Reloads the last page requested. Note that this will resubmit forms
# and their data.
#
# Example:
# reloads
def reloads
request_page(@current_page.url, current_page.http_method, current_page.data)
end
alias_method :reload, :reloads
# Works like clicks_link, but only looks for the link text within a given selector
#
# Example:
# clicks_link_within "#user_12", "Vote"
def clicks_link_within(selector, link_text)
within(selector) do |scope|
scope.clicks_link(link_text)
end
end
alias_method :click_link_within, :clicks_link_within
def within(selector) def within(selector)
yield Scope.new(current_page, response_body, selector) yield Scope.new(current_page, response_body, selector)
end end
@ -75,7 +103,7 @@ module Webrat
def method_missing(name, *args, &block) def method_missing(name, *args, &block)
if current_page.respond_to?(name) if current_page.respond_to?(name)
current_page.send(name, *args, &block) current_scope.send(name, *args, &block)
else else
super super
end end