brynary-webrat 0.3.2.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,6 +52,20 @@ module Webrat
52
52
  end
53
53
 
54
54
  alias_method :match_tag, :have_tag
55
+
56
+ # Asserts that the body of the response contains
57
+ # the supplied tag with the associated selectors
58
+ def assert_have_tag(name, attributes = {})
59
+ ht = HaveTag.new([name, attributes])
60
+ assert ht.matches?(response_body), ht.failure_message
61
+ end
62
+
63
+ # Asserts that the body of the response
64
+ # does not contain the supplied string or regepx
65
+ def assert_have_no_tag(name, attributes = {})
66
+ ht = HaveTag.new([name, attributes])
67
+ assert !ht.matches?(response_body), ht.negative_failure_message
68
+ end
55
69
 
56
70
  end
57
71
  end
@@ -79,5 +79,15 @@ module Webrat
79
79
  end
80
80
  alias_method :match_xpath, :have_xpath
81
81
 
82
+ def assert_have_xpath(expected, &block)
83
+ hs = HaveXpath.new(expected, &block)
84
+ assert hs.matches?(response_body), hs.failure_message
85
+ end
86
+
87
+ def assert_have_no_xpath(expected, &block)
88
+ hs = HaveXpath.new(expected, &block)
89
+ assert !hs.matches?(response_body), hs.negative_failure_message
90
+ end
91
+
82
92
  end
83
93
  end
@@ -47,6 +47,7 @@ module Webrat
47
47
  :select_option,
48
48
  :set_hidden_field, :submit_form,
49
49
  :request_page, :current_dom,
50
+ :response_body,
50
51
  :selects_date, :selects_time, :selects_datetime,
51
52
  :select_date, :select_time, :select_datetime,
52
53
  :field_by_xpath,
@@ -30,6 +30,10 @@ module Webrat
30
30
  def initialize(session, &block) #:nodoc:
31
31
  @session = session
32
32
  instance_eval(&block) if block_given?
33
+
34
+ if @selector && scoped_dom.nil?
35
+ raise Webrat::NotFoundError.new("The scope was not found on the page: #{@selector.inspect}")
36
+ end
33
37
  end
34
38
 
35
39
  # Verifies an input field or textarea exists on the current page, and stores a value for
@@ -50,6 +54,11 @@ module Webrat
50
54
 
51
55
  webrat_deprecate :fills_in, :fill_in
52
56
 
57
+ # Verifies that a hidden field exists on the current page and sets
58
+ # the value to that given by the <tt>:to</tt> option.
59
+ #
60
+ # Example:
61
+ # set_hidden_field 'user_id', :to => 1
53
62
  def set_hidden_field(field_locator, options = {})
54
63
  field = locate_field(field_locator, HiddenField)
55
64
  field.set(options[:to])
@@ -207,7 +216,16 @@ module Webrat
207
216
  end
208
217
 
209
218
  webrat_deprecate :attaches_file, :attach_file
210
-
219
+
220
+ # Issues a request for the URL pointed to by an <tt>area</tt> tag
221
+ # on the current page, follows any redirects, and verifies the
222
+ # final page load was successful.
223
+ #
224
+ # The area used is the first area whose title or id contains the
225
+ # given +area_name+ (case is ignored).
226
+ #
227
+ # Example:
228
+ # click_area 'Australia'
211
229
  def click_area(area_name)
212
230
  find_area(area_name).click
213
231
  end
@@ -258,7 +276,15 @@ module Webrat
258
276
  end
259
277
 
260
278
  webrat_deprecate :clicks_button, :click_button
261
-
279
+
280
+ # Submit the form with the given id.
281
+ #
282
+ # Note that +click_button+ is usually preferrable for simulating
283
+ # form submissions, as you may specify part of the button text
284
+ # rather than the form id.
285
+ #
286
+ # Example:
287
+ # submit_form 'login'
262
288
  def submit_form(id)
263
289
  FormLocator.new(@session, dom, id).locate.submit
264
290
  end
@@ -290,14 +316,8 @@ module Webrat
290
316
  return dom
