Class: Webrat::Session

Inherits:
Object show all
Extended by:
Forwardable
Includes:
Logging, SaveAndOpenPage
Defined in:
lib/webrat/core/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SaveAndOpenPage

#open_in_browser, #rewrite_css_and_image_references, #save_and_open_page, #saved_page_dir

Methods included from Logging

#debug_log, #logger

Constructor Details

#initialize(adapter = nil) ⇒ Session

Returns a new instance of Session.



75
76
77
78
79
80
81
82
# File 'lib/webrat/core/session.rb', line 75

def initialize(adapter = nil)
  @adapter         = adapter
  @http_method     = :get
  @data            = {}
  @default_headers = {}
  @custom_headers  = {}
  reset
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



66
67
68
# File 'lib/webrat/core/session.rb', line 66

def adapter
  @adapter
end

#current_urlObject (readonly)

Returns the value of attribute current_url.



68
69
70
# File 'lib/webrat/core/session.rb', line 68

def current_url
  @current_url
end

#elementsObject (readonly)

Returns the value of attribute elements.



69
70
71
# File 'lib/webrat/core/session.rb', line 69

def elements
  @elements
end

Instance Method Details

#automateObject



254
255
256
257
# File 'lib/webrat/core/session.rb', line 254

def automate
  return unless Webrat.configuration.mode == :selenium
  yield
end

#basic_auth(user, pass) ⇒ Object



110
111
112
113
# File 'lib/webrat/core/session.rb', line 110

def basic_auth(user, pass)
   = ["#{user}:#{pass}"].pack("m*")
  header('HTTP_AUTHORIZATION', "Basic #{}")
end

#check_for_infinite_redirectsObject



144
145
146
147
148
149
150
151
152
153
# File 'lib/webrat/core/session.rb', line 144

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

Works like click_link, but only looks for the link text within a given selector

Example:

click_link_within "#user_12", "Vote"


202
203
204
205
206
# File 'lib/webrat/core/session.rb', line 202

def click_link_within(selector, link_text)
  within(selector) do
    click_link(link_text)
  end
end

#current_domObject

:nodoc:



84
85
86
# File 'lib/webrat/core/session.rb', line 84

def current_dom #:nodoc:
  current_scope.dom
end

#current_pageObject

For backwards compatibility – removing in 1.0



89
90
91
92
93
94
95
96
# File 'lib/webrat/core/session.rb', line 89

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_scopeObject

:nodoc:



185
186
187
# File 'lib/webrat/core/session.rb', line 185

def current_scope #:nodoc:
  scopes.last || page_scope
end

#doc_rootObject

:nodoc:



98
99
100
# File 'lib/webrat/core/session.rb', line 98

def doc_root #:nodoc:
  nil
end

#domObject



241
242
243
# File 'lib/webrat/core/session.rb', line 241

def dom
  page_scope.dom
end

#exception_caught?Boolean

:nodoc:

Returns:

  • (Boolean)


181
182
183
# File 'lib/webrat/core/session.rb', line 181

def exception_caught? #:nodoc:
  response_body =~ /Exception caught/
end

#formatted_errorObject

Subclasses can override this to show error messages without html



229
230
231
# File 'lib/webrat/core/session.rb', line 229

def formatted_error #:nodoc:
  response_body
end

#header(key, value) ⇒ Object



102
103
104
# File 'lib/webrat/core/session.rb', line 102

def header(key, value)
  @custom_headers[key] = value
end

#headersObject

:nodoc:



115
116
117
# File 'lib/webrat/core/session.rb', line 115

def headers #:nodoc:
  @default_headers.dup.merge(@custom_headers.dup)
end

#http_accept(mime_type) ⇒ Object



106
107
108
# File 'lib/webrat/core/session.rb', line 106

def http_accept(mime_type)
  header('Accept', Webrat::MIME.mime_type(mime_type))
end

#infinite_redirect_limit_exceeded?Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/webrat/core/session.rb', line 155

def infinite_redirect_limit_exceeded?
   Webrat.configuration.infinite_redirect_limit &&
   (@_identical_redirect_count || 0) > Webrat.configuration.infinite_redirect_limit
end

#internal_redirect?Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
# File 'lib/webrat/core/session.rb', line 168

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_scopeObject

:nodoc:



237
238
239
# File 'lib/webrat/core/session.rb', line 237

def page_scope #:nodoc:
  @_page_scope ||= Scope.from_page(self, response, response_body)
end

#redirect?Boolean

:nodoc:

Returns:

  • (Boolean)


164
165
166
# File 'lib/webrat/core/session.rb', line 164

def redirect? #:nodoc:
  (response_code / 100).to_i == 3
end

#redirected_toObject

easy helper to pull out where we were redirected to



177
178
179
# File 'lib/webrat/core/session.rb', line 177

def redirected_to
  redirect? ? response_location : nil
end

#reloadObject

Reloads the last page requested. Note that this will resubmit forms and their data.



191
192
193
# File 'lib/webrat/core/session.rb', line 191

def reload
  request_page(@current_url, @http_method, @data)
end

#request_page(url, http_method, data) ⇒ Object

:nodoc:

Raises:



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/webrat/core/session.rb', line 119

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

#scopesObject

:nodoc:



233
234
235
# File 'lib/webrat/core/session.rb', line 233

def scopes #:nodoc:
  @_scopes ||= []
end

#simulateObject



249
250
251
252
# File 'lib/webrat/core/session.rb', line 249

def simulate
  return if Webrat.configuration.mode == :selenium
  yield
end

#success_code?Boolean

:nodoc:

Returns:

  • (Boolean)


160
161
162
# File 'lib/webrat/core/session.rb', line 160

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 "/"


222
223
224
# File 'lib/webrat/core/session.rb', line 222

def visit(url = nil, http_method = :get, data = {})
  request_page(url, http_method, data)
end

#within(selector) ⇒ Object



210
211
212
213
214
215
# File 'lib/webrat/core/session.rb', line 210

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

Returns:

  • (Boolean)


245
246
247
# File 'lib/webrat/core/session.rb', line 245

def xml_content_type?
  false
end