Refactoring away from Page object
This commit is contained in:
parent
9fd8a9ea82
commit
1d39fe71d4
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
@http_method = method
|
||||
@data = data
|
||||
|
||||
reset_scope
|
||||
load_page if @url
|
||||
session.request_page(@url, @http_method, @data) if @url
|
||||
|
||||
session.current_page = self
|
||||
end
|
||||
|
||||
def http_method
|
||||
@method
|
||||
end
|
||||
|
||||
def data
|
||||
@data
|
||||
@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
|
||||
|
@ -3,8 +3,8 @@ module Webrat
|
||||
include Logging
|
||||
include Flunk
|
||||
|
||||
def initialize(page, html, selector = nil)
|
||||
@page = page
|
||||
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
|
||||
|
||||
|
@ -20,6 +20,10 @@ module Webrat
|
||||
open_in_browser(filename)
|
||||
end
|
||||
|
||||
def current_url
|
||||
@current_page.url
|
||||
end
|
||||
|
||||
def doc_root
|
||||
nil
|
||||
end
|
||||
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user