291
317
  end
292
318
 
293
- def scoped_dom #:nodoc:
294
- source = Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first)
295
-
296
- if @session.xml_content_type?
297
- Webrat::XML.xml_document(source)
298
- else
299
- Webrat::XML.html_document(source)
300
- end
319
+ def scoped_dom
320
+ Webrat::XML.css_at(@scope.dom, @selector)
301
321
  end
302
322
 
303
323
  def locate_field(field_locator, *field_types) #:nodoc:
@@ -8,7 +8,7 @@ module Webrat
8
8
  # A page load or form submission returned an unsuccessful response code (500-599)
9
9
  class PageLoadError < WebratError
10
10
  end
11
-
11
+
12
12
  def self.session_class
13
13
  case Webrat.configuration.mode
14
14
  when :rails
@@ -24,32 +24,44 @@ module Webrat
24
24
  when :mechanize
25
25
  MechanizeSession
26
26
  else
27
- raise WebratError.new("Unknown Webrat mode: #{Webrat.configuration.mode.inspect}")
27
+ raise WebratError.new(<<-STR)
28
+ Unknown Webrat mode: #{Webrat.configuration.mode.inspect}
29
+
30
+ Please ensure you have a Webrat configuration block that specifies a mode
31
+ in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).
32
+
33
+ This configure block supercedes the need to require "webrat/<framework>".
34
+
35
+ For example:
36
+
37
+ Webrat.configure do |config|
38
+ config.mode = :rails
39
+ end
40
+ STR
28
41
  end
29
42
  end
30
-
43
+
31
44
  class Session
32
45
  extend Forwardable
33
46
  include Logging
34
47
  include SaveAndOpenPage
35
-
36
48
  attr_reader :current_url
37
49
  attr_reader :elements
38
-
50
+
39
51
  def initialize(context = nil) #:nodoc:
40
52
  @http_method = :get
41
53
  @data = {}
42
54
  @default_headers = {}
43
55
  @custom_headers = {}
44
56
  @context = context
45
-
57
+
46
58
  reset
47
59
  end
48
-
60
+
49
61
  def current_dom #:nodoc:
50
62
  current_scope.dom
51
63
  end
52
-
64
+
53
65
  # For backwards compatibility -- removing in 1.0
54
66
  def current_page #:nodoc:
55
67
  page = OpenStruct.new
@@ -58,7 +70,7 @@ module Webrat
58
70
  page.data = @data
59
71
  page
60
72
  end
61
-
73
+
62
74
  def doc_root #:nodoc:
63
75
  nil
64
76
  end
@@ -70,7 +82,7 @@ module Webrat
70
82
  def http_accept(mime_type)
71
83
  header('Accept', Webrat::MIME.mime_type(mime_type))
72
84
  end
73
-
85
+
74
86
  def basic_auth(user, pass)
75
87
  encoded_login = ["#{user}:#{pass}"].pack("m*")
76
88
  header('HTTP_AUTHORIZATION', "Basic #{encoded_login}")
@@ -93,28 +105,38 @@ module Webrat
93
105
 
94
106
  save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
95
107
  raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?
96
-
108
+
97
109
  reset
98
-
110
+
99
111
  @current_url = url
100
112
  @http_method = http_method
101
113
  @data = data
102
-
114
+
115
+ request_page(response_location, :get, data) if internal_redirect?
116
+
103
117
  return response
104
118
  end
105
-
119
+
106
120
  def success_code? #:nodoc:
107
121
  (200..499).include?(response_code)
108
122
  end
109
-
123
+
124
+ def redirect? #:nodoc:
125
+ response_code / 100 == 3
126
+ end
127
+
128
+ def internal_redirect? #:nodoc:
129
+ redirect? && current_host == response_location_host
130
+ end
131
+
110
132
  def exception_caught? #:nodoc:
111
133
  response_body =~ /Exception caught/
112
134
  end
113
-
135
+
114
136
  def current_scope #:nodoc:
115
137
  scopes.last || page_scope
116
138
  end
117
-
139
+
118
140
  # Reloads the last page requested. Note that this will resubmit forms
