Class: Webrat::Session
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_url ⇒ Object
readonly
Returns the value of attribute current_url.
-
#elements ⇒ Object
readonly
Returns the value of attribute elements.
Instance Method Summary collapse
- #automate ⇒ Object
- #basic_auth(user, pass) ⇒ Object
-
#click_link_within(selector, link_text) ⇒ Object
Works like click_link, but only looks for the link text within a given selector.
-
#current_dom ⇒ Object
:nodoc:.
-
#current_page ⇒ Object
For backwards compatibility – removing in 1.0.
-
#current_scope ⇒ Object
:nodoc:.
-
#doc_root ⇒ Object
:nodoc:.
- #dom ⇒ Object
-
#exception_caught? ⇒ Boolean
:nodoc:.
-
#formatted_error ⇒ Object
Subclasses can override this to show error messages without html.
- #header(key, value) ⇒ Object
-
#headers ⇒ Object
:nodoc:.
- #http_accept(mime_type) ⇒ Object
-
#initialize(context = nil) ⇒ Session
constructor
:nodoc:.
-
#open_in_browser(path) ⇒ Object
:nodoc.
-
#page_scope ⇒ Object
:nodoc:.
-
#reloads ⇒ Object
Reloads the last page requested.
-
#request_page(url, http_method, data) ⇒ Object
:nodoc:.
-
#rewrite_css_and_image_references(response_html) ⇒ Object
:nodoc:.
-
#save_and_open_page ⇒ Object
Saves the page out to RAILS_ROOT/tmp/ and opens it in the default web browser if on OS X.
-
#saved_page_dir ⇒ Object
:nodoc:.
-
#scopes ⇒ Object
:nodoc:.
- #simulate ⇒ Object
-
#success_code? ⇒ Boolean
:nodoc:.
-
#visit(url = nil, http_method = :get, data = {}) ⇒ Object
Issues a GET request for a page, follows any redirects, and verifies the final page load was successful.
- #within(selector) ⇒ Object
- #xml_content_type? ⇒ Boolean
Methods included from Logging
Constructor Details
#initialize(context = nil) ⇒ Session
:nodoc:
37 38 39 40 41 42 43 44 45 |
# File 'lib/webrat/core/session.rb', line 37 def initialize(context = nil) #:nodoc: @http_method = :get @data = {} @default_headers = {} @custom_headers = {} @context = context reset end |
Instance Attribute Details
#current_url ⇒ Object (readonly)
Returns the value of attribute current_url.
34 35 36 |
# File 'lib/webrat/core/session.rb', line 34 def current_url @current_url end |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
35 36 37 |
# File 'lib/webrat/core/session.rb', line 35 def elements @elements end |
Instance Method Details
#automate ⇒ Object
216 217 218 219 |
# File 'lib/webrat/core/session.rb', line 216 def automate return unless Webrat.configuration.mode == :selenium yield end |
#basic_auth(user, pass) ⇒ Object
93 94 95 96 |
# File 'lib/webrat/core/session.rb', line 93 def basic_auth(user, pass) encoded_login = ["#{user}:#{pass}"].pack("m*") header('HTTP_AUTHORIZATION', "Basic #{encoded_login}") end |
#click_link_within(selector, link_text) ⇒ Object
Works like click_link, but only looks for the link text within a given selector
Example:
click_link_within "#user_12", "Vote"
150 151 152 153 154 |
# File 'lib/webrat/core/session.rb', line 150 def click_link_within(selector, link_text) within(selector) do click_link(link_text) end end |
#current_dom ⇒ Object
:nodoc:
64 65 66 |
# File 'lib/webrat/core/session.rb', line 64 def current_dom #:nodoc: current_scope.dom end |
#current_page ⇒ Object
For backwards compatibility – removing in 1.0
69 70 71 72 73 74 75 |
# File 'lib/webrat/core/session.rb', line 69 def current_page #:nodoc: page = OpenStruct.new page.url = @current_url page.http_method = @http_method page.data = @data page end |
#current_scope ⇒ Object
:nodoc:
133 134 135 |
# File 'lib/webrat/core/session.rb', line 133 def current_scope #:nodoc: scopes.last || page_scope end |
#doc_root ⇒ Object
:nodoc:
77 78 79 |
# File 'lib/webrat/core/session.rb', line 77 def doc_root #:nodoc: nil end |
#dom ⇒ Object
203 204 205 |
# File 'lib/webrat/core/session.rb', line 203 def dom page_scope.dom end |
#exception_caught? ⇒ Boolean
:nodoc:
129 130 131 |
# File 'lib/webrat/core/session.rb', line 129 def exception_caught? #:nodoc: response_body =~ /Exception caught/ end |
#formatted_error ⇒ Object
Subclasses can override this to show error messages without html
191 192 193 |
# File 'lib/webrat/core/session.rb', line 191 def formatted_error #:nodoc: response_body end |
#header(key, value) ⇒ Object
85 86 87 |
# File 'lib/webrat/core/session.rb', line 85 def header(key, value) @custom_headers[key] = value end |
#headers ⇒ Object
:nodoc:
98 99 100 |
# File 'lib/webrat/core/session.rb', line 98 def headers #:nodoc: @default_headers.dup.merge(@custom_headers.dup) end |
#http_accept(mime_type) ⇒ Object
89 90 91 |
# File 'lib/webrat/core/session.rb', line 89 def http_accept(mime_type) header('Accept', Webrat::MIME.mime_type(mime_type)) end |
#open_in_browser(path) ⇒ Object
:nodoc
176 177 178 179 180 181 182 183 |
# File 'lib/webrat/core/session.rb', line 176 def open_in_browser(path) # :nodoc platform = ruby_platform if platform =~ /cygwin/ || platform =~ /win32/ `rundll32 url.dll,FileProtocolHandler #{path.gsub("/", "\\\\")}` elsif platform =~ /darwin/ `open #{path}` end end |
#page_scope ⇒ Object
:nodoc:
199 200 201 |
# File 'lib/webrat/core/session.rb', line 199 def page_scope #:nodoc: @_page_scope ||= Scope.from_page(self, response, response_body) end |
#reloads ⇒ Object
Reloads the last page requested. Note that this will resubmit forms and their data.
139 140 141 |
# File 'lib/webrat/core/session.rb', line 139 def reloads request_page(@current_url, @http_method, @data) end |
#request_page(url, http_method, data) ⇒ Object
:nodoc:
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/webrat/core/session.rb', line 102 def request_page(url, http_method, data) #:nodoc: h = headers h['HTTP_REFERER'] = @current_url if @current_url debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}" if h.empty? send "#{http_method}", url, data || {} else send "#{http_method}", url, data || {}, h end save_and_open_page if exception_caught? && Webrat.configuration.open_error_files? raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code? reset @current_url = url @http_method = http_method @data = data return response end |
#rewrite_css_and_image_references(response_html) ⇒ Object
:nodoc:
185 186 187 188 |
# File 'lib/webrat/core/session.rb', line 185 def rewrite_css_and_image_references(response_html) # :nodoc: return response_html unless doc_root response_html.gsub(/"\/(stylesheets|images)/, doc_root + '/\1') end |
#save_and_open_page ⇒ Object
Saves the page out to RAILS_ROOT/tmp/ and opens it in the default web browser if on OS X. Useful for debugging.
Example:
save_and_open_page
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/webrat/core/session.rb', line 52 def save_and_open_page return unless File.exist?(saved_page_dir) filename = "#{saved_page_dir}/webrat-#{Time.now.to_i}.html" File.open(filename, "w") do |f| f.write rewrite_css_and_image_references(response_body) end open_in_browser(filename) end |
#saved_page_dir ⇒ Object
:nodoc:
81 82 83 |
# File 'lib/webrat/core/session.rb', line 81 def saved_page_dir #:nodoc: File.(".") end |
#scopes ⇒ Object
:nodoc:
195 196 197 |
# File 'lib/webrat/core/session.rb', line 195 def scopes #:nodoc: @_scopes ||= [] end |
#simulate ⇒ Object
211 212 213 214 |
# File 'lib/webrat/core/session.rb', line 211 def simulate return if Webrat.configuration.mode == :selenium yield end |
#success_code? ⇒ Boolean
:nodoc:
125 126 127 |
# File 'lib/webrat/core/session.rb', line 125 def success_code? #:nodoc: (200..499).include?(response_code) end |
#visit(url = nil, http_method = :get, data = {}) ⇒ Object
Issues a GET request for a page, follows any redirects, and verifies the final page load was successful.
Example:
visit "/"
170 171 172 |
# File 'lib/webrat/core/session.rb', line 170 def visit(url = nil, http_method = :get, data = {}) request_page(url, http_method, data) end |
#within(selector) ⇒ Object
158 159 160 161 162 163 |
# File 'lib/webrat/core/session.rb', line 158 def within(selector) scopes.push(Scope.from_scope(self, current_scope, selector)) ret = yield(current_scope) scopes.pop return ret end |
#xml_content_type? ⇒ Boolean
207 208 209 |
# File 'lib/webrat/core/session.rb', line 207 def xml_content_type? false end |