Refactoring Page into Scope

This commit is contained in:
Bryan Helmkamp 2008-07-25 20:00:12 -04:00
parent 8405c6cfb0
commit 4f843d3d01
3 changed files with 97 additions and 81 deletions

View File

@ -15,7 +15,7 @@ module Webrat
@method = method @method = method
@data = data @data = data
reset_dom reset_scope
load_page if @url load_page if @url
session.current_page = self session.current_page = self
@ -32,7 +32,7 @@ module Webrat
# <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>) # <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
# or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field. # or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
def fills_in(id_or_name_or_label, options = {}) def fills_in(id_or_name_or_label, options = {})
field = find_field(id_or_name_or_label, TextField, TextareaField, PasswordField) field = scope.find_field(id_or_name_or_label, TextField, TextareaField, PasswordField)
field.set(options[:with]) field.set(options[:with])
end end
@ -44,7 +44,7 @@ module Webrat
# Example: # Example:
# checks 'Remember Me' # checks 'Remember Me'
def checks(id_or_name_or_label) def checks(id_or_name_or_label)
field = find_field(id_or_name_or_label, CheckboxField) field = scope.find_field(id_or_name_or_label, CheckboxField)
field.check field.check
end end
@ -56,7 +56,7 @@ module Webrat
# Example: # Example:
# unchecks 'Remember Me' # unchecks 'Remember Me'
def unchecks(id_or_name_or_label) def unchecks(id_or_name_or_label)
field = find_field(id_or_name_or_label, CheckboxField) field = scope.find_field(id_or_name_or_label, CheckboxField)
field.uncheck field.uncheck
end end
@ -68,7 +68,7 @@ module Webrat
# Example: # Example:
# chooses 'First Option' # chooses 'First Option'
def chooses(label) def chooses(label)
field = find_field(label, RadioField) field = scope.find_field(label, RadioField)
field.choose field.choose
end end
@ -87,10 +87,10 @@ module Webrat
id_or_name_or_label = options[:from] id_or_name_or_label = options[:from]
if id_or_name_or_label if id_or_name_or_label
field = find_field(id_or_name_or_label, SelectField) field = scope.find_field(id_or_name_or_label, SelectField)
option = field.find_option(option_text) option = field.find_option(option_text)
else else
option = find_select_option(option_text) option = scope.find_select_option(option_text)
end end
flunk("Could not find option #{option_text.inspect}") if option.nil? flunk("Could not find option #{option_text.inspect}") if option.nil?
@ -107,7 +107,7 @@ module Webrat
# attaches_file "Resume", "/path/to/the/resume.txt" # attaches_file "Resume", "/path/to/the/resume.txt"
# attaches_file "Photo", "/path/to/the/image.png", "image/png" # attaches_file "Photo", "/path/to/the/image.png", "image/png"
def attaches_file(id_or_name_or_label, path, content_type = nil) def attaches_file(id_or_name_or_label, path, content_type = nil)
field = find_field(id_or_name_or_label, FileField) field = scope.find_field(id_or_name_or_label, FileField)
field.set(path, content_type) field.set(path, content_type)
end end
@ -148,7 +148,7 @@ module Webrat
# #
# clicks_link "Sign up", :javascript => false # clicks_link "Sign up", :javascript => false
def clicks_link(link_text, options = {}) def clicks_link(link_text, options = {})
link = find_link(link_text) link = scope.find_link(link_text)
link.click(nil, options) link.click(nil, options)
end end
@ -159,7 +159,7 @@ module Webrat
# Example: # Example:
# clicks_link_within "#user_12", "Vote" # clicks_link_within "#user_12", "Vote"
def clicks_link_within(selector, link_text) def clicks_link_within(selector, link_text)
link = find_link(link_text, selector) link = scope.find_link(link_text, selector)
link.click link.click
end end
@ -170,7 +170,7 @@ module Webrat
# Example: # Example:
# clicks_get_link "Log out" # clicks_get_link "Log out"
def clicks_get_link(link_text) def clicks_get_link(link_text)
link = find_link(link_text) link = scope.find_link(link_text)
link.click(:get) link.click(:get)
end end
@ -181,7 +181,7 @@ module Webrat
# Example: # Example:
# clicks_delete_link "Log out" # clicks_delete_link "Log out"
def clicks_delete_link(link_text) def clicks_delete_link(link_text)
link = find_link(link_text) link = scope.find_link(link_text)
link.click(:delete) link.click(:delete)
end end
@ -192,7 +192,7 @@ module Webrat
# Example: # Example:
# clicks_post_link "Vote" # clicks_post_link "Vote"
def clicks_post_link(link_text) def clicks_post_link(link_text)
link = find_link(link_text) link = scope.find_link(link_text)
link.click(:post) link.click(:post)
end end
@ -203,7 +203,7 @@ module Webrat
# Example: # Example:
# clicks_put_link "Update profile" # clicks_put_link "Update profile"
def clicks_put_link(link_text) def clicks_put_link(link_text)
link = find_link(link_text) link = scope.find_link(link_text)
link.click(:put) link.click(:put)
end end
@ -221,7 +221,7 @@ module Webrat
def clicks_button(value = nil) def clicks_button(value = nil)
button = nil button = nil
forms.each do |form| scope.forms.each do |form|
button = form.find_button(value) button = form.find_button(value)
break if button break if button
end end
@ -251,78 +251,20 @@ module Webrat
protected protected
def load_page def load_page
request_page(@url, @method, @data) session.request_page(@url, @method, @data)
save_and_open if session.exception_caught?
flunk("Page load was not successful (Code: #{session.response_code.inspect})") unless session.success_code?
reset_scope
end end
def find_select_option(option_text) def reset_scope
forms.each do |form| @scope = nil
result = form.find_select_option(option_text)
return result if result
end end
nil def scope
end @scope ||= Scope.new(self, session.response_body)
def find_link(text, selector = nil)
matching_links = []
links_within(selector).each do |possible_link|
matching_links << possible_link if possible_link.matches_text?(text)
end
if matching_links.any?
matching_links.sort_by { |l| l.text.length }.first
else
flunk("Could not find link with text #{text.inspect}")
end
end
def find_field(id_or_name_or_label, *field_types)
forms.each do |form|
result = form.find_field(id_or_name_or_label, *field_types)
return result if result
end
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
end
def request_page(url, method, data)
debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}"
session.send "#{method}", url, data || {}
if session.response_body =~ /Exception caught/
save_and_open
end
flunk("Page load was not successful (Code: #{session.response_code.inspect})") unless (200..299).include?(session.response_code)
reset_dom
end
def reset_dom
@dom = nil
@forms = nil
end
def links_within(selector)
(dom / selector / "a[@href]").map do |link_element|
Link.new(self, link_element)
end
end
def forms
return @forms if @forms
@forms = (dom / "form").map do |form_element|
Form.new(self, form_element)
end
end
def dom # :nodoc:
return @dom if defined?(@dom) && @dom
flunk("You must visit a path before working with the page.") unless @session.response_code
@dom = Hpricot(@session.response_body)
end end
def flunk(message) def flunk(message)

