Class: Webrat::Session
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#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
- #check_for_infinite_redirects ⇒ 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
- #infinite_redirect_limit_exceeded? ⇒ Boolean
-
#initialize(adapter = nil) ⇒ Session
constructor
A new instance of Session.
- #internal_redirect? ⇒ Boolean
-
#page_scope ⇒ Object
:nodoc:.
-
#redirect? ⇒ Boolean
:nodoc:.
-
#redirected_to ⇒ Object
easy helper to pull out where we were redirected to.
-
#reload ⇒ Object
Reloads the last page requested.
-
#request_page(url, http_method, data) ⇒ 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 SaveAndOpenPage
#open_in_browser, #rewrite_css_and_image_references, #save_and_open_page, #saved_page_dir
Methods included from Logging
Constructor Details
#initialize(adapter = nil) ⇒ Session
Returns a new instance of Session.
72 73 74 75 76 77 78 79 |
# File 'lib/webrat/core/session.rb', line 72 def initialize(adapter = nil) @adapter = adapter @http_method = :get @data = {} @default_headers = {} @custom_headers = {} reset end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
63 64 65 |
# File 'lib/webrat/core/session.rb', line 63 def adapter @adapter end |
#current_url ⇒ Object (readonly)
Returns the value of attribute current_url.
65 66 67 |
# File 'lib/webrat/core/session.rb', line 65 def current_url @current_url end |
#elements ⇒ Object (readonly)
Returns the value of attribute elements.
66 67 68 |
# File 'lib/webrat/core/session.rb', line 66 def elements @elements end |
Instance Method Details
#automate ⇒ Object
251 252 253 254 |
# File 'lib/webrat/core/session.rb', line 251 def automate return unless Webrat.configuration.mode == :selenium yield end |
#basic_auth(user, pass) ⇒ Object
107 108 109 110 |
# File 'lib/webrat/core/session.rb', line 107 def basic_auth(user, pass) encoded_login = ["#{user}:#{pass}"].pack("m*").gsub(/\n/, '') header('HTTP_AUTHORIZATION', "Basic #{encoded_login}") end |
#check_for_infinite_redirects ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/webrat/core/session.rb', line 141 def check_for_infinite_redirects if current_url == response_location @_identical_redirect_count ||= 0 @_identical_redirect_count += 1 end if infinite_redirect_limit_exceeded? raise InfiniteRedirectError.new("#{Webrat.configuration.infinite_redirect_limit} redirects to the same URL (#{current_url.inspect})") end 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"
199 200 201 202 203 |
# File 'lib/webrat/core/session.rb', line 199 def click_link_within(selector, link_text) within(selector) do click_link(link_text) end end |
#current_dom ⇒ Object
:nodoc:
81 82 83 |
# File 'lib/webrat/core/session.rb', line 81 def current_dom #:nodoc: current_scope.dom end |
#current_page ⇒ Object
For backwards compatibility – removing in 1.0
86 87 88 89 90 91 92 93 |
# File 'lib/webrat/core/session.rb', line 86 def current_page #:nodoc: warn "current_page is deprecated and will be going away in the next release. Use current_url instead." page = OpenStruct.new page.url = @current_url page.http_method = @http_method page.data = @data page end |
#current_scope ⇒ Object
:nodoc:
182 183 184 |
# File 'lib/webrat/core/session.rb', line 182 def current_scope #:nodoc: scopes.last || page_scope end |
#doc_root ⇒ Object
:nodoc:
95 96 97 |
# File 'lib/webrat/core/session.rb', line 95 def doc_root #:nodoc: nil end |
#dom ⇒ Object
238 239 240 |
# File 'lib/webrat/core/session.rb', line 238 def dom page_scope.dom end |
#exception_caught? ⇒ Boolean
:nodoc:
178 179 180 |
# File 'lib/webrat/core/session.rb', line 178 def exception_caught? #:nodoc: response_body =~ /Exception caught/ end |
#formatted_error ⇒ Object
Subclasses can override this to show error messages without html
226 227 228 |
# File 'lib/webrat/core/session.rb', line 226 def formatted_error #:nodoc: response_body end |
#header(key, value) ⇒ Object
99 100 101 |
# File 'lib/webrat/core/session.rb', line 99 def header(key, value) @custom_headers[key] = value end |
#headers ⇒ Object
:nodoc:
112 113 114 |
# File 'lib/webrat/core/session.rb', line 112 def headers #:nodoc: @default_headers.dup.merge(@custom_headers.dup) end |
#http_accept(mime_type) ⇒ Object
103 104 105 |
# File 'lib/webrat/core/session.rb', line 103 def http_accept(mime_type) header('Accept', Webrat::MIME.mime_type(mime_type)) end |
#infinite_redirect_limit_exceeded? ⇒ Boolean
152 153 154 155 |
# File 'lib/webrat/core/session.rb', line 152 def infinite_redirect_limit_exceeded? Webrat.configuration.infinite_redirect_limit && (@_identical_redirect_count || 0) > Webrat.configuration.infinite_redirect_limit end |
#internal_redirect? ⇒ Boolean
165 166 167 168 169 170 171 |
# File 'lib/webrat/core/session.rb', line 165 def internal_redirect? return false unless redirect? #should keep internal_redirects if the subdomain changes current_host_domain = current_host.split('.')[-2..-1].join('.') rescue current_host response_location_host_domain = response_location_host.split('.')[-2..-1].join('.') rescue response_location_host current_host_domain == response_location_host_domain end |
#page_scope ⇒ Object
:nodoc:
234 235 236 |
# File 'lib/webrat/core/session.rb', line 234 def page_scope #:nodoc: @_page_scope ||= Scope.from_page(self, response, response_body) end |
#redirect? ⇒ Boolean
:nodoc:
161 162 163 |
# File 'lib/webrat/core/session.rb', line 161 def redirect? #:nodoc: (response_code / 100).to_i == 3 end |
#redirected_to ⇒ Object
easy helper to pull out where we were redirected to
174 175 176 |
# File 'lib/webrat/core/session.rb', line 174 def redirected_to redirect? ? response_location : nil end |
#reload ⇒ Object
Reloads the last page requested. Note that this will resubmit forms and their data.
188 189 190 |
# File 'lib/webrat/core/session.rb', line 188 def reload request_page(@current_url, @http_method, @data) end |
#request_page(url, http_method, data) ⇒ Object
:nodoc:
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/webrat/core/session.rb', line 116 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}" process_request(http_method, url, data, h) 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 if internal_redirect? check_for_infinite_redirects request_page(response_location, :get, {}) end return response end |
#scopes ⇒ Object
:nodoc:
230 231 232 |
# File 'lib/webrat/core/session.rb', line 230 def scopes #:nodoc: @_scopes ||= [] end |
#simulate ⇒ Object
246 247 248 249 |
# File 'lib/webrat/core/session.rb', line 246 def simulate return if Webrat.configuration.mode == :selenium yield end |
#success_code? ⇒ Boolean
:nodoc:
157 158 159 |
# File 'lib/webrat/core/session.rb', line 157 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 "/"
219 220 221 |
# File 'lib/webrat/core/session.rb', line 219 def visit(url = nil, http_method = :get, data = {}) request_page(url, http_method, data) end |
#within(selector) ⇒ Object
207 208 209 210 211 212 |
# File 'lib/webrat/core/session.rb', line 207 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
242 243 244 |
# File 'lib/webrat/core/session.rb', line 242 def xml_content_type? false end |