mechanize 2.14.1 → 2.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -1
  3. data/lib/mechanize/directory_saver.rb +2 -1
  4. data/lib/mechanize/file_saver.rb +2 -0
  5. data/lib/mechanize/parser.rb +33 -3
  6. data/lib/mechanize/test_case.rb +2 -9
  7. data/lib/mechanize/version.rb +1 -1
  8. metadata +3 -172
  9. data/.autotest +0 -6
  10. data/.github/dependabot.yml +0 -11
  11. data/.github/workflows/ci.yml +0 -43
  12. data/.github/workflows/upstream.yml +0 -51
  13. data/.gitignore +0 -15
  14. data/.yardopts +0 -8
  15. data/Gemfile +0 -12
  16. data/Rakefile +0 -56
  17. data/examples/flickr_upload.rb +0 -22
  18. data/examples/latest_user_agents.rb +0 -100
  19. data/examples/mech-dump.rb +0 -5
  20. data/examples/proxy_req.rb +0 -7
  21. data/examples/rubygems.rb +0 -23
  22. data/examples/spider.rb +0 -22
  23. data/examples/wikipedia_links_to_philosophy.rb +0 -158
  24. data/mechanize.gemspec +0 -74
  25. data/test/data/htpasswd +0 -1
  26. data/test/data/server.crt +0 -16
  27. data/test/data/server.csr +0 -12
  28. data/test/data/server.key +0 -15
  29. data/test/data/server.pem +0 -15
  30. data/test/test_mechanize.rb +0 -1382
  31. data/test/test_mechanize_cookie.rb +0 -559
  32. data/test/test_mechanize_cookie_jar.rb +0 -615
  33. data/test/test_mechanize_directory_saver.rb +0 -60
  34. data/test/test_mechanize_download.rb +0 -102
  35. data/test/test_mechanize_element_not_found_error.rb +0 -16
  36. data/test/test_mechanize_file.rb +0 -118
  37. data/test/test_mechanize_file_connection.rb +0 -40
  38. data/test/test_mechanize_file_request.rb +0 -26
  39. data/test/test_mechanize_file_response.rb +0 -48
  40. data/test/test_mechanize_file_saver.rb +0 -22
  41. data/test/test_mechanize_form.rb +0 -1025
  42. data/test/test_mechanize_form_check_box.rb +0 -49
  43. data/test/test_mechanize_form_encoding.rb +0 -110
  44. data/test/test_mechanize_form_field.rb +0 -78
  45. data/test/test_mechanize_form_file_upload.rb +0 -21
  46. data/test/test_mechanize_form_image_button.rb +0 -13
  47. data/test/test_mechanize_form_keygen.rb +0 -34
  48. data/test/test_mechanize_form_multi_select_list.rb +0 -98
  49. data/test/test_mechanize_form_option.rb +0 -60
  50. data/test/test_mechanize_form_radio_button.rb +0 -96
  51. data/test/test_mechanize_form_select_list.rb +0 -81
  52. data/test/test_mechanize_form_textarea.rb +0 -53
  53. data/test/test_mechanize_headers.rb +0 -36
  54. data/test/test_mechanize_history.rb +0 -104
  55. data/test/test_mechanize_http_agent.rb +0 -2145
  56. data/test/test_mechanize_http_auth_challenge.rb +0 -64
  57. data/test/test_mechanize_http_auth_realm.rb +0 -56
  58. data/test/test_mechanize_http_auth_store.rb +0 -233
  59. data/test/test_mechanize_http_content_disposition_parser.rb +0 -167
  60. data/test/test_mechanize_http_www_authenticate_parser.rb +0 -204
  61. data/test/test_mechanize_image.rb +0 -9
  62. data/test/test_mechanize_link.rb +0 -171
  63. data/test/test_mechanize_page.rb +0 -295
  64. data/test/test_mechanize_page_encoding.rb +0 -215
  65. data/test/test_mechanize_page_frame.rb +0 -17
  66. data/test/test_mechanize_page_image.rb +0 -191
  67. data/test/test_mechanize_page_link.rb +0 -377
  68. data/test/test_mechanize_page_meta_refresh.rb +0 -151
  69. data/test/test_mechanize_parser.rb +0 -316
  70. data/test/test_mechanize_pluggable_parser.rb +0 -68
  71. data/test/test_mechanize_redirect_limit_reached_error.rb +0 -25
  72. data/test/test_mechanize_redirect_not_get_or_head_error.rb +0 -15
  73. data/test/test_mechanize_response_read_error.rb +0 -29
  74. data/test/test_mechanize_subclass.rb +0 -23
  75. data/test/test_mechanize_util.rb +0 -144
  76. data/test/test_mechanize_xml_file.rb +0 -30
  77. data/test/test_multi_select.rb +0 -120
