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(context = nil) ⇒ Session

:nodoc:



51
52
53
54
55
56
57
58
59
# File 'lib/webrat/core/session.rb', line 51

def initialize(context = nil) #:nodoc:
  @http_method     = :get
  @data            = {}
  @default_headers = {}
  @custom_headers  = {}
  @context         = context

  reset
end

Instance Attribute Details

#current_urlObject (readonly)

Returns the value of attribute current_url.



48
49
50
# File 'lib/webrat/core/session.rb', line 48

def current_url
  @current_url
end

#elementsObject (readonly)

Returns the value of attribute elements.



49
50
51
# File 'lib/webrat/core/session.rb', line 49

def elements
  @elements
end

Instance Method Details

#automateObject



214
215
216
217
# File 'lib/webrat/core/session.rb', line 214

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

#basic_auth(user, pass) ⇒ Object



86
87
88
89
# File 'lib/webrat/core/session.rb', line 86

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

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

Example:

click_link_within "#user_12", "Vote"


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

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

#current_domObject

:nodoc:



61
62
63
# File 'lib/webrat/core/session.rb', line 61

def current_dom #:nodoc:
  current_scope.dom
end

#current_pageObject

For backwards compatibility – removing in 1.0



66
67
68
69
70
71
72
# File 'lib/webrat/core/session.rb', line 66

def current_page #:nodoc:
  page = OpenStruct.new
  page.url = @current_url
  page.http_method = @http_method
  page.data = @data
  page
end

#current_scopeObject

:nodoc:



145
146
147
# File 'lib/webrat/core/session.rb', line 145

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

#doc_rootObject

:nodoc:



74
75
76
# File 'lib/webrat/core/session.rb', line 74

def doc_root #:nodoc:
  nil
end

#domObject



201
202
203
# File 'lib/webrat/core/session.rb', line 201

def dom
  page_scope.dom
end

#exception_caught?Boolean

:nodoc:

Returns:

  • (Boolean)


141
142
143
# File 'lib/webrat/core/session.rb', line 141

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

#formatted_errorObject

Subclasses can override this to show error messages without html



189
190
191
# File 'lib/webrat/core/session.rb', line 189

def formatted_error #:nodoc:
  response_body
end

#header(key, value) ⇒ Object



78
79
80
# File 'lib/webrat/core/session.rb', line 78

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

#headersObject

:nodoc:



91
92
93
# File 'lib/webrat/core/session.rb', line 91

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

#http_accept(mime_type) ⇒ Object



82
83
84
# File 'lib/webrat/core/session.rb', line 82

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

#internal_redirect?Boolean

Returns:

  • (Boolean)


128
129
130
131
132
133
134
# File 'lib/webrat/core/session.rb', line 128

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:



197
198
199
# File 'lib/webrat/core/session.rb', line 197

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

#redirect?Boolean

:nodoc:

Returns:

  • (Boolean)


124
125
126
# File 'lib/webrat/core/session.rb', line 124

def redirect? #:nodoc:
  response_code / 100 == 3
end

#redirected_toObject

easy helper to pull out where we were redirected to



137
138
139
# File 'lib/webrat/core/session.rb', line 137

def redirected_to
  redirect? ? response_location : nil
end

#reloadObject

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



151
152
153
# File 'lib/webrat/core/session.rb', line 151

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

#request_page(url, http_method, data) ⇒ Object

:nodoc:

Raises:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/webrat/core/session.rb', line 95

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

  request_page(response_location, :get, {}) if internal_redirect?

  return response
end

#scopesObject

:nodoc:



193
194
195
# File 'lib/webrat/core/session.rb', line 193

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

#simulateObject



209
210
211
212
# File 'lib/webrat/core/session.rb', line 209

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

#success_code?Boolean

:nodoc:

Returns:

  • (Boolean)


120
121
122
# File 'lib/webrat/core/session.rb', line 120

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


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

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

#within(selector) ⇒ Object



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

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)


205
206
207
# File 'lib/webrat/core/session.rb', line 205

def xml_content_type?
  false
end