public
Fork of brynary/webrat
Description: Webrat - Ruby Acceptance Testing for Web applications
Homepage: http://www.brynary.com/uploads/webrat/rdoc/index.html
Clone URL: git://github.com/nicksieger/webrat.git
Search Repo:
Add save_and_open_page. Add radio button support via #chooses method
brynary (author)
Sun Mar 02 17:06:43 -0800 2008
commit  f7420463fdd2fb0dd7e1c66833d5d7e6f4f0ac68
tree    be3b2909613c2301996b699306a33cedd3f59723
parent  7b85e0d15ad07abfd61543348d166551b970eba6
...
1
2
3
 
 
 
4
5
6
...
1
2
 
3
4
5
6
7
8
0
@@ -1,6 +1,8 @@
0
 == SVN
0
 
0
-* Fix bad reference to #select method in README (Patch from Luke Melia)
0
+* Add save_and_open_page to aid in debugging
0
+* Add radio button support via #chooses method
0
+* Docfix: bad reference to #select method in README (Patch from Luke Melia)
0
 * Allow specifying the input name/label when doing a select (Patch from David Chelimsky)
0
 * Raise a specific exception if the developer tries to manipulate form elements before loading a page (Patch from James Deville)
0
 * Add basic support for Rails-generated JavaScript link tags
...
1
2
3
4
...
 
1
2
3
0
@@ -1,4 +1,3 @@
0
-Option button support
0
 Full support for multiple forms on a page
0
 Track the current form based on the location of the last manipulated input, use this as a default for clicks_button
0
 Make current_url work with redirections
...
139
140
141
 
 
 
 
 
 
 
 
 
 
 
 
142
143
144
...
158
159
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
162
163
...
222
223
224
225
 
226
227
228
...
269
270
271
272
 
 
 
 
 
 
273
274
275
...
343
344
345
346
 
 
347
348
349
...
362
363
364
365
 
 
 
 
 
 
 
 
 
 
366
367
368
...
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
...
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
...
249
250
251
 
252
253
254
255
...
296
297
298
 
299
300
301
302
303
304
305
306
307
...
375
376
377
 
378
379
380
381
382
...
395
396
397
 
398
399
400
401
402
403
404
405
406
407
408
409
410
0
@@ -139,6 +139,18 @@ module ActionController
0
           add_form_data(input, input.attributes["value"])
0
         end
0
       end
0
+
0
+ # Verifies that an input radio button exists on the current page and marks it
0
+ # as checked, so that the value will be submitted with the form.
0
+ #
0
+ # Example:
0
+ # chooses 'First Option'
0
+ def chooses(field)
0
+ radio = find_field_by_name_or_label(field)
0
+ return flunk("Could not find radio button #{field.inspect}") if radio.nil?
0
+ return flunk("Input #{radio.inspect} is not a radio button") unless radio.attributes['type'] == 'radio'
0
+ add_form_data(radio, radio.attributes["value"] || "on")
0
+ end
0
       
0
       # Verifies that a submit button exists for the form, then submits the form, follows
0
       # any redirects, and verifies the final page was successful.
0
@@ -158,6 +170,21 @@ module ActionController
0
       
0
       def submits_form(form_id = nil) # :nodoc:
0
       end
0
+
0
+ # Saves the currently loaded page out to RAILS_ROOT/tmp/ and opens it in the default
0
+ # web browser if on OS X. Useful for debugging.
0
+ #
0
+ # Example:
0
+ # save_and_open_page
0
+ def save_and_open_page
0
+ return unless File.exist?(RAILS_ROOT + "/tmp")
0
+
0
+ filename = "webrat-#{Time.now.to_i}.html"
0
+ File.open(RAILS_ROOT + "/tmp/#{filename}", "w") do |f|
0
+ f.write response.body
0
+ end
0
+ `open tmp/#{filename}`
0
+ end
0
     
0
     protected # Methods you could call, but probably shouldn't
0
     
0
@@ -222,7 +249,7 @@ module ActionController
0
       
0
       def find_button(value = nil) # :nodoc:
0
         return nil unless value
