forked from brynary/webrat
/
click_button_spec.rb
506 lines (465 loc) · 14.4 KB
/
click_button_spec.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
describe "click_button" do
it "should fail if no buttons" do
with_html <<-HTML
<html>
<form method="get" action="/login"></form>
</html>
HTML
lambda { click_button }.should raise_error(Webrat::NotFoundError)
end
it "should fail if input is not a submit button" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="reset" />
</form>
</html>
HTML
lambda { click_button }.should raise_error(Webrat::NotFoundError)
end
it "should fail if button is disabled" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="submit" disabled="disabled" />
</form>
</html>
HTML
lambda { click_button }.should raise_error(Webrat::DisabledFieldError)
end
it "should default to get method" do
with_html <<-HTML
<html>
<form action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button
end
it "should assert valid response" do
with_html <<-HTML
<html>
<form action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.response_code = 501
lambda { click_button }.should raise_error(Webrat::PageLoadError)
end
[200, 300, 400, 499].each do |status|
it "should consider the #{status} status code as success" do
with_html <<-HTML
<html>
<form action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.stub!(:redirect? => false)
webrat_session.response_code = status
lambda { click_button }.should_not raise_error
end
end
it "should submit the first form by default" do
with_html <<-HTML
<html>
<form method="get" action="/form1">
<input type="submit" />
</form>
<form method="get" action="/form2">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/form1", {})
click_button
end
it "should not explode on file fields" do
with_html <<-HTML
<html>
<form method="get" action="/form1">
<input type="file" />
<input type="submit" />
</form>
</html>
HTML
click_button
end
it "should submit the form with the specified button" do
with_html <<-HTML
<html>
<form method="get" action="/form1">
<input type="submit" />
</form>
<form method="get" action="/form2">
<input type="submit" value="Form2" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/form2", {})
click_button "Form2"
end
it "should use action from form" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", {})
click_button
end
it "should use method from form" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post)
click_button
end
it "should send button as param if it has a name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" name="login" value="Login" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "login" => "Login")
click_button("Login")
end
it "should not send button as param if it has no name" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" value="Login" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", {})
click_button("Login")
end
it "should send default password field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_password" name="user[password]" value="mypass" type="password" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"password" => "mypass"})
click_button
end
it "should send default hidden field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="hidden" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
click_button
end
it "should send default text field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="test@example.com" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => "test@example.com"})
click_button
end
it "should not send disabled field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input disabled="disabled" id="user_email" name="user[email]" value="test@example.com" type="text" />
<input disabled="disabled" id="user_gender_male" name="user[gender]" type="radio" value="M" />
<label for="user_gender_male">Male</label>
<input disabled="disabled" id="user_gender_female" name="user[gender]" type="radio" value="F" checked="checked" />
<label for="user_gender_female">Female</label>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", {})
click_button
end
it "should send default checked fields" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" checked="checked" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
click_button
end
it "should send default radio options" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"gender" => "F"})
click_button
end
it "should send correct data for rails style unchecked fields" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "0"})
click_button
end
it "should send correct data for rails style checked fields" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"tos" => "1"})
click_button
end
it "should send default collection fields" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:post).with("/login",
"options" => ["burger", "fries", "soda", "soda", "dessert"],
"response" => { "choices" => [{"selected" => "one"}, {"selected" => "two"}, {"selected" => "two"}]})
click_button
end
it "should not send default unchecked fields" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_tos" name="user[tos]" value="1" type="checkbox" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", {})
click_button
end
it "should send default textarea values" do
with_html <<-HTML
<html>
<form method="post" action="/posts">
<textarea name="post[body]">Post body here!</textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Post body here!"})
click_button
end
it "should properly handle HTML entities in textarea default values" do
with_html <<-HTML
<html>
<form method="post" action="/posts">
<textarea name="post[body]">Peanut butter & jelly</textarea>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/posts", "post" => {"body" => "Peanut butter & jelly"})
click_button
end
it "should send default selected option value from select" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "month" => "2")
click_button
end
it "should send default selected option inner html from select when no value attribute" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month">
<option>January</option>
<option selected="selected">February</option>
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "month" => "February")
click_button
end
it "should send first select option value when no option selected" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<select name="month">
<option value="1">January</option>
<option value="2">February</option>
</select>
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "month" => "1")
click_button
end
it "should handle nested properties" do
with_html <<-HTML
<html>
<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>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "contestant" => {"scores" => {'1' => '2', '3' => '4'}})
click_button
end
it "should send default empty text field values" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
click_button
end
it "should recognize button tags" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<button type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
click_button
end
it "should recognize input tags with the type button" do
with_html <<-HTML
<html>
<form action="/">
<input type="button" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button
end
it "should recognize image button tags" do
with_html <<-HTML
<html>
<form action="/">
<input type="image" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button
end
it "should find buttons by their IDs" do
with_html <<-HTML
<html>
<form action="/">
<input type="submit" id="my_button" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button "my_button"
end
it "should find image buttons by their alt text" do
with_html <<-HTML
<html>
<form action="/">
<input type="image" alt="Go" />
</form>
</html>
HTML
webrat_session.should_receive(:get)
click_button "Go"
end
it "should recognize button tags by content" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input id="user_email" name="user[email]" value="" type="text" />
<button type="submit">Login</button>
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "user" => {"email" => ""})
click_button "Login"
end
it "should provide a default x and y coordinate of 0,0 when clicking an image" do
with_html <<-HTML
<html>
<form method="get" action="/login">
<input type="image" name="login" value="Login" />
</form>
</html>
HTML
webrat_session.should_receive(:get).with("/login", "login" => "Login", "login.x" => 0, "login.y" => 0)
click_button("Login")
end
end