@@ -1,1025 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'mechanize/test_case'
3
-
4
- class TestMechanizeForm < Mechanize::TestCase
5
-
6
- def setup
7
- super
8
-
9
- @uri = URI 'http://example'
10
- @page = page @uri
11
-
12
- @form = Mechanize::Form.new node('form', 'name' => @NAME), @mech, @page
13
- end
14
-
15
- def test_action
16
- form = Mechanize::Form.new node('form', 'action' => '?a=b&amp;b=c')
17
-
18
- assert_equal '?a=b&b=c', form.action
19
- end
20
-
21
- def test_add_button_to_query
22
- button = Mechanize::Form::Button.new node('input', 'type' => 'submit')
23
-
24
- e = assert_raises ArgumentError do
25
- @form.add_button_to_query button
26
- end
27
-
28
- assert_equal "#{button.inspect} does not belong to the same page " \
29
- "as the form \"#{@NAME}\" in #{@uri}",
30
- e.message
31
- end
32
-
33
- def test_aset
34
- assert_empty @form.keys
35
-
36
- @form['intarweb'] = 'Aaron'
37
-
38
- assert_equal 'Aaron', @form['intarweb']
39
- end
40
-
41
- def test_aset_exists
42
- page = html_page <<-BODY
43
- <title>Page Title</title>
44
- <form name="post_form">
45
- <input name="first" type="text" id="name_first">
46
- <input name="first" type="text">
47
- <input type="submit" value="Submit">
48
- </form>
49
- BODY
50
-
51
- form = page.form_with(:name => 'post_form')
52
-
53
- assert_equal %w[first first], form.keys
54
-
55
- form['first'] = 'Aaron'
56
-
57
- assert_equal 'Aaron', form['first']
58
- assert_equal ['Aaron', ''], form.values
59
- end
60
-
61
- def test_build_query_blank_form
62
- page = @mech.get('http://localhost/tc_blank_form.html')
63
- form = page.forms.first
64
- query = form.build_query
65
- assert(query.length > 0)
66
- assert query.all? { |x| x[1] == '' }
67
- end
68
-
69
- def test_build_query_blank_input_name
70
- html = Nokogiri::HTML <<-HTML
71
- <form>
72
- <input type="text" name="" value="foo" />
73
- </form>
74
- HTML
75
-
76
- form = Mechanize::Form.new html.at('form'), @mech, @page
77
-
78
- assert_equal [], form.build_query
79
- end
80
-
81
- def test_build_query_radio_button_duplicate
82
- html = Nokogiri::HTML <<-HTML
83
- <form>
84
- <input type=radio name=name value=a checked=true>
85
- <input type=radio name=name value=a checked=true>
86
- </form>
87
- HTML
88
-
89
- form = Mechanize::Form.new html.at('form'), @mech, @page
90
-
91
- query = form.build_query
92
-
93
- assert_equal [%w[name a]], query
94
- end
95
-
96
- def test_build_query_radio_button_multiple_checked
97
- html = Nokogiri::HTML <<-HTML
98
- <form>
99
- <input type=radio name=name value=a checked=true>
100
- <input type=radio name=name value=b checked=true>
101
- </form>
102
- HTML
103
-
104
- form = Mechanize::Form.new html.at('form'), @mech, @page
105
-
106
- e = assert_raises Mechanize::Error do
107
- form.build_query
108
- end
109
-
110
- assert_equal 'radiobuttons "a, b" are checked in the "name" group, ' \
111
- 'only one is allowed',
112
- e.message
113
- end
114
-
115
- def test_method_missing_get
116
- page = html_page <<-BODY
117
- <form>
118
- <input name="not_a_method" value="some value">
119
- </form>
120
- BODY
121
-
122
- form = page.forms.first
123
-
124
- assert_equal 'some value', form.not_a_method
125
- end
126
-
127
- def test_method_missing_set
128
- page = html_page <<-BODY
129
- <form>
130
- <input name="not_a_method">
131
- </form>
132
- BODY
133
-
134
- form = page.forms.first
135
-
136
- form.not_a_method = 'some value'
137
-
138
- assert_equal [%w[not_a_method some\ value]], form.build_query
139
- end
140
-
141
- def test_parse_buttons
142
- page = html_page <<-BODY
143
- <form>
144
- <input type="submit" value="submit">
145
- <input type="button" value="submit">
146
- <button type="submit" value="submit">
147
- <button type="button" value="submit">
148
- <input type="image" name="submit" src="/button.jpeg">
149
- <input type="image" src="/button.jpeg">
150
- </form>
151
- BODY
152
-
153
- form = page.forms.first
154
- buttons = form.buttons.sort
155
-
156
- assert buttons.all? { |b| Mechanize::Form::Button === b }
157
-
158
- assert_equal 'submit', buttons.shift.type
159
- assert_equal 'button', buttons.shift.type
160
- assert_equal 'submit', buttons.shift.type
161
- assert_equal 'button', buttons.shift.type
162
- assert_equal 'image', buttons.shift.type
163
- assert_equal 'image', buttons.shift.type
164
-
165
- assert_empty buttons
166
- end
167
-
168
- def test_parse_select
169
- page = html_page <<-BODY
170
- <form>
171
- <select name="multi" multiple></select>
172
- <select name="single"></select>
173
- </form>
174
- BODY
175
-
176
- form = page.forms.first
177
- selects = form.fields.sort
178
-
179
- multi = selects.shift
180
- assert_kind_of Mechanize::Form::MultiSelectList, multi
181
-
182
- single = selects.shift
183
- assert_kind_of Mechanize::Form::SelectList, single
184
-
185
- assert_empty selects
186
- end
187
-
188
- def test_checkboxes_no_input_name
189
- page = @mech.get('http://localhost/form_no_input_name.html')
190
- form = page.forms.first
191
-
192
- assert_equal(0, form.checkboxes.length)
193
- end
194
-
195
- def test_field_with
196
- page = @mech.get("http://localhost/google.html")
197
- search = page.forms.find { |f| f.name == "f" }
198
-
199
- assert(search.field_with(:name => 'q'))
200
- assert(search.field_with(:name => 'hl'))
201
- assert(search.fields.find { |f| f.name == 'ie' })
202
- end
203
-
204
- def test_fields_no_input_name
205
- page = @mech.get('http://localhost/form_no_input_name.html')
206
- form = page.forms.first
207
-
208
- assert_equal(0, form.fields.length)
209
- end
210
-
211
- def test_file_uploads_no_value
212
- page = @mech.get("http://localhost/file_upload.html")
213
- form = page.form('value_test')
214
- assert_nil(form.file_uploads.first.value)
215
- assert_nil(form.file_uploads.first.file_name)
216
- end
217
-
218
- def test_forms_no_input_name
219
- page = @mech.get('http://localhost/form_no_input_name.html')
220
- form = page.forms.first
221
-
222
- assert_equal(0, form.radiobuttons.length)
223
- end
224
-
225
- def test_has_field_eh
226
- refute @form.has_field? 'name'
227
-
228
- @form['name'] = 'Aaron'
229
-
230
- assert_equal true, @form.has_field?('name')
231
- end
232
-
233
- def test_has_value_eh
234
- refute @form.has_value? 'Aaron'
235
-
236
- @form['name'] = 'Aaron'
237
-
238
- assert_equal true, @form.has_value?('Aaron')
239
- end
240
-
241
- def test_keys
242
- assert_empty @form.keys
243
-
244
- @form['name'] = 'Aaron'
245
-
246
- assert_equal %w[name], @form.keys
247
- end
248
-
249
- def test_parse_textarea
250
- form = Nokogiri::HTML <<-FORM
251
- <form>
252
- <textarea name="t">hi</textarea>
253
- </form>
254
- FORM
255
-
256
- form = Mechanize::Form.new form, @mech
257
- textarea = form.fields.first
258
-
259
- assert_kind_of Mechanize::Form::Textarea, textarea
260
- assert_equal 'hi', textarea.value
261
- end
262
-
263
- def test_post_with_rails_3_encoding_hack
264
- page = @mech.get("http://localhost/rails_3_encoding_hack_form_test.html")
265
- form = page.forms.first
266
- form.submit
267
- end
268
-
269
- def test_post_with_blank_encoding
270
- page = @mech.get("http://localhost/form_test.html")
271
- form = page.form('post_form1')
272
- form.page.encoding = nil
273
- form.submit
274
- end
275
-
276
- def test_set_fields_duplicate
277
- page = html_page '<form><input name="a" value="b"><input name="a"></form>'
278
- form = page.forms.first
279
-
280
- form.set_fields :a => 'c'
281
-
282
- assert_equal 'c', form.fields.first.value
283
- assert_equal '', form.fields.last.value
284
- end
285
-
286
- def test_set_fields_none
287
- page = html_page '<form><input name="a" value="b"></form>'
288
- form = page.forms.first
289
-
290
- form.set_fields
291
-
292
- assert_equal 'b', form.fields.first.value
293
- end
294
-
295
- def test_set_fields_many
296
- page = html_page '<form><input name="a" value="b"><input name="b"></form>'
297
- form = page.forms.first
298
-
299
- form.set_fields :a => 'c', :b => 'd'
300
-
301
- assert_equal 'c', form.fields.first.value
302
- assert_equal 'd', form.fields.last.value
303
- end
304
-
305
- def test_set_fields_one
306
- page = html_page '<form><input name="a" value="b"></form>'
307
- form = page.forms.first
308
-
309
- form.set_fields :a => 'c'
310
-
311
- assert_equal 'c', form.fields.first.value
312
- end
313
-
314
- def test_set_fields_position
315
- page = html_page '<form><input name="a" value="b"><input name="a"></form>'
316
- form = page.forms.first
317
-
318
- form.set_fields :a => { 0 => 'c', 1 => 'd' }
319
-
320
- assert_equal 'c', form.fields.first.value
321
- assert_equal 'd', form.fields.last.value
322
- end
323
-
324
- def test_set_fields_position_crappily
325
- page = html_page '<form><input name="a" value="b"><input name="a"></form>'
326
- form = page.forms.first
327
-
328
- form.set_fields :a => ['c', 1]
329
-
330
- assert_equal 'b', form.fields.first.value
331
- assert_equal 'c', form.fields.last.value
332
- end
333
-
334
- def test_values
335
- assert_empty @form.values
336
-
337
- @form['name'] = 'Aaron'
338
-
339
- assert_equal %w[Aaron], @form.values
340
- end
341
-
342
- def test_no_form_action
343
- page = @mech.get('http://localhost:2000/form_no_action.html')
344
- page.forms.first.fields.first.value = 'Aaron'
345
- page = @mech.submit(page.forms.first)
346
- assert_match('/form_no_action.html?first=Aaron', page.uri.to_s)
347
- end
348
-
349
- def test_submit_first_field_wins
350
- page = @mech.get('http://localhost/tc_field_precedence.html')
351
- form = page.forms.first
352
-
353
- assert !form.checkboxes.empty?
354
- assert_equal "1", form.checkboxes.first.value
355
-
356
- submitted = form.submit
357
-
358
- assert_equal 'ticky=1&ticky=0', submitted.parser.at('#query').text
359
- end
360
-
361
- def test_submit_takes_arbitrary_headers
362
- page = @mech.get('http://localhost:2000/form_no_action.html')
363
- assert form = page.forms.first
364
- form.action = '/http_headers'
365
- page = @mech.submit(form, nil, { 'foo' => 'bar' })
366
-
367
- headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
368
- headers = Hash[*headers]
369
-
370
- assert_equal 'bar', headers['foo']
371
- end
372
-
373
- def test_submit_select_default_all
374
- page = html_page <<-BODY
375
- <form name="form1" method="post" action="/form_post">
376
- <select name="list">
377
- <option value="1" selected>Option 1</option>
378
- <option value="2" selected>Option 2</option>
379
- <option value="3" selected>Option 3</option>
380
- <option value="4" selected>Option 4</option>
381
- <option value="5" selected>Option 5</option>
382
- <option value="6" selected>Option 6</option>
383
- </select>
384
- <br />
385
- <input type="submit" value="Submit" />
386
- </form>
387
- BODY
388
-
389
- form = page.forms.first
390
- assert_equal "6", form.list
391
-
392
- page = @mech.submit form
393
- assert_equal 1, page.links.length
394
- assert_equal 1, page.links_with(:text => 'list:6').length
395
- end
396
-
397
- def test_submit_select_default_none
398
- page = html_page <<-BODY
399
- <form name="form1" method="post" action="/form_post">
400
- <select name="list">
401
- <option value="1">Option 1</option>
402
- <option value="2">Option 2</option>
403
- <option>Option No Value</option>
404
- <option value="3">Option 3</option>
405
- <option value="4">Option 4</option>
406
- <option value="5">Option 5</option>
407
- <option value="6">Option 6</option>
408
- </select>
409
- <br />
410
- <input type="submit" value="Submit" />
411
- </form>
412
- BODY
413
-
414
- form = page.forms.first
415
-
416
- assert_equal "1", form.list
417
- page = @mech.submit form
418
-
419
- assert_equal 1, page.links.length
420
- assert_equal 1, page.links_with(:text => 'list:1').length
421
- end
422
-
423
- def test_form_select_default_noopts
424
- page = html_page <<-BODY
425
- <form name="form1" method="post" action="/form_post">
426
- <select name="list">
427
- </select>
428
- <br />
429
- <input type="submit" value="Submit" />
430
- </form>
431
- BODY
432
- form = page.forms.first
433
-
434
- assert form.field 'list'
435
- assert_nil form.list
436
-
437
- page = @mech.submit form
438
-
439
- assert_empty page.links
440
- end
441
-
442
- # Test submitting form with two fields of the same name
443
- def test_post_multival
444
- page = @mech.get("http://localhost/form_multival.html")
445
- form = page.form_with(:name => 'post_form')
446
-
447
- assert_equal(2, form.fields_with(:name => 'first').length)
448
-
449
- form.fields_with(:name => 'first')[0].value = 'Aaron'
450
- form.fields_with(:name => 'first')[1].value = 'Patterson'
451
-
452
- page = @mech.submit(form)
453
-
454
- assert_equal(2, page.links.length)
455
- assert(page.link_with(:text => 'first:Aaron'))
456
- assert(page.link_with(:text => 'first:Patterson'))
457
- end
458
-
459
- # Test calling submit on the form object
460
- def test_submit_on_form
461
- page = @mech.get("http://localhost/form_multival.html")
462
- form = page.form_with(:name => 'post_form')
463
-
464
- assert_equal(2, form.fields_with(:name => 'first').length)
465
-
466
- form.fields_with(:name => 'first')[0].value = 'Aaron'
467
- form.fields_with(:name => 'first')[1].value = 'Patterson'
468
-
469
- page = form.submit
470
-
471
- assert_equal(2, page.links.length)
472
- assert(page.link_with(:text => 'first:Aaron'))
473
- assert(page.link_with(:text => 'first:Patterson'))
474
- end
475
-
476
- # Test submitting form with two fields of the same name
477
- def test_get_multival
478
- page = @mech.get("http://localhost/form_multival.html")
479
- form = page.form_with(:name => 'get_form')
480
-
481
- assert_equal(2, form.fields_with(:name => 'first').length)
482
-
483
- form.fields_with(:name => 'first')[0].value = 'Aaron'
484
- form.fields_with(:name => 'first')[1].value = 'Patterson'
485
-
486
- page = @mech.submit(form)
487
-
488
- assert_equal(2, page.links.length)
489
- assert(page.link_with(:text => 'first:Aaron'))
490
- assert(page.link_with(:text => 'first:Patterson'))
491
- end
492
-
493
- def test_post_with_non_strings
494
- page = @mech.get("http://localhost/form_test.html")
495
- page.form('post_form1') do |form|
496
- form.first_name = 10
497
- end.submit
498
- end
499
-
500
- def test_post_with_bang
501
- page = @mech.get("http://localhost/form_test.html")
502
- page.form_with!(:name => 'post_form1') do |form|
503
- form.first_name = 10
504
- end.submit
505
- end
506
-
507
- def test_post
508
- page = @mech.get("http://localhost/form_test.html")
509
- post_form = page.forms.find { |f| f.name == "post_form1" }
510
-
511
- assert_equal("post", post_form.method.downcase)
512
- assert_equal("/form_post", post_form.action)
513
-
514
- assert_equal(3, post_form.fields.size)
515
-
516
- assert_equal(1, post_form.buttons.size)
517
- assert_equal(2, post_form.radiobuttons.size)
518
- assert_equal(3, post_form.checkboxes.size)
519
- assert(post_form.fields.find { |f| f.name == "first_name" },
520
- "First name field was nil")
521
- assert(post_form.fields.find { |f| f.name == "country" },
522
- "Country field was nil")
523
- assert(post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
524
- "Gender male button was nil")
525
-
526
- assert(post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
527
- "Gender female button was nil")
528
-
529
- assert(post_form.checkboxes.find { |f| f.name == "cool person" },
530
- "couldn't find cool person checkbox")
531
- assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
532
- "couldn't find likes ham checkbox")
533
- assert(post_form.checkboxes.find { |f| f.name == "green[eggs]" },
534
- "couldn't find green[eggs] checkbox")
535
-
536
- # Find the select list
537
- s = post_form.fields.find { |f| f.name == "country" }
538
- assert_equal(2, s.options.length)
539
- assert_equal("USA", s.value)
540
- assert_equal("USA", s.options.first.value)
541
- assert_equal("USA", s.options.first.text)
542
- assert_equal("CANADA", s.options[1].value)
543
- assert_equal("CANADA", s.options[1].text)
544
-
545
- # Now set all the fields
546
- post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
547
- post_form.radiobuttons.find { |f|
548
- f.name == "gender" && f.value == "male"
549
- }.checked = true
550
- post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
551
- post_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
552
- page = @mech.submit(post_form, post_form.buttons.first)
553
-
554
- # Check that the submitted fields exist
555
- assert_equal(5, page.links.size, "Not enough links")
556
- assert(page.links.find { |l| l.text == "likes ham:on" },
557
- "likes ham check box missing")
558
- assert(page.links.find { |l| l.text == "green[eggs]:on" },
559
- "green[eggs] check box missing")
560
- assert(page.links.find { |l| l.text == "first_name:Aaron" },
561
- "first_name field missing")
562
- assert(page.links.find { |l| l.text == "gender:male" },
563
- "gender field missing")
564
- assert(page.links.find { |l| l.text == "country:USA" },
565
- "select box not submitted")
566
- end
567
-
568
- def test_post_multipart
569
- page = @mech.get("http://localhost/form_test.html")
570
- post_form = page.forms.find { |f| f.name == "post_form4_multipart" }
571
- assert(post_form, "Post form is null")
572
- assert_equal("post", post_form.method.downcase)
573
- assert_equal("/form_post", post_form.action)
574
-
575
- assert_equal(1, post_form.fields.size)
576
- assert_equal(1, post_form.buttons.size)
577
-
578
- page = @mech.submit(post_form, post_form.buttons.first)
579
-
580
- assert page
581
- end
582
-
583
- def test_select_box
584
- page = @mech.get("http://localhost/form_test.html")
585
- post_form = page.forms.find { |f| f.name == "post_form1" }
586
-
587
- assert(page.header)
588
- assert(page.root)
589
- assert_equal(0, page.iframes.length)
590
- assert_equal("post", post_form.method.downcase)
591
- assert_equal("/form_post", post_form.action)
592
-
593
- # Find the select list
594
- s = post_form.field_with(:name => /country/)
595
-
596
- assert_equal(2, s.options.length)
597
- assert_equal("USA", s.value)
598
- assert_equal("USA", s.options.first.value)
599
- assert_equal("USA", s.options.first.text)
600
- assert_equal("CANADA", s.options[1].value)
601
- assert_equal("CANADA", s.options[1].text)
602
-
603
- # Now set all the fields
604
- post_form.field_with(:name => /country/).value = s.options[1]
605
- assert_equal('CANADA', post_form.country)
606
- page = @mech.submit(post_form, post_form.buttons.first)
607
-
608
- # Check that the submitted fields exist
609
- assert(page.links.find { |l| l.text == "country:CANADA" },
610
- "select box not submitted")
611
- end
612
-
613
- def test_get
614
- page = @mech.get("http://localhost/form_test.html")
615
- get_form = page.forms.find { |f| f.name == "get_form1" }
616
-
617
- assert_equal("get", get_form.method.downcase)
618
- assert_equal("/form_post", get_form.action)
619
- assert_equal(1, get_form.fields.size)
620
- assert_equal(2, get_form.buttons.size)
621
- assert_equal(2, get_form.radiobuttons.size)
622
- assert_equal(3, get_form.checkboxes.size)
623
- assert(get_form.fields.find { |f| f.name == "first_name" },
624
- "First name field was nil")
625
- assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
626
- "Gender male button was nil")
627
-
628
- assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
629
- "Gender female button was nil")
630
-
631
- assert(get_form.checkboxes.find { |f| f.name == "cool person" },
632
- "couldn't find cool person checkbox")
633
- assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
634
- "couldn't find likes ham checkbox")
635
- assert(get_form.checkboxes.find { |f| f.name == "green[eggs]" },
636
- "couldn't find green[eggs] checkbox")
637
-
638
- # Set up the image button
639
- img = get_form.buttons.find { |f| f.name == "button" }
640
- img.x = "9"
641
- img.y = "10"
642
- # Now set all the fields
643
- get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
644
- get_form.radiobuttons.find { |f|
645
- f.name == "gender" && f.value == "male"
646
- }.checked = true
647
- get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
648
- get_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
649
- page = @mech.submit(get_form, get_form.buttons.first)
650
-
651
- # Check that the submitted fields exist
652
- assert_equal(6, page.links.size, "Not enough links")
653
- assert(page.links.find { |l| l.text == "likes ham:on" },
654
- "likes ham check box missing")
655
- assert(page.links.find { |l| l.text == "green[eggs]:on" },
656
- "green[eggs] check box missing")
657
- assert(page.links.find { |l| l.text == "first_name:Aaron" },
658
- "first_name field missing")
659
- assert(page.links.find { |l| l.text == "gender:male" },
660
- "gender field missing")
661
- assert(page.links.find { |l| l.text == "button.y:10" },
662
- "Image button missing")
663
- assert(page.links.find { |l| l.text == "button.x:9" },
664
- "Image button missing")
665
- end
666
-
667
- def test_reset
668
- page = @mech.get("http://localhost/form_test.html")
669
- get_form = page.forms.find { |f| f.name == "get_form1" }
670
-
671
- image_button = get_form.buttons.first
672
- submit_button = get_form.submits.first
673
-
674
- new_page = @mech.submit(get_form, submit_button)
675
- assert_equal "http://localhost/form_post?first_name=", new_page.uri.to_s
676
-
677
- new_page = @mech.submit(get_form, image_button)
678
- assert_equal "http://localhost/form_post?first_name=&button.x=0&button.y=0", new_page.uri.to_s
679
-
680
- get_form.reset
681
-
682
- new_page = @mech.submit(get_form, submit_button)
683
- assert_equal "http://localhost/form_post?first_name=", new_page.uri.to_s
684
- end
685
-
686
- def test_post_with_space_in_action
687
- page = @mech.get("http://localhost/form_test.html")
688
- post_form = page.forms.find { |f| f.name == "post_form2" }
689
-
690
- assert_equal("post", post_form.method.downcase)
691
- assert_equal("/form post", post_form.action)
692
- assert_equal(1, post_form.fields.size)
693
- assert_equal(1, post_form.buttons.size)
694
- assert_equal(2, post_form.radiobuttons.size)
695
- assert_equal(2, post_form.checkboxes.size)
696
- assert(post_form.fields.find { |f| f.name == "first_name" },
697
- "First name field was nil")
698
- assert(post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
699
- "Gender male button was nil")
700
-
701
- assert(post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
702
- "Gender female button was nil")
703
-
704
- assert(post_form.checkboxes.find { |f| f.name == "cool person" },
705
- "couldn't find cool person checkbox")
706
- assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
707
- "couldn't find likes ham checkbox")
708
-
709
- # Now set all the fields
710
- post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
711
- post_form.radiobuttons.find { |f|
712
- f.name == "gender" && f.value == "male"
713
- }.checked = true
714
- post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
715
- page = @mech.submit(post_form, post_form.buttons.first)
716
-
717
- # Check that the submitted fields exist
718
- assert_equal(3, page.links.size, "Not enough links")
719
- assert(page.links.find { |l| l.text == "likes ham:on" },
720
- "likes ham check box missing")
721
- assert(page.links.find { |l| l.text == "first_name:Aaron" },
722
- "first_name field missing")
723
- assert(page.links.find { |l| l.text == "gender:male" },
724
- "gender field missing")
725
- end
726
-
727
- def test_get_with_space_in_action
728
- page = @mech.get("http://localhost/form_test.html")
729
- get_form = page.forms.find { |f| f.name == "get_form2" }
730
-
731
- assert_equal("get", get_form.method.downcase)
732
- assert_equal("/form post", get_form.action)
733
- assert_equal(1, get_form.fields.size)
734
- assert_equal(1, get_form.buttons.size)
735
- assert_equal(2, get_form.radiobuttons.size)
736
- assert_equal(2, get_form.checkboxes.size)
737
- assert(get_form.fields.find { |f| f.name == "first_name" },
738
- "First name field was nil")
739
- assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
740
- "Gender male button was nil")
741
-
742
- assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
743
- "Gender female button was nil")
744
-
745
- assert(get_form.checkboxes.find { |f| f.name == "cool person" },
746
- "couldn't find cool person checkbox")
747
- assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
748
- "couldn't find likes ham checkbox")
749
-
750
- # Now set all the fields
751
- get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
752
- get_form.radiobuttons.find { |f|
753
- f.name == "gender" && f.value == "male"
754
- }.checked = true
755
- get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
756
- page = @mech.submit(get_form, get_form.buttons.first)
757
-
758
- # Check that the submitted fields exist
759
- assert_equal(3, page.links.size, "Not enough links")
760
- assert(page.links.find { |l| l.text == "likes ham:on" },
761
- "likes ham check box missing")
762
- assert(page.links.find { |l| l.text == "first_name:Aaron" },
763
- "first_name field missing")
764
- assert(page.links.find { |l| l.text == "gender:male" },
765
- "gender field missing")
766
- end
767
-
768
- def test_post_with_param_in_action
769
- page = @mech.get("http://localhost/form_test.html")
770
- post_form = page.forms.find { |f| f.name == "post_form3" }
771
-
772
- assert_equal("post", post_form.method.downcase)
773
- assert_equal("/form_post?great day=yes&one=two", post_form.action)
774
- assert_equal(1, post_form.fields.size)
775
- assert_equal(1, post_form.buttons.size)
776
- assert_equal(2, post_form.radiobuttons.size)
777
- assert_equal(2, post_form.checkboxes.size)
778
-
779
- assert(post_form.fields.find { |f| f.name == "first_name" },
780
- "First name field was nil")
781
-
782
- male_button = post_form.radiobuttons.find { |f|
783
- f.name == "gender" && f.value == "male"
784
- }
785
- assert(male_button, "Gender male button was nil")
786
-
787
- female_button = post_form.radiobuttons.find { |f|
788
- f.name == "gender" && f.value == "female"
789
- }
790
-
791
- assert(female_button, "Gender female button was nil")
792
-
793
- assert(post_form.checkbox_with(:name => "cool person"),
794
- "couldn't find cool person checkbox")
795
-
796
- assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
797
- "couldn't find likes ham checkbox")
798
-
799
- # Now set all the fields
800
- post_form.field_with(:name => 'first_name').value = "Aaron"
801
- post_form.radiobuttons.find { |f|
802
- f.name == "gender" && f.value == "male"
803
- }.checked = true
804
- post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
805
-
806
- page = @mech.submit(post_form, post_form.buttons.first)
807
-
808
- # Check that the submitted fields exist
809
- assert_equal(3, page.links.size, "Not enough links")
810
-
811
- assert(page.links.find { |l| l.text == "likes ham:on" },
812
- "likes ham check box missing")
813
- assert(page.links.find { |l| l.text == "first_name:Aaron" },
814
- "first_name field missing")
815
- assert(page.links.find { |l| l.text == "gender:male" },
816
- "gender field missing")
817
- end
818
-
819
- def test_get_with_param_in_action
820
- page = @mech.get("http://localhost/form_test.html")
821
- get_form = page.forms.find { |f| f.name == "get_form3" }
822
-
823
- assert_equal("get", get_form.method.downcase)
824
- assert_equal("/form_post?great day=yes&one=two", get_form.action)
825
- assert_equal(1, get_form.fields.size)
826
- assert_equal(1, get_form.buttons.size)
827
- assert_equal(2, get_form.radiobuttons.size)
828
- assert_equal(2, get_form.checkboxes.size)
829
- assert(get_form.fields.find { |f| f.name == "first_name" },
830
- "First name field was nil")
831
- assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
832
- "Gender male button was nil")
833
-
834
- assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
835
- "Gender female button was nil")
836
-
837
- assert(get_form.checkboxes.find { |f| f.name == "cool person" },
838
- "couldn't find cool person checkbox")
839
- assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
840
- "couldn't find likes ham checkbox")
841
-
842
- # Now set all the fields
843
- get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
844
- get_form.radiobuttons.find { |f|
845
- f.name == "gender" && f.value == "male"
846
- }.checked = true
847
- get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
848
- page = @mech.submit(get_form, get_form.buttons.first)
849
- # Check that the submitted fields exist
850
- assert_equal(3, page.links.size, "Not enough links")
851
- assert(page.links.find { |l| l.text == "likes ham:on" },
852
- "likes ham check box missing")
853
- assert(page.links.find { |l| l.text == "first_name:Aaron" },
854
- "first_name field missing")
855
- assert(page.links.find { |l| l.text == "gender:male" },
856
- "gender field missing")
857
- end
858
-
859
- def test_field_addition
860
- page = @mech.get("http://localhost/form_test.html")
861
- get_form = page.forms.find { |f| f.name == "get_form1" }
862
- get_form.field("first_name").value = "Gregory"
863
- assert_equal( "Gregory", get_form.field("first_name").value )
864
- end
865
-
866
- def test_fields_as_accessors
867
- page = @mech.get("http://localhost/form_multival.html")
868
- form = page.form_with(:name => 'post_form')
869
-
870
- assert_equal(2, form.fields_with(:name => 'first').length)
871
-
872
- form.first = 'Aaron'
873
- assert_equal('Aaron', form.first)
874
- end
875
-
876
- def test_form_and_fields_dom_id
877
- # blatant copypasta of test above
878
- page = @mech.get("http://localhost/form_test.html")
879
- form = page.form_with(dom_id: 'generic_form')
880
-
881
- assert_equal(1, form.fields_with(dom_id: 'name_first').length)
882
- assert_equal('first_name', form.field_with(dom_id: 'name_first').name)
883
-
884
- assert_equal(form, page.form_with(id: 'generic_form'))
885
- assert_equal(form, page.form_with(css: '#generic_form'))
886
-
887
- fields_by_dom_id = form.fields_with(dom_id: 'name_first')
888
- assert_equal(fields_by_dom_id, form.fields_with(id: 'name_first'))
889
- assert_equal(fields_by_dom_id, form.fields_with(css: '#name_first'))
890
- assert_equal(fields_by_dom_id, form.fields_with(xpath: '//*[@id="name_first"]'))
891
- assert_equal(fields_by_dom_id, form.fields_with(search: '//*[@id="name_first"]'))
892
- end
893
-
894
- def test_form_and_fields_dom_class
895
- # blatant copypasta of test above
896
- page = @mech.get("http://localhost/form_test.html")
897
- form = page.form_with(:dom_class => 'really_generic_form')
898
- form_by_class = page.form_with(:class => 'really_generic_form')
899
-
900
- assert_equal(1, form.fields_with(:dom_class => 'text_input').length)
901
- assert_equal('first_name', form.field_with(:dom_class => 'text_input').name)
902
-
903
- # *_with(:class => blah) should work exactly like (:dom_class => blah)
904
- assert_equal(form, form_by_class)
905
- assert_equal(form.fields_with(:dom_class => 'text_input'), form.fields_with(:class => 'text_input'))
906
- end
907
-
908
- def test_add_field
909
- page = @mech.get("http://localhost/form_multival.html")
910
- form = page.form_with(:name => 'post_form')
911
-
912
- number_of_fields = form.fields.length
913
-
914
- assert form.add_field!('intarweb')
915
- assert_equal(number_of_fields + 1, form.fields.length)
916
- end
917
-
918
- def test_delete_field
919
- page = @mech.get("http://localhost/form_multival.html")
920
- form = page.form_with(:name => 'post_form')
921
-
922
- number_of_fields = form.fields.length
923
- assert_equal 2, number_of_fields
924
-
925
- form.delete_field!('first')
926
- assert_nil(form['first'])
927
- assert_equal(number_of_fields - 2, form.fields.length)
928
- end
929
-
930
- def test_has_field
931
- page = @mech.get("http://localhost/form_multival.html")
932
- form = page.form_with(:name => 'post_form')
933
-
934
- assert_equal false, form.has_field?('intarweb')
935
- assert form.add_field!('intarweb')
936
- assert_equal true, form.has_field?('intarweb')
937
- end
938
-
939
- def test_fill_unexisting_form
940
- page = @mech.get("http://localhost/empty_form.html")
941
- assert_raises(NoMethodError) {
942
- page.form_with(:name => 'no form') { |f| f.foo = 'bar' }
943
- }
944
- begin
945
- page.form_with!(:name => 'no form') { |f| f.foo = 'bar' }
946
- rescue => e
947
- assert_instance_of Mechanize::ElementNotFoundError, e
948
- assert_kind_of Mechanize::Page, e.source
949
- assert_equal :form, e.element
950
- assert_kind_of Hash, e.conditions
951
- assert_equal 'no form', e.conditions[:name]
952
- end
953
- end
954
-
955
- def test_field_error
956
- @page = @mech.get('http://localhost/empty_form.html')
957
- form = @page.forms.first
958
- assert_raises(NoMethodError) {
959
- form.foo = 'asdfasdf'
960
- }
961
-
962
- assert_raises(NoMethodError) {
963
- form.foo
964
- }
965
- end
966
-
967
- def test_form_build_query
968
- page = @mech.get("http://localhost/form_order_test.html")
969
- get_form = page.forms.first
970
-
971
- query = get_form.build_query
972
-
973
- expected = [
974
- %w[1 RADIO],
975
- %w[3 nobody@example],
976
- %w[2 TEXT],
977
- %w[3 2011-10],
978
- ]
979
-
980
- assert_equal expected, query
981
- end
982
-
983
- def test_form_input_disabled
984
- page = html_page <<-BODY
985
- <form name="form1" method="post" action="/form_post">
986
- <input type="text" name="opa" value="omg" disabled />
987
- <input type="submit" value="Submit" />
988
- </form>
989
- BODY
990
- form = page.forms.first
991
-
992
- page = @mech.submit form
993
-
994
- assert_empty page.links
995
- end
996
-
997
- def test_form_built_from_array_post
998
- submitted = @mech.post(
999
- 'http://example/form_post',
1000
- [
1001
- %w[order_matters 0],
1002
- %w[order_matters 1],
1003
- %w[order_matters 2],
1004
- %w[mess_it_up asdf]
1005
- ]
1006
- )
1007
-
1008
- assert_equal 'order_matters=0&order_matters=1&order_matters=2&mess_it_up=asdf', submitted.parser.at('#query').text
1009
- end
1010
-
1011
- def test_form_built_from_hashes_submit
1012
- uri = URI 'http://example/form_post'
1013
- page = page uri
1014
- form = Mechanize::Form.new node('form', 'name' => @NAME, 'method' => 'POST'), @mech, page
1015
- form.fields << Mechanize::Form::Field.new({'name' => 'order_matters'}, '0')
1016
- form.fields << Mechanize::Form::Field.new({'name' => 'order_matters'}, '1')
1017
- form.fields << Mechanize::Form::Field.new({'name' => 'order_matters'}, '2')
1018
- form.fields << Mechanize::Form::Field.new({'name' => 'mess_it_up'}, 'asdf')
1019
-
1020
- submitted = @mech.submit form
1021
-
1022
- assert_equal 'order_matters=0&order_matters=1&order_matters=2&mess_it_up=asdf', submitted.parser.at('#query').text
1023
- end
1024
-
1025
- end