Refactoring away from Page object

This commit is contained in:
Bryan Helmkamp 2008-08-10 15:24:02 -04:00
parent 9fd8a9ea82
commit 1d39fe71d4
6 changed files with 33 additions and 52 deletions

View File

@ -2,8 +2,8 @@ module Webrat
class Form class Form
attr_reader :element attr_reader :element
def initialize(page, element) def initialize(session, element)
@page = page @session = session
@element = element @element = element
@fields = nil @fields = nil
end end
@ -53,7 +53,7 @@ module Webrat
end end
def submit def submit
@page.session.request_page(form_action, form_method, params) @session.request_page(form_action, form_method, params)
end end
protected protected
@ -104,7 +104,7 @@ module Webrat
end end
def form_action def form_action
@element["action"].blank? ? @page.url : @element["action"] @element["action"].blank? ? @session.current_url : @element["action"]
end end
def merge(all_params, new_param) def merge(all_params, new_param)

View File

@ -1,8 +1,8 @@
module Webrat module Webrat
class Link class Link
def initialize(page, element) def initialize(session, element)
@page = page @session = session
@element = element @element = element
end end
@ -13,9 +13,9 @@ module Webrat
options[:javascript] = true if options[:javascript].nil? options[:javascript] = true if options[:javascript].nil?
if options[:javascript] if options[:javascript]
@page.session.request_page(absolute_href, method, data) @session.request_page(absolute_href, method, data)
else else
@page.session.request_page(absolute_href, :get, {}) @session.request_page(absolute_href, :get, {})
end end
end end
@ -47,9 +47,9 @@ module Webrat
if href =~ %r{^https?://www.example.com(/.*)} if href =~ %r{^https?://www.example.com(/.*)}
$LAST_MATCH_INFO.captures.first $LAST_MATCH_INFO.captures.first
elsif href =~ /^\?/ elsif href =~ /^\?/
"#{@page.url}#{href}" "#{@session.current_url}#{href}"
elsif href !~ /^\// elsif href !~ /^\//
"#{@page.url}/#{href}" "#{@session.current_url}/#{href}"
else else
href href
end end

View File

@ -9,42 +9,23 @@ module Webrat
include Logging include Logging
include Flunk include Flunk
attr_reader :session
attr_reader :url attr_reader :url
attr_reader :data
attr_reader :http_method
def initialize(session, url = nil, method = :get, data = {}) def initialize(session, url = nil, method = :get, data = {})
@session = session @session = session
@url = url @url = url
@method = method @http_method = method
@data = data @data = data
reset_scope session.request_page(@url, @http_method, @data) if @url
load_page if @url
session.current_page = self @scope = nil
end
def http_method
@method
end
def data
@data
end end
def scope def scope
@scope ||= Scope.new(self, session.response_body) @scope ||= Scope.new(@session, @session.response_body)
end
protected
def load_page
session.request_page(@url, @method, @data)
reset_scope
end
def reset_scope
@scope = nil
end end
end end

View File

@ -3,9 +3,9 @@ module Webrat
include Logging include Logging
include Flunk include Flunk
def initialize(page, html, selector = nil) def initialize(session, html, selector = nil)
@page = page @session = session
@html = html @html = html
@selector = selector @selector = selector
end end
@ -210,7 +210,7 @@ module Webrat
def links_within(selector) def links_within(selector)
(dom / selector / "a[@href]").map do |link_element| (dom / selector / "a[@href]").map do |link_element|
Link.new(@page, link_element) Link.new(@session, link_element)
end end
end end
@ -218,7 +218,7 @@ module Webrat
return @forms if @forms return @forms if @forms
@forms = (dom / "form").map do |form_element| @forms = (dom / "form").map do |form_element|
Form.new(@page, form_element) Form.new(@session, form_element)
end end
end end

View File

@ -20,6 +20,10 @@ module Webrat
open_in_browser(filename) open_in_browser(filename)
end end
def current_url
@current_page.url
end
def doc_root def doc_root
nil nil
end end
@ -52,10 +56,6 @@ module Webrat
current_page.scope current_page.scope
end end
def current_page=(new_page)
@current_page = new_page
end
# Reloads the last page requested. Note that this will resubmit forms # Reloads the last page requested. Note that this will resubmit forms
# and their data. # and their data.
# #
@ -81,11 +81,11 @@ module Webrat
alias_method :click_link_within, :clicks_link_within 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(self, response_body, selector)
end end
def visits(*args) def visits(*args)
Page.new(self, *args) @current_page = Page.new(self, *args)
end end
alias_method :visit, :visits alias_method :visit, :visits

View File

@ -12,7 +12,7 @@ module ActionController
# Example: # Example:
# visits "/" # visits "/"
def visits(*args) def visits(*args)
Webrat::Page.new(webrat_session, *args) @webrat_session.visits(*args)
end end
def respond_to?(name) def respond_to?(name)