0
- submit_buttons.detect { |el| el.attributes["value"] == value }
0
+ submit_buttons.detect { |el| el.attributes["value"] =~ /^\W*#{value}\b/i }
0
       end
0
       
0
       def add_form_data(input_element, value) # :nodoc:
0
@@ -269,7 +296,12 @@ module ActionController
0
         debug_log "REQUESTING PAGE: #{method.to_s.upcase} #{url} with #{data.inspect}"
0
         @current_url = url
0
         self.send "#{method}_via_redirect", @current_url, data || {}
0
- assert_response :success
0
+
0
+ if response.body =~ /Exception caught/ || response.body.blank?
0
+ save_and_open_page
0
+ end
0
+
0
+ assert_response :success
0
         reset_dom
0
       end
0
       
0
@@ -343,7 +375,8 @@ module ActionController
0
       def add_default_params_for(form) # :nodoc:
0
         add_default_params_from_inputs_for(form)
0
         add_default_params_from_checkboxes_for(form)
0
- add_default_params_from_textateas_for(form)
0
+ add_default_params_from_radio_buttons_for(form)
0
+ add_default_params_from_textareas_for(form)
0
       end
0
       
0
       def add_default_params_from_inputs_for(form) # :nodoc:
0
@@ -362,7 +395,16 @@ module ActionController
0
         end
0
       end
0
       
0
- def add_default_params_from_textateas_for(form) # :nodoc:
0
+ def add_default_params_from_radio_buttons_for(form) # :nodoc:
0
+ (form / "input").each do |input|
0
+ next if input.attributes["type"] != "radio"
0
+ if input.attributes["checked"] == "checked"
0
+ add_form_data(input, input.attributes["value"])
0
+ end
0
+ end
0
+ end
0
+
0
+ def add_default_params_from_textareas_for(form) # :nodoc:
0
         (form / "textarea").each do |input|
0
           add_form_data(input, input.inner_html)
0
         end
...
5
6
7
8
 
 
9
10
11
...
158
159
160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
162
163
...
5
6
7
 
8
9
10
11
12
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
0
@@ -5,7 +5,8 @@ class ClicksButtonTest < Test::Unit::TestCase
0
     @session = ActionController::Integration::Session.new
0
     @session.stubs(:assert_response)
0
     @session.stubs(:get_via_redirect)
0
- @session.stubs(:response).returns(@response=mock)
0
+ @response = mock
0
+ @session.stubs(:response).returns(@response)
0
   end
0
   
0
   def test_should_fail_if_no_buttons
0
@@ -158,6 +159,20 @@ class ClicksButtonTest < Test::Unit::TestCase
0
     @session.clicks_button
0
   end
0
   
0
+ def test_should_send_default_radio_options
0
+ @response.stubs(:body).returns(<<-EOS)
0
+ <form method="get" action="/login">
0
+ <input id="user_gender_male" name="user[gender]" type="radio" value="M" />
0
+ <label for="user_gender_male">Male</label>
0
+ <input id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
0
+ <label for="user_gender_female">Female</label>
0
+ <input type="submit" />
0
+ </form>
0
+ EOS
0
+ @session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "F"})
0
+ @session.clicks_button
0
+ end
0
+
0
   def test_should_send_correct_data_for_rails_style_unchecked_fields
0
     @response.stubs(:body).returns(<<-EOS)
0
       <form method="get" action="/login">
...
1
2
 
 
3
 
4
5
6
7
 
 
 
8
9
10
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0
@@ -1,10 +1,16 @@
0
 require File.dirname(__FILE__) + "/helper"
0
 
0
+RAILS_ROOT = "." unless defined?(RAILS_ROOT)
0
+
0
 class VisitsTest < Test::Unit::TestCase
0
+
0
   def setup
0
     @session = ActionController::Integration::Session.new
0
     @session.stubs(:assert_response)
0
     @session.stubs(:get_via_redirect)
0
+ @response = mock
0
+ @session.stubs(:response).returns(@response)
0
+ @response.stubs(:body).returns("")
0
   end
0
 
0
   def test_should_use_get

Comments

    No one has commented yet.