diamond-mechanize 2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. data/CHANGELOG.rdoc +718 -0
  2. data/EXAMPLES.rdoc +187 -0
  3. data/FAQ.rdoc +11 -0
  4. data/GUIDE.rdoc +163 -0
  5. data/LICENSE.rdoc +20 -0
  6. data/Manifest.txt +159 -0
  7. data/README.rdoc +64 -0
  8. data/Rakefile +49 -0
  9. data/lib/mechanize.rb +1079 -0
  10. data/lib/mechanize/content_type_error.rb +13 -0
  11. data/lib/mechanize/cookie.rb +232 -0
  12. data/lib/mechanize/cookie_jar.rb +194 -0
  13. data/lib/mechanize/download.rb +59 -0
  14. data/lib/mechanize/element_matcher.rb +36 -0
  15. data/lib/mechanize/file.rb +65 -0
  16. data/lib/mechanize/file_connection.rb +17 -0
  17. data/lib/mechanize/file_request.rb +26 -0
  18. data/lib/mechanize/file_response.rb +74 -0
  19. data/lib/mechanize/file_saver.rb +39 -0
  20. data/lib/mechanize/form.rb +543 -0
  21. data/lib/mechanize/form/button.rb +6 -0
  22. data/lib/mechanize/form/check_box.rb +12 -0
  23. data/lib/mechanize/form/field.rb +54 -0
  24. data/lib/mechanize/form/file_upload.rb +21 -0
  25. data/lib/mechanize/form/hidden.rb +3 -0
  26. data/lib/mechanize/form/image_button.rb +19 -0
  27. data/lib/mechanize/form/keygen.rb +34 -0
  28. data/lib/mechanize/form/multi_select_list.rb +94 -0
  29. data/lib/mechanize/form/option.rb +50 -0
  30. data/lib/mechanize/form/radio_button.rb +55 -0
  31. data/lib/mechanize/form/reset.rb +3 -0
  32. data/lib/mechanize/form/select_list.rb +44 -0
  33. data/lib/mechanize/form/submit.rb +3 -0
  34. data/lib/mechanize/form/text.rb +3 -0
  35. data/lib/mechanize/form/textarea.rb +3 -0
  36. data/lib/mechanize/headers.rb +23 -0
  37. data/lib/mechanize/history.rb +82 -0
  38. data/lib/mechanize/http.rb +8 -0
  39. data/lib/mechanize/http/agent.rb +1004 -0
  40. data/lib/mechanize/http/auth_challenge.rb +59 -0
  41. data/lib/mechanize/http/auth_realm.rb +31 -0
  42. data/lib/mechanize/http/content_disposition_parser.rb +188 -0
  43. data/lib/mechanize/http/www_authenticate_parser.rb +155 -0
  44. data/lib/mechanize/monkey_patch.rb +16 -0
  45. data/lib/mechanize/page.rb +440 -0
  46. data/lib/mechanize/page/base.rb +7 -0
  47. data/lib/mechanize/page/frame.rb +27 -0
  48. data/lib/mechanize/page/image.rb +30 -0
  49. data/lib/mechanize/page/label.rb +20 -0
  50. data/lib/mechanize/page/link.rb +98 -0
  51. data/lib/mechanize/page/meta_refresh.rb +68 -0
  52. data/lib/mechanize/parser.rb +173 -0
  53. data/lib/mechanize/pluggable_parsers.rb +144 -0
  54. data/lib/mechanize/redirect_limit_reached_error.rb +19 -0
  55. data/lib/mechanize/redirect_not_get_or_head_error.rb +21 -0
  56. data/lib/mechanize/response_code_error.rb +21 -0
  57. data/lib/mechanize/response_read_error.rb +27 -0
  58. data/lib/mechanize/robots_disallowed_error.rb +28 -0
  59. data/lib/mechanize/test_case.rb +663 -0
  60. data/lib/mechanize/unauthorized_error.rb +3 -0
  61. data/lib/mechanize/unsupported_scheme_error.rb +6 -0
  62. data/lib/mechanize/util.rb +101 -0
  63. data/test/data/htpasswd +1 -0
  64. data/test/data/server.crt +16 -0
  65. data/test/data/server.csr +12 -0
  66. data/test/data/server.key +15 -0
  67. data/test/data/server.pem +15 -0
  68. data/test/htdocs/alt_text.html +10 -0
  69. data/test/htdocs/bad_form_test.html +9 -0
  70. data/test/htdocs/button.jpg +0 -0
  71. data/test/htdocs/canonical_uri.html +9 -0
  72. data/test/htdocs/dir with spaces/foo.html +1 -0
  73. data/test/htdocs/empty_form.html +6 -0
  74. data/test/htdocs/file_upload.html +26 -0
  75. data/test/htdocs/find_link.html +41 -0
  76. data/test/htdocs/form_multi_select.html +16 -0
  77. data/test/htdocs/form_multival.html +37 -0
  78. data/test/htdocs/form_no_action.html +18 -0
  79. data/test/htdocs/form_no_input_name.html +16 -0
  80. data/test/htdocs/form_order_test.html +11 -0
  81. data/test/htdocs/form_select.html +16 -0
  82. data/test/htdocs/form_set_fields.html +14 -0
  83. data/test/htdocs/form_test.html +188 -0
  84. data/test/htdocs/frame_referer_test.html +10 -0
  85. data/test/htdocs/frame_test.html +30 -0
  86. data/test/htdocs/google.html +13 -0
  87. data/test/htdocs/index.html +6 -0
  88. data/test/htdocs/link with space.html +5 -0
  89. data/test/htdocs/meta_cookie.html +11 -0
  90. data/test/htdocs/no_title_test.html +6 -0
  91. data/test/htdocs/noindex.html +9 -0
  92. data/test/htdocs/rails_3_encoding_hack_form_test.html +27 -0
  93. data/test/htdocs/relative/tc_relative_links.html +21 -0
  94. data/test/htdocs/robots.html +8 -0
  95. data/test/htdocs/robots.txt +2 -0
  96. data/test/htdocs/tc_bad_charset.html +9 -0
  97. data/test/htdocs/tc_bad_links.html +5 -0
  98. data/test/htdocs/tc_base_link.html +8 -0
  99. data/test/htdocs/tc_blank_form.html +11 -0
  100. data/test/htdocs/tc_charset.html +6 -0
  101. data/test/htdocs/tc_checkboxes.html +19 -0
  102. data/test/htdocs/tc_encoded_links.html +5 -0
  103. data/test/htdocs/tc_field_precedence.html +11 -0
  104. data/test/htdocs/tc_follow_meta.html +8 -0
  105. data/test/htdocs/tc_form_action.html +48 -0
  106. data/test/htdocs/tc_links.html +19 -0
  107. data/test/htdocs/tc_meta_in_body.html +9 -0
  108. data/test/htdocs/tc_pretty_print.html +17 -0
  109. data/test/htdocs/tc_referer.html +16 -0
  110. data/test/htdocs/tc_relative_links.html +19 -0
  111. data/test/htdocs/tc_textarea.html +23 -0
  112. data/test/htdocs/test_click.html +11 -0
  113. data/test/htdocs/unusual______.html +5 -0
  114. data/test/test_mechanize.rb +1164 -0
  115. data/test/test_mechanize_cookie.rb +451 -0
  116. data/test/test_mechanize_cookie_jar.rb +483 -0
  117. data/test/test_mechanize_download.rb +43 -0
  118. data/test/test_mechanize_file.rb +61 -0
  119. data/test/test_mechanize_file_connection.rb +21 -0
  120. data/test/test_mechanize_file_request.rb +19 -0
  121. data/test/test_mechanize_file_saver.rb +21 -0
  122. data/test/test_mechanize_form.rb +875 -0
  123. data/test/test_mechanize_form_check_box.rb +38 -0
  124. data/test/test_mechanize_form_encoding.rb +114 -0
  125. data/test/test_mechanize_form_field.rb +63 -0
  126. data/test/test_mechanize_form_file_upload.rb +20 -0
  127. data/test/test_mechanize_form_image_button.rb +12 -0
  128. data/test/test_mechanize_form_keygen.rb +32 -0
  129. data/test/test_mechanize_form_multi_select_list.rb +84 -0
  130. data/test/test_mechanize_form_option.rb +55 -0
  131. data/test/test_mechanize_form_radio_button.rb +78 -0
  132. data/test/test_mechanize_form_select_list.rb +76 -0
  133. data/test/test_mechanize_form_textarea.rb +52 -0
  134. data/test/test_mechanize_headers.rb +35 -0
  135. data/test/test_mechanize_history.rb +103 -0
  136. data/test/test_mechanize_http_agent.rb +1225 -0
  137. data/test/test_mechanize_http_auth_challenge.rb +39 -0
  138. data/test/test_mechanize_http_auth_realm.rb +49 -0
  139. data/test/test_mechanize_http_content_disposition_parser.rb +118 -0
  140. data/test/test_mechanize_http_www_authenticate_parser.rb +146 -0
  141. data/test/test_mechanize_link.rb +80 -0
  142. data/test/test_mechanize_page.rb +118 -0
  143. data/test/test_mechanize_page_encoding.rb +182 -0
  144. data/test/test_mechanize_page_frame.rb +16 -0
  145. data/test/test_mechanize_page_link.rb +390 -0
  146. data/test/test_mechanize_page_meta_refresh.rb +127 -0
  147. data/test/test_mechanize_parser.rb +289 -0
  148. data/test/test_mechanize_pluggable_parser.rb +52 -0
  149. data/test/test_mechanize_redirect_limit_reached_error.rb +24 -0
  150. data/test/test_mechanize_redirect_not_get_or_head_error.rb +14 -0
  151. data/test/test_mechanize_subclass.rb +22 -0
  152. data/test/test_mechanize_util.rb +103 -0
  153. data/test/test_multi_select.rb +119 -0
  154. metadata +216 -0