61
lib/webrat/core/scope.rb Normal file
View File

@ -0,0 +1,61 @@
module Webrat
class Scope
def initialize(page, html)
@page = page
@html = html
end
def find_select_option(option_text)
forms.each do |form|
result = form.find_select_option(option_text)
return result if result
end
nil
end
def find_link(text, selector = nil)
matching_links = []
links_within(selector).each do |possible_link|
matching_links << possible_link if possible_link.matches_text?(text)
end
if matching_links.any?
matching_links.sort_by { |l| l.text.length }.first
else
flunk("Could not find link with text #{text.inspect}")
end
end
def find_field(id_or_name_or_label, *field_types)
forms.each do |form|
result = form.find_field(id_or_name_or_label, *field_types)
return result if result
end
flunk("Could not find #{field_types.inspect}: #{id_or_name_or_label.inspect}")
end
def links_within(selector)
(dom / selector / "a[@href]").map do |link_element|
Link.new(@page, link_element)
end
end
def forms
return @forms if @forms
@forms = (dom / "form").map do |form_element|
Form.new(@page, form_element)
end
end
def dom # :nodoc:
return @dom if defined?(@dom) && @dom
@dom = Hpricot(@html)
end
end
end

View File

@ -9,6 +9,19 @@ module Webrat
File.expand_path(".") File.expand_path(".")
end end
def request_page(url, method, data)
debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}"
send "#{method}", url, data || {}
end
def success_code?
(200..299).include?(response_code)
end
def exception_caught?
response_body =~ /Exception caught/
end
def current_page def current_page
@current_page ||= Page.new(self) @current_page ||= Page.new(self)
end end