119
141
  # and their data.
120
142
  def reloads
@@ -122,10 +144,10 @@ module Webrat
122
144
  end
123
145
 
124
146
  webrat_deprecate :reload, :reloads
125
-
126
-
147
+
148
+
127
149
  # Works like click_link, but only looks for the link text within a given selector
128
- #
150
+ #
129
151
  # Example:
130
152
  # click_link_within "#user_12", "Vote"
131
153
  def click_link_within(selector, link_text)
@@ -135,14 +157,14 @@ module Webrat
135
157
  end
136
158
 
137
159
  webrat_deprecate :clicks_link_within, :click_link_within
138
-
160
+
139
161
  def within(selector)
140
162
  scopes.push(Scope.from_scope(self, current_scope, selector))
141
163
  ret = yield(current_scope)
142
164
  scopes.pop
143
165
  return ret
144
166
  end
145
-
167
+
146
168
  # Issues a GET request for a page, follows any redirects, and verifies the final page
147
169
  # load was successful.
148
170
  #
@@ -151,7 +173,7 @@ module Webrat
151
173
  def visit(url = nil, http_method = :get, data = {})
152
174
  request_page(url, http_method, data)
153
175
  end
154
-
176
+
155
177
  webrat_deprecate :visits, :visit
156
178
 
157
179
  # Subclasses can override this to show error messages without html
@@ -166,25 +188,25 @@ module Webrat
166
188
  def page_scope #:nodoc:
167
189
  @_page_scope ||= Scope.from_page(self, response, response_body)
168
190
  end
169
-
191
+
170
192
  def dom
171
193
  page_scope.dom
172
194
  end
173
-
195
+
174
196
  def xml_content_type?
175
197
  false
176
198
  end
177
-
199
+
178
200
  def simulate
179
201
  return if Webrat.configuration.mode == :selenium
180
202
  yield
181
203
  end
182
-
204
+
183
205
  def automate
184
206
  return unless Webrat.configuration.mode == :selenium
185
207
  yield
186
208
  end
187
-
209
+
188
210
  def_delegators :current_scope, :fill_in, :fills_in
189
211
  def_delegators :current_scope, :set_hidden_field
190
212
  def_delegators :current_scope, :submit_form
@@ -199,15 +221,25 @@ module Webrat
199
221
  def_delegators :current_scope, :click_area, :clicks_area
200
222
  def_delegators :current_scope, :click_link, :clicks_link
201
223
  def_delegators :current_scope, :click_button, :clicks_button
202
- def_delegators :current_scope, :should_see
203
- def_delegators :current_scope, :should_not_see
204
224
  def_delegators :current_scope, :field_labeled
205
225
  def_delegators :current_scope, :field_by_xpath
206
226
  def_delegators :current_scope, :field_with_id
207
227
  def_delegators :current_scope, :select_option
208
-
228
+
209
229
  private
210
-
230
+
231
+ def response_location
232
+ response.headers["Location"]
233
+ end
234
+
235
+ def current_host
236
+ URI.parse(current_url).host || "www.example.com"
237
+ end
238
+
239
+ def response_location_host
240
+ URI.parse(response_location).host || "www.example.com"
241
+ end
242
+
211
243
  def reset
212
244
  @elements = {}
213
245
  @_scopes = nil
@@ -10,11 +10,11 @@ module Webrat
10
10
  elsif Nokogiri::XML::NodeSet === stringlike
11
11
  stringlike
12
12
  elsif StringIO === stringlike
13
- Nokogiri.parse(stringlike.string)
13
+ Nokogiri::HTML(stringlike.string)
14
14
  elsif stringlike.respond_to?(:body)
15
- Nokogiri.parse(stringlike.body.to_s)
15
+ Nokogiri::HTML(stringlike.body.to_s)
16
16
  else
17
- Nokogiri.parse(stringlike.to_s)
17
+ Nokogiri::HTML(stringlike.to_s)
18
18
  end
19
19
  end
20
20
 
@@ -6,6 +6,10 @@ module Webrat #:nodoc:
6
6
  attr_accessor :response
