From 1d39fe71d4086170a63a774d2e0029c6266c7165 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Sun, 10 Aug 2008 15:24:02 -0400 Subject: [PATCH] Refactoring away from Page object --- lib/webrat/core/form.rb | 8 ++++---- lib/webrat/core/link.rb | 12 ++++++------ lib/webrat/core/page.rb | 39 ++++++++++--------------------------- lib/webrat/core/scope.rb | 10 +++++----- lib/webrat/core/session.rb | 14 ++++++------- lib/webrat/rails/session.rb | 2 +- 6 files changed, 33 insertions(+), 52 deletions(-) diff --git a/lib/webrat/core/form.rb b/lib/webrat/core/form.rb index edeb0ca..9be4ece 100644 --- a/lib/webrat/core/form.rb +++ b/lib/webrat/core/form.rb @@ -2,8 +2,8 @@ module Webrat class Form attr_reader :element - def initialize(page, element) - @page = page + def initialize(session, element) + @session = session @element = element @fields = nil end @@ -53,7 +53,7 @@ module Webrat end def submit - @page.session.request_page(form_action, form_method, params) + @session.request_page(form_action, form_method, params) end protected @@ -104,7 +104,7 @@ module Webrat end def form_action - @element["action"].blank? ? @page.url : @element["action"] + @element["action"].blank? ? @session.current_url : @element["action"] end def merge(all_params, new_param) diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 581334f..b12d4dd 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -1,8 +1,8 @@ module Webrat class Link - def initialize(page, element) - @page = page + def initialize(session, element) + @session = session @element = element end @@ -13,9 +13,9 @@ module Webrat options[:javascript] = true if options[:javascript].nil? if options[:javascript] - @page.session.request_page(absolute_href, method, data) + @session.request_page(absolute_href, method, data) else - @page.session.request_page(absolute_href, :get, {}) + @session.request_page(absolute_href, :get, {}) end end @@ -47,9 +47,9 @@ module Webrat if href =~ %r{^https?://www.example.com(/.*)} $LAST_MATCH_INFO.captures.first elsif href =~ /^\?/ - "#{@page.url}#{href}" + "#{@session.current_url}#{href}" elsif href !~ /^\// - "#{@page.url}/#{href}" + "#{@session.current_url}/#{href}" else href end diff --git a/lib/webrat/core/page.rb b/lib/webrat/core/page.rb index 7c2bceb..9f2683e 100644 --- a/lib/webrat/core/page.rb +++ b/lib/webrat/core/page.rb @@ -9,42 +9,23 @@ module Webrat include Logging include Flunk - attr_reader :session attr_reader :url + attr_reader :data + attr_reader :http_method def initialize(session, url = nil, method = :get, data = {}) - @session = session - @url = url - @method = method - @data = data - - reset_scope - load_page if @url + @session = session + @url = url + @http_method = method + @data = data - session.current_page = self - end - - def http_method - @method - end - - def data - @data + session.request_page(@url, @http_method, @data) if @url + + @scope = nil end def scope - @scope ||= Scope.new(self, session.response_body) - end - - protected - - def load_page - session.request_page(@url, @method, @data) - reset_scope - end - - def reset_scope - @scope = nil + @scope ||= Scope.new(@session, @session.response_body) end end diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index e26e270..c0fec28 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -3,9 +3,9 @@ module Webrat include Logging include Flunk - def initialize(page, html, selector = nil) - @page = page - @html = html + def initialize(session, html, selector = nil) + @session = session + @html = html @selector = selector end @@ -210,7 +210,7 @@ module Webrat def links_within(selector) (dom / selector / "a[@href]").map do |link_element| - Link.new(@page, link_element) + Link.new(@session, link_element) end end @@ -218,7 +218,7 @@ module Webrat return @forms if @forms @forms = (dom / "form").map do |form_element| - Form.new(@page, form_element) + Form.new(@session, form_element) end end diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index e182372..6e02440 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -18,7 +18,11 @@ module Webrat end open_in_browser(filename) - end + end + + def current_url + @current_page.url + end def doc_root nil @@ -52,10 +56,6 @@ module Webrat current_page.scope end - def current_page=(new_page) - @current_page = new_page - end - # Reloads the last page requested. Note that this will resubmit forms # and their data. # @@ -81,11 +81,11 @@ module Webrat alias_method :click_link_within, :clicks_link_within def within(selector) - yield Scope.new(current_page, response_body, selector) + yield Scope.new(self, response_body, selector) end def visits(*args) - Page.new(self, *args) + @current_page = Page.new(self, *args) end alias_method :visit, :visits diff --git a/lib/webrat/rails/session.rb b/lib/webrat/rails/session.rb index 4990efd..2b924e5 100644 --- a/lib/webrat/rails/session.rb +++ b/lib/webrat/rails/session.rb @@ -12,7 +12,7 @@ module ActionController # Example: # visits "/" def visits(*args) - Webrat::Page.new(webrat_session, *args) + @webrat_session.visits(*args) end def respond_to?(name)