@@ -0,0 +1,43 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeDownload < Mechanize::TestCase
4
+
5
+ def setup
6
+ super
7
+
8
+ @parser = Mechanize::Download
9
+ end
10
+
11
+ def test_save_string_io
12
+ uri = URI.parse 'http://example/foo.html'
13
+ body_io = StringIO.new '0123456789'
14
+
15
+ download = @parser.new uri, nil, body_io
16
+
17
+ in_tmpdir do
18
+ download.save
19
+
20
+ assert File.exist? 'foo.html'
21
+ end
22
+ end
23
+
24
+ def test_save_tempfile
25
+ uri = URI.parse 'http://example/foo.html'
26
+ Tempfile.new __name__ do |body_io|
27
+ body_io.write '0123456789'
28
+
29
+ body_io.flush
30
+ body_io.rewind
31
+
32
+ download = @parser.new uri, nil, body_io
33
+
34
+ in_tmpdir do
35
+ download.save
36
+
37
+ assert File.exist? 'foo.html'
38
+ end
39
+ end
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,61 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeFile < Mechanize::TestCase
4
+
5
+ def setup
6
+ super
7
+
8
+ @parser = Mechanize::File
9
+ end
10
+
11
+ def test_save
12
+ uri = URI 'http://example/name.html'
13
+ page = Mechanize::File.new uri, nil, '0123456789'
14
+
15
+ Dir.mktmpdir do |dir|
16
+ Dir.chdir dir do
17
+ page.save 'test.html'
18
+
19
+ assert_equal '0123456789', File.read('test.html')
20
+ end
21
+ end
22
+ end
23
+
24
+ def test_save_default
25
+ uri = URI 'http://example/test.html'
26
+ page = Mechanize::File.new uri, nil, ''
27
+
28
+ Dir.mktmpdir do |dir|
29
+ Dir.chdir dir do
30
+ page.save
31
+
32
+ assert File.exist? 'test.html'
33
+
34
+ page.save
35
+
36
+ assert File.exist? 'test.html.1'
37
+
38
+ page.save
39
+
40
+ assert File.exist? 'test.html.2'
41
+ end
42
+ end
43
+ end
44
+
45
+ def test_save_default_dots
46
+ uri = URI 'http://localhost/../test.html'
47
+ page = Mechanize::File.new uri, nil, ''
48
+
49
+ Dir.mktmpdir do |dir|
50
+ Dir.chdir dir do
51
+ page.save
52
+ assert File.exist? 'test.html'
53
+
54
+ page.save
55
+ assert File.exist? 'test.html.1'
56
+ end
57
+ end
58
+ end
59
+
60
+ end
61
+
@@ -0,0 +1,21 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeFileConnection < Mechanize::TestCase
4
+
5
+ def test_request
6
+ uri = URI.parse "file://#{File.expand_path __FILE__}"
7
+ conn = Mechanize::FileConnection.new
8
+
9
+ body = ''
10
+
11
+ conn.request uri, nil do |response|
12
+ response.read_body do |part|
13
+ body << part
14
+ end
15
+ end
16
+
17
+ assert_equal File.read(__FILE__), body
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,19 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeFileRequest < Mechanize::TestCase
4
+
5
+ def test_initialize
6
+ uri = URI.parse 'http://example/'
7
+
8
+ r = Mechanize::FileRequest.new uri
9
+
10
+ assert_equal uri, r.uri
11
+ assert_equal '/', r.path
12
+
13
+ assert_respond_to r, :[]=
14
+ assert_respond_to r, :add_field
15
+ assert_respond_to r, :each_header
16
+ end
17
+
18
+ end
19
+
@@ -0,0 +1,21 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeFileSaver < Mechanize::TestCase
4
+
5
+ def setup
6
+ super
7
+
8
+ @uri = URI 'http://example'
9
+ @io = StringIO.new 'hello world'
10
+ end
11
+
12
+ def test_initialize
13
+ in_tmpdir do
14
+ Mechanize::FileSaver.new @uri, nil, @io, 200
15
+
16
+ assert File.exist? 'example/index.html'
17
+ end
18
+ end
19
+
20
+ end
21
+
@@ -0,0 +1,875 @@
1
+ require 'mechanize/test_case'
2
+
3
+ class TestMechanizeForm < Mechanize::TestCase
4
+
5
+ def setup
6
+ super
7
+
8
+ @form = Mechanize::Form.new node 'form'
9
+ end
10
+
11
+ def test_action
12
+ form = Mechanize::Form.new node('form', 'action' => '?a=b&amp;b=c')
13
+
14
+ assert_equal '?a=b&b=c', form.action
15
+ end
16
+
17
+ def test_aset
18
+ assert_empty @form.keys
19
+
20
+ @form['intarweb'] = 'Aaron'
21
+
22
+ assert_equal 'Aaron', @form['intarweb']
23
+ end
24
+
25
+ def test_aset_exists
26
+ page = html_page <<-BODY
27
+ <title>Page Title</title>
28
+ <form name="post_form">
29
+ <input name="first" type="text" id="name_first">
30
+ <input name="first" type="text">
31
+ <input type="submit" value="Submit">
32
+ </form>
33
+ BODY
34
+
35
+ form = page.form_with(:name => 'post_form')
36
+
37
+ assert_equal %w[first first], form.keys
38
+
39
+ form['first'] = 'Aaron'
40
+
41
+ assert_equal 'Aaron', form['first']
42
+ assert_equal ['Aaron', ''], form.values
43
+ end
44
+
45
+ def test_build_query_blank_form
46
+ page = @mech.get('http://localhost/tc_blank_form.html')
47
+ form = page.forms.first
48
+ query = form.build_query
49
+ assert(query.length > 0)
50
+ assert query.all? { |x| x[1] == '' }
51
+ end
52
+
53
+ def test_method_missing_get
54
+ page = html_page <<-BODY
55
+ <form>
56
+ <input name="not_a_method" value="some value">
57
+ </form>
58
+ BODY
59
+
60
+ form = page.forms.first
61
+
62
+ assert_equal 'some value', form.not_a_method
63
+ end
64
+
65
+ def test_method_missing_set
66
+ page = html_page <<-BODY
67
+ <form>
68
+ <input name="not_a_method">
69
+ </form>
70
+ BODY
71
+
72
+ form = page.forms.first
73
+
74
+ form.not_a_method = 'some value'
75
+
76
+ assert_equal [%w[not_a_method some\ value]], form.build_query
77
+ end
78
+
79
+ def test_parse_buttons
80
+ page = html_page <<-BODY
81
+ <form>
82
+ <input type="submit" value="submit">
83
+ <input type="button" value="submit">
84
+ <button type="submit" value="submit">
85
+ <button type="button" value="submit">
86
+ <input type="image" name="submit" src="/button.jpeg">
87
+ <input type="image" src="/button.jpeg">
88
+ </form>
89
+ BODY
90
+
91
+ form = page.forms.first
92
+ buttons = form.buttons.sort
93
+
94
+ assert buttons.all? { |b| Mechanize::Form::Button === b }
95
+
96
+ assert_equal 'submit', buttons.shift.type
97
+ assert_equal 'button', buttons.shift.type
98
+ assert_equal 'submit', buttons.shift.type
99
+ assert_equal 'button', buttons.shift.type
100
+ assert_equal 'image', buttons.shift.type
101
+ assert_equal 'image', buttons.shift.type
102
+
103
+ assert_empty buttons
104
+ end
105
+
106
+ def test_parse_select
107
+ page = html_page <<-BODY
108
+ <form>
109
+ <select name="multi" multiple></select>
110
+ <select name="single"></select>
111
+ </form>
112
+ BODY
113
+
114
+ form = page.forms.first
115
+ selects = form.fields.sort
116
+
117
+ multi = selects.shift
118
+ assert_kind_of Mechanize::Form::MultiSelectList, multi
119
+
120
+ single = selects.shift
121
+ assert_kind_of Mechanize::Form::SelectList, single
122
+
123
+ assert_empty selects
124
+ end
125
+
126
+ def test_checkboxes_no_input_name
127
+ page = @mech.get('http://localhost/form_no_input_name.html')
128
+ form = page.forms.first
129
+
130
+ assert_equal(0, form.checkboxes.length)
131
+ end
132
+
133
+ def test_field_with
134
+ page = @mech.get("http://localhost/google.html")
135
+ search = page.forms.find { |f| f.name == "f" }
136
+
137
+ assert(search.field_with(:name => 'q'))
138
+ assert(search.field_with(:name => 'hl'))
139
+ assert(search.fields.find { |f| f.name == 'ie' })
140
+ end
141
+
142
+ def test_fields_no_input_name
143
+ page = @mech.get('http://localhost/form_no_input_name.html')
144
+ form = page.forms.first
145
+
146
+ assert_equal(0, form.fields.length)
147
+ end
148
+
149
+ def test_file_uploads_no_value
150
+ page = @mech.get("http://localhost/file_upload.html")
151
+ form = page.form('value_test')
152
+ assert_nil(form.file_uploads.first.value)
153
+ assert_nil(form.file_uploads.first.file_name)
154
+ end
155
+
156
+ def test_forms_no_input_name
157
+ page = @mech.get('http://localhost/form_no_input_name.html')
158
+ form = page.forms.first
159
+
160
+ assert_equal(0, form.radiobuttons.length)
161
+ end
162
+
163
+ def test_has_field_eh
164
+ refute @form.has_field? 'name'
165
+
166
+ @form['name'] = 'Aaron'
167
+
168
+ assert @form.has_field?('name')
169
+ end
170
+
171
+ def test_has_value_eh
172
+ refute @form.has_value? 'Aaron'
173
+
174
+ @form['name'] = 'Aaron'
175
+
176
+ assert @form.has_value?('Aaron')
177
+ end
178
+
179
+ def test_keys
180
+ assert_empty @form.keys
181
+
182
+ @form['name'] = 'Aaron'
183
+
184
+ assert_equal %w[name], @form.keys
185
+ end
186
+
187
+ def test_parse_textarea
188
+ form = Nokogiri::HTML <<-FORM
189
+ <form>
190
+ <textarea name="t">hi</textarea>
191
+ </form>
192
+ FORM
193
+
194
+ form = Mechanize::Form.new form, @mech
195
+ textarea = form.fields.first
196
+
197
+ assert_kind_of Mechanize::Form::Textarea, textarea
198
+ assert_equal 'hi', textarea.value
199
+ end
200
+
201
+ def test_post_with_rails_3_encoding_hack
202
+ page = @mech.get("http://localhost/rails_3_encoding_hack_form_test.html")
203
+ form = page.forms.first
204
+ form.submit
205
+ end
206
+
207
+ def test_post_with_blank_encoding
208
+ page = @mech.get("http://localhost/form_test.html")
209
+ form = page.form('post_form1')
210
+ form.page.encoding = nil
211
+ form.submit
212
+ end
213
+
214
+ def test_set_fields_duplicate
215
+ page = html_page '<form><input name="a" value="b"><input name="a"></form>'
216
+ form = page.forms.first
217
+
218
+ form.set_fields :a => 'c'
219
+
220
+ assert_equal 'c', form.fields.first.value
221
+ assert_equal '', form.fields.last.value
222
+ end
223
+
224
+ def test_set_fields_none
225
+ page = html_page '<form><input name="a" value="b"></form>'
226
+ form = page.forms.first
227
+
228
+ form.set_fields
229
+
230
+ assert_equal 'b', form.fields.first.value
231
+ end
232
+
233
+ def test_set_fields_many
234
+ page = html_page '<form><input name="a" value="b"><input name="b"></form>'
235
+ form = page.forms.first
236
+
237
+ form.set_fields :a => 'c', :b => 'd'
238
+
239
+ assert_equal 'c', form.fields.first.value
240
+ assert_equal 'd', form.fields.last.value
241
+ end
242
+
243
+ def test_set_fields_one
244
+ page = html_page '<form><input name="a" value="b"></form>'
245
+ form = page.forms.first
246
+
247
+ form.set_fields :a => 'c'
248
+
249
+ assert_equal 'c', form.fields.first.value
250
+ end
251
+
252
+ def test_set_fields_position
253
+ page = html_page '<form><input name="a" value="b"><input name="a"></form>'
254
+ form = page.forms.first
255
+
256
+ form.set_fields :a => { 0 => 'c', 1 => 'd' }
257
+
258
+ assert_equal 'c', form.fields.first.value
259
+ assert_equal 'd', form.fields.last.value
260
+ end
261
+
262
+ def test_set_fields_position_crappily
263
+ page = html_page '<form><input name="a" value="b"><input name="a"></form>'
264
+ form = page.forms.first
265
+
266
+ form.set_fields :a => ['c', 1]
267
+
268
+ assert_equal 'b', form.fields.first.value
269
+ assert_equal 'c', form.fields.last.value
270
+ end
271
+
272
+ def test_values
273
+ assert_empty @form.values
274
+
275
+ @form['name'] = 'Aaron'
276
+
277
+ assert_equal %w[Aaron], @form.values
278
+ end
279
+
280
+ def test_no_form_action
281
+ page = @mech.get('http://localhost:2000/form_no_action.html')
282
+ page.forms.first.fields.first.value = 'Aaron'
283
+ page = @mech.submit(page.forms.first)
284
+ assert_match('/form_no_action.html?first=Aaron', page.uri.to_s)
285
+ end
286
+
287
+ def test_submit_first_field_wins
288
+ page = @mech.get('http://localhost/tc_field_precedence.html')
289
+ form = page.forms.first
290
+
291
+ assert !form.checkboxes.empty?
292
+ assert_equal "1", form.checkboxes.first.value
293
+
294
+ submitted = form.submit
295
+
296
+ assert_equal 'ticky=1&ticky=0', submitted.parser.at('#query').text
297
+ end
298
+
299
+ def test_submit_takes_arbirary_headers
300
+ page = @mech.get('http://localhost:2000/form_no_action.html')
301
+ assert form = page.forms.first
302
+ form.action = '/http_headers'
303
+ page = @mech.submit(form, nil, { 'foo' => 'bar' })
304
+
305
+ headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
306
+ headers = Hash[*headers]
307
+
308
+ assert_equal 'bar', headers['foo']
309
+ end
310
+
311
+ def test_submit_select_default_all
312
+ page = html_page <<-BODY
313
+ <form name="form1" method="post" action="/form_post">
314
+ <select name="list">
315
+ <option value="1" selected>Option 1</option>
316
+ <option value="2" selected>Option 2</option>
317
+ <option value="3" selected>Option 3</option>
318
+ <option value="4" selected>Option 4</option>
319
+ <option value="5" selected>Option 5</option>
320
+ <option value="6" selected>Option 6</option>
321
+ </select>
322
+ <br />
323
+ <input type="submit" value="Submit" />
324
+ </form>
325
+ BODY
326
+
327
+ form = page.forms.first
328
+ assert_equal "6", form.list
329
+
330
+ page = @mech.submit form
331
+ assert_equal 1, page.links.length
332
+ assert_equal 1, page.links_with(:text => 'list:6').length
333
+ end
334
+
335
+ def test_submit_select_default_none
336
+ page = html_page <<-BODY
337
+ <form name="form1" method="post" action="/form_post">
338
+ <select name="list">
339
+ <option value="1">Option 1</option>
340
+ <option value="2">Option 2</option>
341
+ <option>Option No Value</option>
342
+ <option value="3">Option 3</option>
343
+ <option value="4">Option 4</option>
344
+ <option value="5">Option 5</option>
345
+ <option value="6">Option 6</option>
346
+ </select>
347
+ <br />
348
+ <input type="submit" value="Submit" />
349
+ </form>
350
+ BODY
351
+
352
+ form = page.forms.first
353
+
354
+ assert_equal "1", form.list
355
+ page = @mech.submit form
356
+
357
+ assert_equal 1, page.links.length
358
+ assert_equal 1, page.links_with(:text => 'list:1').length
359
+ end
360
+
361
+ def test_form_select_default_noopts
362
+ page = html_page <<-BODY
363
+ <form name="form1" method="post" action="/form_post">
364
+ <select name="list">
365
+ </select>
366
+ <br />
367
+ <input type="submit" value="Submit" />
368
+ </form>
369
+ BODY
370
+ form = page.forms.first
371
+
372
+ assert form.field 'list'
373
+ assert_nil form.list
374
+
375
+ page = @mech.submit form
376
+
377
+ assert_empty page.links
378
+ end
379
+
380
+ # Test submitting form with two fields of the same name
381
+ def test_post_multival
382
+ page = @mech.get("http://localhost/form_multival.html")
383
+ form = page.form_with(:name => 'post_form')
384
+
385
+ assert_equal(2, form.fields_with(:name => 'first').length)
386
+
387
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
388
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
389
+
390
+ page = @mech.submit(form)
391
+
392
+ assert_equal(2, page.links.length)
393
+ assert(page.link_with(:text => 'first:Aaron'))
394
+ assert(page.link_with(:text => 'first:Patterson'))
395
+ end
396
+
397
+ # Test calling submit on the form object
398
+ def test_submit_on_form
399
+ page = @mech.get("http://localhost/form_multival.html")
400
+ form = page.form_with(:name => 'post_form')
401
+
402
+ assert_equal(2, form.fields_with(:name => 'first').length)
403
+
404
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
405
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
406
+
407
+ page = form.submit
408
+
409
+ assert_equal(2, page.links.length)
410
+ assert(page.link_with(:text => 'first:Aaron'))
411
+ assert(page.link_with(:text => 'first:Patterson'))
412
+ end
413
+
414
+ # Test submitting form with two fields of the same name
415
+ def test_get_multival
416
+ page = @mech.get("http://localhost/form_multival.html")
417
+ form = page.form_with(:name => 'get_form')
418
+
419
+ assert_equal(2, form.fields_with(:name => 'first').length)
420
+
421
+ form.fields_with(:name => 'first')[0].value = 'Aaron'
422
+ form.fields_with(:name => 'first')[1].value = 'Patterson'
423
+
424
+ page = @mech.submit(form)
425
+
426
+ assert_equal(2, page.links.length)
427
+ assert(page.link_with(:text => 'first:Aaron'))
428
+ assert(page.link_with(:text => 'first:Patterson'))
429
+ end
430
+
431
+ def test_post_with_non_strings
432
+ page = @mech.get("http://localhost/form_test.html")
433
+ page.form('post_form1') do |form|
434
+ form.first_name = 10
435
+ end.submit
436
+ end
437
+
438
+ def test_post
439
+ page = @mech.get("http://localhost/form_test.html")
440
+ post_form = page.forms.find { |f| f.name == "post_form1" }
441
+
442
+ assert_equal("post", post_form.method.downcase)
443
+ assert_equal("/form_post", post_form.action)
444
+
445
+ assert_equal(3, post_form.fields.size)
446
+
447
+ assert_equal(1, post_form.buttons.size)
448
+ assert_equal(2, post_form.radiobuttons.size)
449
+ assert_equal(3, post_form.checkboxes.size)
450
+ assert(post_form.fields.find { |f| f.name == "first_name" },
451
+ "First name field was nil")
452
+ assert(post_form.fields.find { |f| f.name == "country" },
453
+ "Country field was nil")
454
+ assert(post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
455
+ "Gender male button was nil")
456
+
457
+ assert(post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
458
+ "Gender female button was nil")
459
+
460
+ assert(post_form.checkboxes.find { |f| f.name == "cool person" },
461
+ "couldn't find cool person checkbox")
462
+ assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
463
+ "couldn't find likes ham checkbox")
464
+ assert(post_form.checkboxes.find { |f| f.name == "green[eggs]" },
465
+ "couldn't find green[eggs] checkbox")
466
+
467
+ # Find the select list
468
+ s = post_form.fields.find { |f| f.name == "country" }
469
+ assert_equal(2, s.options.length)
470
+ assert_equal("USA", s.value)
471
+ assert_equal("USA", s.options.first.value)
472
+ assert_equal("USA", s.options.first.text)
473
+ assert_equal("CANADA", s.options[1].value)
474
+ assert_equal("CANADA", s.options[1].text)
475
+
476
+ # Now set all the fields
477
+ post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
478
+ post_form.radiobuttons.find { |f|
479
+ f.name == "gender" && f.value == "male"
480
+ }.checked = true
481
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
482
+ post_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
483
+ page = @mech.submit(post_form, post_form.buttons.first)
484
+
485
+ # Check that the submitted fields exist
486
+ assert_equal(5, page.links.size, "Not enough links")
487
+ assert(page.links.find { |l| l.text == "likes ham:on" },
488
+ "likes ham check box missing")
489
+ assert(page.links.find { |l| l.text == "green[eggs]:on" },
490
+ "green[eggs] check box missing")
491
+ assert(page.links.find { |l| l.text == "first_name:Aaron" },
492
+ "first_name field missing")
493
+ assert(page.links.find { |l| l.text == "gender:male" },
494
+ "gender field missing")
495
+ assert(page.links.find { |l| l.text == "country:USA" },
496
+ "select box not submitted")
497
+ end
498
+
499
+ def test_post_multipart
500
+ page = @mech.get("http://localhost/form_test.html")
501
+ post_form = page.forms.find { |f| f.name == "post_form4_multipart" }
502
+ assert(post_form, "Post form is null")
503
+ assert_equal("post", post_form.method.downcase)
504
+ assert_equal("/form_post", post_form.action)
505
+
506
+ assert_equal(1, post_form.fields.size)
507
+ assert_equal(1, post_form.buttons.size)
508
+
509
+ page = @mech.submit(post_form, post_form.buttons.first)
510
+
511
+ assert page
512
+ end
513
+
514
+ def test_select_box
515
+ page = @mech.get("http://localhost/form_test.html")
516
+ post_form = page.forms.find { |f| f.name == "post_form1" }
517
+
518
+ assert(page.header)
519
+ assert(page.root)
520
+ assert_equal(0, page.iframes.length)
521
+ assert_equal("post", post_form.method.downcase)
522
+ assert_equal("/form_post", post_form.action)
523
+
524
+ # Find the select list
525
+ s = post_form.field_with(:name => /country/)
526
+
527
+ assert_equal(2, s.options.length)
528
+ assert_equal("USA", s.value)
529
+ assert_equal("USA", s.options.first.value)
530
+ assert_equal("USA", s.options.first.text)
531
+ assert_equal("CANADA", s.options[1].value)
532
+ assert_equal("CANADA", s.options[1].text)
533
+
534
+ # Now set all the fields
535
+ post_form.field_with(:name => /country/).value = s.options[1]
536
+ assert_equal('CANADA', post_form.country)
537
+ page = @mech.submit(post_form, post_form.buttons.first)
538
+
539
+ # Check that the submitted fields exist
540
+ assert(page.links.find { |l| l.text == "country:CANADA" },
541
+ "select box not submitted")
542
+ end
543
+
544
+ def test_get
545
+ page = @mech.get("http://localhost/form_test.html")
546
+ get_form = page.forms.find { |f| f.name == "get_form1" }
547
+
548
+ assert_equal("get", get_form.method.downcase)
549
+ assert_equal("/form_post", get_form.action)
550
+ assert_equal(1, get_form.fields.size)
551
+ assert_equal(2, get_form.buttons.size)
552
+ assert_equal(2, get_form.radiobuttons.size)
553
+ assert_equal(3, get_form.checkboxes.size)
554
+ assert(get_form.fields.find { |f| f.name == "first_name" },
555
+ "First name field was nil")
556
+ assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
557
+ "Gender male button was nil")
558
+
559
+ assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
560
+ "Gender female button was nil")
561
+
562
+ assert(get_form.checkboxes.find { |f| f.name == "cool person" },
563
+ "couldn't find cool person checkbox")
564
+ assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
565
+ "couldn't find likes ham checkbox")
566
+ assert(get_form.checkboxes.find { |f| f.name == "green[eggs]" },
567
+ "couldn't find green[eggs] checkbox")
568
+
569
+ # Set up the image button
570
+ img = get_form.buttons.find { |f| f.name == "button" }
571
+ img.x = "9"
572
+ img.y = "10"
573
+ # Now set all the fields
574
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
575
+ get_form.radiobuttons.find { |f|
576
+ f.name == "gender" && f.value == "male"
577
+ }.checked = true
578
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
579
+ get_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
580
+ page = @mech.submit(get_form, get_form.buttons.first)
581
+
582
+ # Check that the submitted fields exist
583
+ assert_equal(6, page.links.size, "Not enough links")
584
+ assert(page.links.find { |l| l.text == "likes ham:on" },
585
+ "likes ham check box missing")
586
+ assert(page.links.find { |l| l.text == "green[eggs]:on" },
587
+ "green[eggs] check box missing")
588
+ assert(page.links.find { |l| l.text == "first_name:Aaron" },
589
+ "first_name field missing")
590
+ assert(page.links.find { |l| l.text == "gender:male" },
591
+ "gender field missing")
592
+ assert(page.links.find { |l| l.text == "button.y:10" },
593
+ "Image button missing")
594
+ assert(page.links.find { |l| l.text == "button.x:9" },
595
+ "Image button missing")
596
+ end
597
+
598
+ def test_post_with_space_in_action
599
+ page = @mech.get("http://localhost/form_test.html")
600
+ post_form = page.forms.find { |f| f.name == "post_form2" }
601
+
602
+ assert_equal("post", post_form.method.downcase)
603
+ assert_equal("/form post", post_form.action)
604
+ assert_equal(1, post_form.fields.size)
605
+ assert_equal(1, post_form.buttons.size)
606
+ assert_equal(2, post_form.radiobuttons.size)
607
+ assert_equal(2, post_form.checkboxes.size)
608
+ assert(post_form.fields.find { |f| f.name == "first_name" },
609
+ "First name field was nil")
610
+ assert(post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
611
+ "Gender male button was nil")
612
+
613
+ assert(post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
614
+ "Gender female button was nil")
615
+
616
+ assert(post_form.checkboxes.find { |f| f.name == "cool person" },
617
+ "couldn't find cool person checkbox")
618
+ assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
619
+ "couldn't find likes ham checkbox")
620
+
621
+ # Now set all the fields
622
+ post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
623
+ post_form.radiobuttons.find { |f|
624
+ f.name == "gender" && f.value == "male"
625
+ }.checked = true
626
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
627
+ page = @mech.submit(post_form, post_form.buttons.first)
628
+
629
+ # Check that the submitted fields exist
630
+ assert_equal(3, page.links.size, "Not enough links")
631
+ assert(page.links.find { |l| l.text == "likes ham:on" },
632
+ "likes ham check box missing")
633
+ assert(page.links.find { |l| l.text == "first_name:Aaron" },
634
+ "first_name field missing")
635
+ assert(page.links.find { |l| l.text == "gender:male" },
636
+ "gender field missing")
637
+ end
638
+
639
+ def test_get_with_space_in_action
640
+ page = @mech.get("http://localhost/form_test.html")
641
+ get_form = page.forms.find { |f| f.name == "get_form2" }
642
+
643
+ assert_equal("get", get_form.method.downcase)
644
+ assert_equal("/form post", get_form.action)
645
+ assert_equal(1, get_form.fields.size)
646
+ assert_equal(1, get_form.buttons.size)
647
+ assert_equal(2, get_form.radiobuttons.size)
648
+ assert_equal(2, get_form.checkboxes.size)
649
+ assert(get_form.fields.find { |f| f.name == "first_name" },
650
+ "First name field was nil")
651
+ assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
652
+ "Gender male button was nil")
653
+
654
+ assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
655
+ "Gender female button was nil")
656
+
657
+ assert(get_form.checkboxes.find { |f| f.name == "cool person" },
658
+ "couldn't find cool person checkbox")
659
+ assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
660
+ "couldn't find likes ham checkbox")
661
+
662
+ # Now set all the fields
663
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
664
+ get_form.radiobuttons.find { |f|
665
+ f.name == "gender" && f.value == "male"
666
+ }.checked = true
667
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
668
+ page = @mech.submit(get_form, get_form.buttons.first)
669
+
670
+ # Check that the submitted fields exist
671
+ assert_equal(3, page.links.size, "Not enough links")
672
+ assert(page.links.find { |l| l.text == "likes ham:on" },
673
+ "likes ham check box missing")
674
+ assert(page.links.find { |l| l.text == "first_name:Aaron" },
675
+ "first_name field missing")
676
+ assert(page.links.find { |l| l.text == "gender:male" },
677
+ "gender field missing")
678
+ end
679
+
680
+ def test_post_with_param_in_action
681
+ page = @mech.get("http://localhost/form_test.html")
682
+ post_form = page.forms.find { |f| f.name == "post_form3" }
683
+
684
+ assert_equal("post", post_form.method.downcase)
685
+ assert_equal("/form_post?great day=yes&one=two", post_form.action)
686
+ assert_equal(1, post_form.fields.size)
687
+ assert_equal(1, post_form.buttons.size)
688
+ assert_equal(2, post_form.radiobuttons.size)
689
+ assert_equal(2, post_form.checkboxes.size)
690
+
691
+ assert(post_form.fields.find { |f| f.name == "first_name" },
692
+ "First name field was nil")
693
+
694
+ male_button = post_form.radiobuttons.find { |f|
695
+ f.name == "gender" && f.value == "male"
696
+ }
697
+ assert(male_button, "Gender male button was nil")
698
+
699
+ female_button = post_form.radiobuttons.find { |f|
700
+ f.name == "gender" && f.value == "female"
701
+ }
702
+
703
+ assert(female_button, "Gender female button was nil")
704
+
705
+ assert(post_form.checkbox_with(:name => "cool person"),
706
+ "couldn't find cool person checkbox")
707
+
708
+ assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
709
+ "couldn't find likes ham checkbox")
710
+
711
+ # Now set all the fields
712
+ post_form.field_with(:name => 'first_name').value = "Aaron"
713
+ post_form.radiobuttons.find { |f|
714
+ f.name == "gender" && f.value == "male"
715
+ }.checked = true
716
+ post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
717
+
718
+ page = @mech.submit(post_form, post_form.buttons.first)
719
+
720
+ # Check that the submitted fields exist
721
+ assert_equal(3, page.links.size, "Not enough links")
722
+
723
+ assert(page.links.find { |l| l.text == "likes ham:on" },
724
+ "likes ham check box missing")
725
+ assert(page.links.find { |l| l.text == "first_name:Aaron" },
726
+ "first_name field missing")
727
+ assert(page.links.find { |l| l.text == "gender:male" },
728
+ "gender field missing")
729
+ end
730
+
731
+ def test_get_with_param_in_action
732
+ page = @mech.get("http://localhost/form_test.html")
733
+ get_form = page.forms.find { |f| f.name == "get_form3" }
734
+
735
+ assert_equal("get", get_form.method.downcase)
736
+ assert_equal("/form_post?great day=yes&one=two", get_form.action)
737
+ assert_equal(1, get_form.fields.size)
738
+ assert_equal(1, get_form.buttons.size)
739
+ assert_equal(2, get_form.radiobuttons.size)
740
+ assert_equal(2, get_form.checkboxes.size)
741
+ assert(get_form.fields.find { |f| f.name == "first_name" },
742
+ "First name field was nil")
743
+ assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
744
+ "Gender male button was nil")
745
+
746
+ assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
747
+ "Gender female button was nil")
748
+
749
+ assert(get_form.checkboxes.find { |f| f.name == "cool person" },
750
+ "couldn't find cool person checkbox")
751
+ assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
752
+ "couldn't find likes ham checkbox")
753
+
754
+ # Now set all the fields
755
+ get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
756
+ get_form.radiobuttons.find { |f|
757
+ f.name == "gender" && f.value == "male"
758
+ }.checked = true
759
+ get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
760
+ page = @mech.submit(get_form, get_form.buttons.first)
761
+ # Check that the submitted fields exist
762
+ assert_equal(3, page.links.size, "Not enough links")
763
+ assert(page.links.find { |l| l.text == "likes ham:on" },
764
+ "likes ham check box missing")
765
+ assert(page.links.find { |l| l.text == "first_name:Aaron" },
766
+ "first_name field missing")
767
+ assert(page.links.find { |l| l.text == "gender:male" },
768
+ "gender field missing")
769
+ end
770
+
771
+ def test_field_addition
772
+ page = @mech.get("http://localhost/form_test.html")
773
+ get_form = page.forms.find { |f| f.name == "get_form1" }
774
+ get_form.field("first_name").value = "Gregory"
775
+ assert_equal( "Gregory", get_form.field("first_name").value )
776
+ end
777
+
778
+ def test_fields_as_accessors
779
+ page = @mech.get("http://localhost/form_multival.html")
780
+ form = page.form_with(:name => 'post_form')
781
+
782
+ assert_equal(2, form.fields_with(:name => 'first').length)
783
+
784
+ form.first = 'Aaron'
785
+ assert_equal('Aaron', form.first)
786
+ end
787
+
788
+ def test_form_and_fields_dom_id
789
+ # blatant copypasta of test above
790
+ page = @mech.get("http://localhost/form_test.html")
791
+ form = page.form_with(:dom_id => 'generic_form')
792
+ form_by_id = page.form_with(:id => 'generic_form')
793
+
794
+ assert_equal(1, form.fields_with(:dom_id => 'name_first').length)
795
+ assert_equal('first_name', form.field_with(:dom_id => 'name_first').name)
796
+
797
+ # *_with(:id => blah) should work exactly like (:dom_id => blah)
798
+ assert_equal(form, form_by_id)
799
+ assert_equal(form.fields_with(:dom_id => 'name_first'), form.fields_with(:id => 'name_first'))
800
+ end
801
+
802
+ def test_form_and_fields_dom_class
803
+ # blatant copypasta of test above
804
+ page = @mech.get("http://localhost/form_test.html")
805
+ form = page.form_with(:dom_class => 'really_generic_form')
806
+ form_by_class = page.form_with(:class => 'really_generic_form')
807
+
808
+ assert_equal(1, form.fields_with(:dom_class => 'text_input').length)
809
+ assert_equal('first_name', form.field_with(:dom_class => 'text_input').name)
810
+
811
+ # *_with(:class => blah) should work exactly like (:dom_class => blah)
812
+ assert_equal(form, form_by_class)
813
+ assert_equal(form.fields_with(:dom_class => 'text_input'), form.fields_with(:class => 'text_input'))
814
+ end
815
+
816
+ def test_add_field
817
+ page = @mech.get("http://localhost/form_multival.html")
818
+ form = page.form_with(:name => 'post_form')
819
+
820
+ number_of_fields = form.fields.length
821
+
822
+ assert form.add_field!('intarweb')
823
+ assert_equal(number_of_fields + 1, form.fields.length)
824
+ end
825
+
826
+ def test_delete_field
827
+ page = @mech.get("http://localhost/form_multival.html")
828
+ form = page.form_with(:name => 'post_form')
829
+
830
+ number_of_fields = form.fields.length
831
+ assert_equal 2, number_of_fields
832
+
833
+ form.delete_field!('first')
834
+ assert_nil(form['first'])
835
+ assert_equal(number_of_fields - 2, form.fields.length)
836
+ end
837
+
838
+ def test_has_field
839
+ page = @mech.get("http://localhost/form_multival.html")
840
+ form = page.form_with(:name => 'post_form')
841
+
842
+ assert(!form.has_field?('intarweb'))
843
+ assert form.add_field!('intarweb')
844
+ assert(form.has_field?('intarweb'))
845
+ end
846
+
847
+ def test_field_error
848
+ @page = @mech.get('http://localhost/empty_form.html')
849
+ form = @page.forms.first
850
+ assert_raises(NoMethodError) {
851
+ form.foo = 'asdfasdf'
852
+ }
853
+
854
+ assert_raises(NoMethodError) {
855
+ form.foo
856
+ }
857
+ end
858
+
859
+ def test_form_build_query
860
+ page = @mech.get("http://localhost/form_order_test.html")
861
+ get_form = page.forms.first
862
+
863
+ query = get_form.build_query
864
+
865
+ expected = [
866
+ %w[1 RADIO],
867
+ %w[3 nobody@example],
868
+ %w[2 TEXT],
869
+ %w[3 2011-10],
870
+ ]
871
+
872
+ assert_equal expected, query
873
+ end
874
+
875
+ end