7
7
  alias :page :response
8
8
 
9
+ def request_page(url, http_method, data) #:nodoc:
10
+ super(absolute_url(url), http_method, data)
11
+ end
12
+
9
13
  def get(url, data, headers_argument_not_used = nil)
10
14
  @response = mechanize.get(url, data)
11
15
  end
@@ -36,8 +40,35 @@ module Webrat #:nodoc:
36
40
  end
37
41
 
38
42
  def_delegators :mechanize, :basic_auth
43
+
44
+ def absolute_url(url) #:nodoc:
45
+ current_host, current_path = split_current_url
46
+ if url =~ Regexp.new('^https?://')
47
+ url
48
+ elsif url =~ Regexp.new('^/')
49
+ current_host + url
50
+ elsif url =~ Regexp.new('^\.')
51
+ current_host + absolute_path(current_path, url)
52
+ else
53
+ url
54
+ end
55
+ end
56
+
57
+ private
58
+ def split_current_url
59
+ current_url =~ Regexp.new('^(https?://[^/]+)(/.*)?')
60
+ [Regexp.last_match(1), Regexp.last_match(2)]
61
+ end
39
62
 
63
+ def absolute_path(current_path, url)
64
+ levels_up = url.split('/').find_all { |x| x == '..' }.size
65
+ ancestor = if current_path.nil?
66
+ ""
67
+ else
68
+ current_path.split("/")[0..(-1 - levels_up)].join("/")
69
+ end
70
+ descendent = url.split("/")[levels_up..-1]
71
+ "#{ancestor}/#{descendent}"
72
+ end
40
73
  end
41
74
  end
42
-
43
- Webrat.configuration.mode = :mechanize
@@ -1,77 +1,9 @@
1
1
  require "webrat"
2
2
 
3
- require "cgi"
4
- gem "extlib"
5
- require "extlib"
6
- require "merb-core"
7
-
8
- HashWithIndifferentAccess = Mash
9
-
10
- module Webrat
11
- class MerbSession < Session #:nodoc:
12
- include Merb::Test::MakeRequest
13
-
14
- attr_accessor :response
15
-
16
- def get(url, data, headers = nil)
17
- do_request(url, data, headers, "GET")
18
- end
19
-
20
- def post(url, data, headers = nil)
21
- do_request(url, data, headers, "POST")
22
- end
23
-
24
- def put(url, data, headers = nil)
25
- do_request(url, data, headers, "PUT")
26
- end
27
-
28
- def delete(url, data, headers = nil)
29
- do_request(url, data, headers, "DELETE")
30
- end
31
-
32
- def response_body
33
- @response.body.to_s
34
- end
35
-
36
- def response_code
37
- @response.status
38
- end
39
-
40
- def do_request(url, data, headers, method)
41
- @response = request(url,
42
- :params => (data && data.any?) ? data : nil,
43
- :headers => headers,
44
- :method => method)
45
- follow_redirect
46
- end
47
-
48
- def follow_redirect
49
- self.get(@response.headers['Location'], nil, @response.headers) if @response.status == 302
50
- end
51
-
52
- end
53
- end
54
-
55
- module Merb #:nodoc:
56
- module Test #:nodoc:
57
- module RequestHelper #:nodoc:
58
- def request(uri, env = {})
59
- @_webrat_session ||= Webrat::MerbSession.new
60
- @_webrat_session.response = @_webrat_session.request(uri, env)
61
- end
62
-
63
- def follow_redirect
64
- @_webrat_session.follow_redirect
65
- end
66
- end
67
- end
68
- end
69
-
70
- class Merb::Test::RspecStory #:nodoc:
71
- def browser
72
- @browser ||= Webrat::MerbSession.new
73
- end
74
- end
75
-
76
- Webrat.configuration.mode = :merb
3
+ # This is a temporary hack to support backwards compatibility
4
+ # with Merb 1.0.8 until it's updated to use the new Webrat.configure
5
+ # syntax
77
6
 
7
+ Webrat.configure do |config|
8
+ config.mode = :merb
9
+ end