/
clicks_button_test.rb
379 lines (342 loc) · 12.5 KB
/
clicks_button_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
require File.dirname(__FILE__) + "/helper"
class ClicksButtonTest < Test::Unit::TestCase
def setup
@session = test_session
@session.stubs(:assert_response)
@session.stubs(:get_via_redirect)
@page = Webrat::Page.new(@session)
@session.stubs(:current_page).returns(@page)
@response = mock
@session.stubs(:response).returns(@response)
@controller = mock
@controller.stubs(:status).returns(200)
end
def test_should_fail_if_no_buttons
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login"></form>
EOS
assert_raises RuntimeError do
@session.clicks_button
end
end
def test_should_fail_if_input_is_not_a_submit_button
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input type="reset" />
</form>
EOS
assert_raises RuntimeError do
@session.clicks_button
end
end
def test_should_default_to_get_method
@response.stubs(:body).returns(<<-EOS)
<form action="/login">
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect)
@session.clicks_button
end
def test_should_assert_valid_response
@response.stubs(:body).returns(<<-EOS)
<form action="/login">
<input type="submit" />
</form>
EOS
@session.expects(:assert_response).with(:success)
@session.clicks_button
end
def test_should_default_to_current_url
@response.stubs(:body).returns(<<-EOS)
<form method="get">
<input type="submit" />
</form>
EOS
@page.stubs(:url).returns("/current")
@session.expects(:get_via_redirect).with("/current", {})
@session.clicks_button
end
def test_should_submit_the_first_form_by_default
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/form1">
<input type="submit" />
</form>
<form method="get" action="/form2">
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/form1", {})
@session.clicks_button
end
#<button type='submit'> is also valid HTML.
def test_should_work_with_button_submit
@response.stubs(:body).returns(<<-EOS)
<form action="/form1" method="post"><p><label for="login">Login</label><br/>
<input type="text" class="text" name="login"/></p>
<p><label for="password">Password</label><br/>
<input type="password" class="password" name="password"/></p>
<p><button type="submit">Log in</button></p>
</form>
EOS
@session.expects(:post_via_redirect)
@session.clicks_button
end
def test_should_not_explode_on_file_fields
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/form1">
<input type="file" />
<input type="submit" />
</form>
EOS
@session.clicks_button
end
def test_should_submit_the_form_with_the_specified_button
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/form1">
<input type="submit" />
</form>
<form method="get" action="/form2">
<input type="submit" value="Form2" />
</form>
EOS
@session.expects(:get_via_redirect).with("/form2", {})
@session.clicks_button "Form2"
end
def test_should_use_action_from_form
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", {})
@session.clicks_button
end
def test_should_use_method_from_form
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect)
@session.clicks_button
end
def test_should_send_button_as_param_if_it_has_a_name
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" name="login" value="Login" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "login" => "Login")
@session.clicks_button("Login")
end
def test_should_not_send_button_as_param_if_it_has_no_name
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" value="Login" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", {})
@session.clicks_button("Login")
end
def test_should_send_default_password_field_values
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_password" name="user[password]" value="mypass" type="password" />
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"password" => "mypass"})
@session.clicks_button
end
def test_should_send_default_hidden_field_values
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => "test@example.com"})
@session.clicks_button
end
def test_should_send_default_text_field_values
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => "test@example.com"})
@session.clicks_button
end
def test_should_send_default_checked_fields
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" checked="checked" />
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"})
@session.clicks_button
end
def test_should_send_default_radio_options
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"gender" => "F"})
@session.clicks_button
end
def test_should_send_correct_data_for_rails_style_unchecked_fields
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" />
<input name="user[tos]" type="hidden" value="0" /> TOS
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "0"})
@session.clicks_button
end
def test_should_send_correct_data_for_rails_style_checked_fields
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" type="checkbox" value="1" checked="checked" />
<input name="user[tos]" type="hidden" value="0" /> TOS
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"tos" => "1"})
@session.clicks_button
end
def test_should_send_default_collection_fields
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="checkbox" name="options[]" value="burger" checked="checked" />
<input type="radio" name="options[]" value="fries" checked="checked" />
<input type="text" name="options[]" value="soda" />
<!-- Same value appearing twice -->
<input type="text" name="options[]" value="soda" />
<input type="hidden" name="options[]" value="dessert" />
<input type="hidden" name="response[choices][][selected]" value="one" />
<input type="hidden" name="response[choices][][selected]" value="two" />
<!-- Same value appearing twice -->
<input type="hidden" name="response[choices][][selected]" value="two" />
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login",
"options" => ["burger", "fries", "soda", "soda", "dessert"],
"response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]})
@session.clicks_button
end
def test_should_not_send_default_unchecked_fields
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" />
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", {})
@session.clicks_button
end
def test_should_send_default_textarea_values
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/posts">
<textarea name="post[body]">Post body here!</textarea>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/posts", "post" => {"body" => "Post body here!"})
@session.clicks_button
end
def test_should_send_default_selected_option_value_from_select
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
<option value="2" selected="selected">February</option>
</select>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "month" => "2")
@session.clicks_button
end
def test_should_send_default_selected_option_inner_html_from_select_when_no_value_attribute
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month">
<option>January</option>
<option selected="selected">February</option>
</select>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "month" => "February")
@session.clicks_button
end
def test_should_send_first_select_option_value_when_no_option_selected
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
</select>
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "month" => "1")
@session.clicks_button
end
def test_should_handle_nested_properties
@response.stubs(:body).returns(<<-EOS)
<form method="post" action="/login">
<input type="text" id="contestant_scores_12" name="contestant[scores][1]" value="2"/>
<input type="text" id="contestant_scores_13" name="contestant[scores][3]" value="4"/>
<input type="submit" />
</form>
EOS
@session.expects(:post_via_redirect).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}})
@session.clicks_button
end
def test_should_send_default_empty_text_field_values
@response.stubs(:body).returns(<<-EOS)
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<input type="submit" />
</form>
EOS
@session.expects(:get_via_redirect).with("/login", "user" => {"email" => ""})
@session.clicks_button
end
def test_should_use_put_method_when_needed
@response.stubs(:body).returns(<<-EOS)
<form method="put" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
EOS
@session.stubs(:redirect?).returns(false)
@session.expects(:put).returns(@controller)
@session.clicks_button
end
def test_should_use_delete_method_when_needed
@response.stubs(:body).returns(<<-EOS)
<form method="delete" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
EOS
@session.stubs(:redirect?).returns(false)
@session.expects(:delete).returns(@controller)
@session.clicks_button
end
end