aai10-mechanize 2.0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +6 -0
- data/.gitignore +9 -0
- data/CHANGELOG.rdoc +652 -0
- data/EXAMPLES.rdoc +187 -0
- data/FAQ.rdoc +11 -0
- data/GUIDE.rdoc +163 -0
- data/LICENSE.rdoc +20 -0
- data/Manifest.txt +172 -0
- data/README.rdoc +63 -0
- data/Rakefile +36 -0
- data/aai10-mechanize.gemspec +20 -0
- data/examples/flickr_upload.rb +22 -0
- data/examples/mech-dump.rb +5 -0
- data/examples/proxy_req.rb +7 -0
- data/examples/rubyforge.rb +20 -0
- data/examples/spider.rb +21 -0
- data/lib/mechanize.rb +664 -0
- data/lib/mechanize/content_type_error.rb +14 -0
- data/lib/mechanize/cookie.rb +116 -0
- data/lib/mechanize/cookie_jar.rb +202 -0
- data/lib/mechanize/element_matcher.rb +35 -0
- data/lib/mechanize/file.rb +80 -0
- data/lib/mechanize/file_connection.rb +17 -0
- data/lib/mechanize/file_request.rb +26 -0
- data/lib/mechanize/file_response.rb +74 -0
- data/lib/mechanize/file_saver.rb +37 -0
- data/lib/mechanize/form.rb +478 -0
- data/lib/mechanize/form/button.rb +9 -0
- data/lib/mechanize/form/check_box.rb +11 -0
- data/lib/mechanize/form/field.rb +44 -0
- data/lib/mechanize/form/file_upload.rb +23 -0
- data/lib/mechanize/form/image_button.rb +20 -0
- data/lib/mechanize/form/multi_select_list.rb +83 -0
- data/lib/mechanize/form/option.rb +49 -0
- data/lib/mechanize/form/radio_button.rb +48 -0
- data/lib/mechanize/form/select_list.rb +40 -0
- data/lib/mechanize/headers.rb +25 -0
- data/lib/mechanize/history.rb +83 -0
- data/lib/mechanize/http.rb +3 -0
- data/lib/mechanize/http/agent.rb +738 -0
- data/lib/mechanize/inspect.rb +88 -0
- data/lib/mechanize/monkey_patch.rb +37 -0
- data/lib/mechanize/page.rb +408 -0
- data/lib/mechanize/page/base.rb +8 -0
- data/lib/mechanize/page/frame.rb +27 -0
- data/lib/mechanize/page/image.rb +30 -0
- data/lib/mechanize/page/label.rb +20 -0
- data/lib/mechanize/page/link.rb +82 -0
- data/lib/mechanize/page/meta_refresh.rb +56 -0
- data/lib/mechanize/pluggable_parsers.rb +101 -0
- data/lib/mechanize/redirect_limit_reached_error.rb +16 -0
- data/lib/mechanize/redirect_not_get_or_head_error.rb +19 -0
- data/lib/mechanize/response_code_error.rb +22 -0
- data/lib/mechanize/response_read_error.rb +27 -0
- data/lib/mechanize/robots_disallowed_error.rb +29 -0
- data/lib/mechanize/unsupported_scheme_error.rb +8 -0
- data/lib/mechanize/util.rb +113 -0
- data/test/data/htpasswd +1 -0
- data/test/data/server.crt +16 -0
- data/test/data/server.csr +12 -0
- data/test/data/server.key +15 -0
- data/test/data/server.pem +15 -0
- data/test/helper.rb +175 -0
- data/test/htdocs/alt_text.html +10 -0
- data/test/htdocs/bad_form_test.html +9 -0
- data/test/htdocs/button.jpg +0 -0
- data/test/htdocs/canonical_uri.html +9 -0
- data/test/htdocs/dir with spaces/foo.html +1 -0
- data/test/htdocs/empty_form.html +6 -0
- data/test/htdocs/file_upload.html +26 -0
- data/test/htdocs/find_link.html +41 -0
- data/test/htdocs/form_multi_select.html +16 -0
- data/test/htdocs/form_multival.html +37 -0
- data/test/htdocs/form_no_action.html +18 -0
- data/test/htdocs/form_no_input_name.html +16 -0
- data/test/htdocs/form_select.html +16 -0
- data/test/htdocs/form_select_all.html +16 -0
- data/test/htdocs/form_select_none.html +17 -0
- data/test/htdocs/form_select_noopts.html +10 -0
- data/test/htdocs/form_set_fields.html +14 -0
- data/test/htdocs/form_test.html +188 -0
- data/test/htdocs/frame_referer_test.html +10 -0
- data/test/htdocs/frame_test.html +30 -0
- data/test/htdocs/google.html +13 -0
- data/test/htdocs/iframe_test.html +16 -0
- data/test/htdocs/index.html +6 -0
- data/test/htdocs/link with space.html +5 -0
- data/test/htdocs/meta_cookie.html +11 -0
- data/test/htdocs/no_title_test.html +6 -0
- data/test/htdocs/nofollow.html +9 -0
- data/test/htdocs/noindex.html +9 -0
- data/test/htdocs/norobots.html +8 -0
- data/test/htdocs/rails_3_encoding_hack_form_test.html +27 -0
- data/test/htdocs/rel_nofollow.html +8 -0
- data/test/htdocs/relative/tc_relative_links.html +21 -0
- data/test/htdocs/robots.html +8 -0
- data/test/htdocs/robots.txt +2 -0
- data/test/htdocs/tc_bad_charset.html +9 -0
- data/test/htdocs/tc_bad_links.html +5 -0
- data/test/htdocs/tc_base_images.html +10 -0
- data/test/htdocs/tc_base_link.html +8 -0
- data/test/htdocs/tc_blank_form.html +11 -0
- data/test/htdocs/tc_charset.html +6 -0
- data/test/htdocs/tc_checkboxes.html +19 -0
- data/test/htdocs/tc_encoded_links.html +5 -0
- data/test/htdocs/tc_field_precedence.html +11 -0
- data/test/htdocs/tc_follow_meta.html +8 -0
- data/test/htdocs/tc_form_action.html +48 -0
- data/test/htdocs/tc_images.html +8 -0
- data/test/htdocs/tc_links.html +18 -0
- data/test/htdocs/tc_meta_in_body.html +9 -0
- data/test/htdocs/tc_no_attributes.html +16 -0
- data/test/htdocs/tc_pretty_print.html +17 -0
- data/test/htdocs/tc_radiobuttons.html +17 -0
- data/test/htdocs/tc_referer.html +16 -0
- data/test/htdocs/tc_relative_links.html +19 -0
- data/test/htdocs/tc_textarea.html +23 -0
- data/test/htdocs/test_bad_encoding.html +52 -0
- data/test/htdocs/test_click.html +11 -0
- data/test/htdocs/unusual______.html +5 -0
- data/test/servlets.rb +402 -0
- data/test/ssl_server.rb +48 -0
- data/test/test_cookies.rb +129 -0
- data/test/test_form_action.rb +52 -0
- data/test/test_form_as_hash.rb +59 -0
- data/test/test_form_button.rb +46 -0
- data/test/test_frames.rb +34 -0
- data/test/test_headers.rb +33 -0
- data/test/test_history.rb +118 -0
- data/test/test_history_added.rb +16 -0
- data/test/test_html_unscape_forms.rb +46 -0
- data/test/test_if_modified_since.rb +20 -0
- data/test/test_images.rb +19 -0
- data/test/test_mechanize.rb +852 -0
- data/test/test_mechanize_cookie.rb +345 -0
- data/test/test_mechanize_cookie_jar.rb +433 -0
- data/test/test_mechanize_file.rb +53 -0
- data/test/test_mechanize_file_request.rb +19 -0
- data/test/test_mechanize_file_response.rb +21 -0
- data/test/test_mechanize_form.rb +576 -0
- data/test/test_mechanize_form_check_box.rb +37 -0
- data/test/test_mechanize_form_encoding.rb +120 -0
- data/test/test_mechanize_form_field.rb +21 -0
- data/test/test_mechanize_form_image_button.rb +12 -0
- data/test/test_mechanize_form_textarea.rb +51 -0
- data/test/test_mechanize_http_agent.rb +697 -0
- data/test/test_mechanize_link.rb +84 -0
- data/test/test_mechanize_page_encoding.rb +147 -0
- data/test/test_mechanize_page_link.rb +382 -0
- data/test/test_mechanize_page_meta_refresh.rb +115 -0
- data/test/test_mechanize_redirect_not_get_or_head_error.rb +18 -0
- data/test/test_mechanize_subclass.rb +22 -0
- data/test/test_mechanize_util.rb +92 -0
- data/test/test_multi_select.rb +118 -0
- data/test/test_no_attributes.rb +13 -0
- data/test/test_option.rb +18 -0
- data/test/test_pluggable_parser.rb +136 -0
- data/test/test_post_form.rb +37 -0
- data/test/test_pretty_print.rb +22 -0
- data/test/test_radiobutton.rb +75 -0
- data/test/test_redirect_limit_reached.rb +39 -0
- data/test/test_redirect_ok.rb +25 -0
- data/test/test_referer.rb +81 -0
- data/test/test_relative_links.rb +40 -0
- data/test/test_request.rb +13 -0
- data/test/test_response_code.rb +53 -0
- data/test/test_robots.rb +72 -0
- data/test/test_save_file.rb +48 -0
- data/test/test_scheme.rb +48 -0
- data/test/test_select.rb +119 -0
- data/test/test_select_all.rb +15 -0
- data/test/test_select_none.rb +15 -0
- data/test/test_select_noopts.rb +18 -0
- data/test/test_set_fields.rb +44 -0
- data/test/test_ssl_server.rb +20 -0
- metadata +360 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class TestMechanizeFile < MiniTest::Unit::TestCase
|
4
|
+
def test_content_disposition
|
5
|
+
file = Mechanize::File.new(
|
6
|
+
URI.parse('http://localhost/foo'),
|
7
|
+
{ 'content-disposition' => 'attachment; filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', }
|
8
|
+
)
|
9
|
+
assert_equal('genome.jpeg', file.filename)
|
10
|
+
|
11
|
+
file = Mechanize::File.new(
|
12
|
+
URI.parse('http://localhost/foo'),
|
13
|
+
{ 'content-disposition' => 'filename=genome.jpeg; modification-date="Wed, 12 Feb 1997 16:29:51 -0500"', }
|
14
|
+
)
|
15
|
+
assert_equal('genome.jpeg', file.filename)
|
16
|
+
|
17
|
+
file = Mechanize::File.new(
|
18
|
+
URI.parse('http://localhost/foo'),
|
19
|
+
{ 'content-disposition' => 'filename=genome.jpeg', }
|
20
|
+
)
|
21
|
+
assert_equal('genome.jpeg', file.filename)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_content_disposition_double_semicolon
|
25
|
+
agent = Mechanize.new
|
26
|
+
page = agent.get("http://localhost/http_headers?content-disposition=#{CGI.escape('attachment;; filename=fooooo')}")
|
27
|
+
assert page.parser
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_from_uri
|
31
|
+
file = Mechanize::File.new(
|
32
|
+
URI.parse('http://localhost/foo'),
|
33
|
+
{}
|
34
|
+
)
|
35
|
+
assert_equal('foo.html', file.filename)
|
36
|
+
|
37
|
+
file = Mechanize::File.new(
|
38
|
+
URI.parse('http://localhost/foo.jpg'),
|
39
|
+
{}
|
40
|
+
)
|
41
|
+
assert_equal('foo.jpg', file.filename)
|
42
|
+
|
43
|
+
file = Mechanize::File.new(
|
44
|
+
URI.parse('http://localhost/foo.jpg')
|
45
|
+
)
|
46
|
+
assert_equal('foo.jpg', file.filename)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_no_uri
|
50
|
+
file = Mechanize::File.new()
|
51
|
+
assert_equal('index.html', file.filename)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestMechanizeFileRequest < MiniTest::Unit::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 'helper'
|
2
|
+
|
3
|
+
class TestMechanizeFileConnection < MiniTest::Unit::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,576 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class TestMechanizeForm < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@agent = Mechanize.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_build_query_blank_form
|
10
|
+
page = @agent.get('http://localhost/tc_blank_form.html')
|
11
|
+
form = page.forms.first
|
12
|
+
query = form.build_query
|
13
|
+
assert(query.length > 0)
|
14
|
+
assert query.all? { |x| x[1] == '' }
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_checkboxes_no_input_name
|
18
|
+
page = @agent.get('http://localhost/form_no_input_name.html')
|
19
|
+
form = page.forms.first
|
20
|
+
|
21
|
+
assert_equal(0, form.checkboxes.length)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_field_with
|
25
|
+
page = @agent.get("http://localhost/google.html")
|
26
|
+
search = page.forms.find { |f| f.name == "f" }
|
27
|
+
|
28
|
+
assert(search.field_with(:name => 'q'))
|
29
|
+
assert(search.field_with(:name => 'hl'))
|
30
|
+
assert(search.fields.find { |f| f.name == 'ie' })
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_fields_no_input_name
|
34
|
+
page = @agent.get('http://localhost/form_no_input_name.html')
|
35
|
+
form = page.forms.first
|
36
|
+
|
37
|
+
assert_equal(0, form.fields.length)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_file_uploads_no_value
|
41
|
+
page = @agent.get("http://localhost/file_upload.html")
|
42
|
+
form = page.form('value_test')
|
43
|
+
assert_nil(form.file_uploads.first.value)
|
44
|
+
assert_nil(form.file_uploads.first.file_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_forms_no_input_name
|
48
|
+
page = @agent.get('http://localhost/form_no_input_name.html')
|
49
|
+
form = page.forms.first
|
50
|
+
|
51
|
+
assert_equal(0, form.radiobuttons.length)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_parse_textarea
|
55
|
+
form = Nokogiri::HTML <<-FORM
|
56
|
+
<form>
|
57
|
+
<textarea name="t">hi</textarea>
|
58
|
+
</form>
|
59
|
+
FORM
|
60
|
+
|
61
|
+
form = Mechanize::Form.new form, @agent
|
62
|
+
textarea = form.fields.first
|
63
|
+
|
64
|
+
assert_kind_of Mechanize::Form::Textarea, textarea
|
65
|
+
assert_equal 'hi', textarea.value
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_post_with_rails_3_encoding_hack
|
69
|
+
page = @agent.get("http://localhost/rails_3_encoding_hack_form_test.html")
|
70
|
+
form = page.forms.first
|
71
|
+
form.submit
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_post_with_blank_encoding
|
75
|
+
page = @agent.get("http://localhost/form_test.html")
|
76
|
+
form = page.form('post_form1')
|
77
|
+
form.page.encoding = nil
|
78
|
+
form.submit
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_no_form_action
|
82
|
+
page = @agent.get('http://localhost:2000/form_no_action.html')
|
83
|
+
page.forms.first.fields.first.value = 'Aaron'
|
84
|
+
page = @agent.submit(page.forms.first)
|
85
|
+
assert_match('/form_no_action.html?first=Aaron', page.uri.to_s)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_submit_first_field_wins
|
89
|
+
page = @agent.get('http://localhost/tc_field_precedence.html')
|
90
|
+
form = page.forms.first
|
91
|
+
|
92
|
+
assert !form.checkboxes.empty?
|
93
|
+
assert_equal "1", form.checkboxes.first.value
|
94
|
+
|
95
|
+
submitted = form.submit
|
96
|
+
|
97
|
+
assert_equal 'ticky=1&ticky=0', submitted.parser.at('#query').text
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_submit_takes_arbirary_headers
|
101
|
+
page = @agent.get('http://localhost:2000/form_no_action.html')
|
102
|
+
assert form = page.forms.first
|
103
|
+
form.action = '/http_headers'
|
104
|
+
page = @agent.submit(form, nil, { 'foo' => 'bar' })
|
105
|
+
|
106
|
+
headers = page.body.split("\n").map { |x| x.split('|', 2) }.flatten
|
107
|
+
headers = Hash[*headers]
|
108
|
+
|
109
|
+
assert_equal 'bar', headers['foo']
|
110
|
+
end
|
111
|
+
|
112
|
+
# Test submitting form with two fields of the same name
|
113
|
+
def test_post_multival
|
114
|
+
page = @agent.get("http://localhost/form_multival.html")
|
115
|
+
form = page.form_with(:name => 'post_form')
|
116
|
+
|
117
|
+
assert_equal(2, form.fields_with(:name => 'first').length)
|
118
|
+
|
119
|
+
form.fields_with(:name => 'first')[0].value = 'Aaron'
|
120
|
+
form.fields_with(:name => 'first')[1].value = 'Patterson'
|
121
|
+
|
122
|
+
page = @agent.submit(form)
|
123
|
+
|
124
|
+
assert_equal(2, page.links.length)
|
125
|
+
assert(page.link_with(:text => 'first:Aaron'))
|
126
|
+
assert(page.link_with(:text => 'first:Patterson'))
|
127
|
+
end
|
128
|
+
|
129
|
+
# Test calling submit on the form object
|
130
|
+
def test_submit_on_form
|
131
|
+
page = @agent.get("http://localhost/form_multival.html")
|
132
|
+
form = page.form_with(:name => 'post_form')
|
133
|
+
|
134
|
+
assert_equal(2, form.fields_with(:name => 'first').length)
|
135
|
+
|
136
|
+
form.fields_with(:name => 'first')[0].value = 'Aaron'
|
137
|
+
form.fields_with(:name => 'first')[1].value = 'Patterson'
|
138
|
+
|
139
|
+
page = form.submit
|
140
|
+
|
141
|
+
assert_equal(2, page.links.length)
|
142
|
+
assert(page.link_with(:text => 'first:Aaron'))
|
143
|
+
assert(page.link_with(:text => 'first:Patterson'))
|
144
|
+
end
|
145
|
+
|
146
|
+
# Test submitting form with two fields of the same name
|
147
|
+
def test_get_multival
|
148
|
+
page = @agent.get("http://localhost/form_multival.html")
|
149
|
+
form = page.form_with(:name => 'get_form')
|
150
|
+
|
151
|
+
assert_equal(2, form.fields_with(:name => 'first').length)
|
152
|
+
|
153
|
+
form.fields_with(:name => 'first')[0].value = 'Aaron'
|
154
|
+
form.fields_with(:name => 'first')[1].value = 'Patterson'
|
155
|
+
|
156
|
+
page = @agent.submit(form)
|
157
|
+
|
158
|
+
assert_equal(2, page.links.length)
|
159
|
+
assert(page.link_with(:text => 'first:Aaron'))
|
160
|
+
assert(page.link_with(:text => 'first:Patterson'))
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_post_with_non_strings
|
164
|
+
page = @agent.get("http://localhost/form_test.html")
|
165
|
+
page.form('post_form1') do |form|
|
166
|
+
form.first_name = 10
|
167
|
+
end.submit
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_post
|
171
|
+
page = @agent.get("http://localhost/form_test.html")
|
172
|
+
post_form = page.forms.find { |f| f.name == "post_form1" }
|
173
|
+
|
174
|
+
assert_equal("post", post_form.method.downcase)
|
175
|
+
assert_equal("/form_post", post_form.action)
|
176
|
+
|
177
|
+
assert_equal(3, post_form.fields.size)
|
178
|
+
|
179
|
+
assert_equal(1, post_form.buttons.size)
|
180
|
+
assert_equal(2, post_form.radiobuttons.size)
|
181
|
+
assert_equal(3, post_form.checkboxes.size)
|
182
|
+
assert(post_form.fields.find { |f| f.name == "first_name" },
|
183
|
+
"First name field was nil")
|
184
|
+
assert(post_form.fields.find { |f| f.name == "country" },
|
185
|
+
"Country field was nil")
|
186
|
+
assert(post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
|
187
|
+
"Gender male button was nil")
|
188
|
+
|
189
|
+
assert(post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
|
190
|
+
"Gender female button was nil")
|
191
|
+
|
192
|
+
assert(post_form.checkboxes.find { |f| f.name == "cool person" },
|
193
|
+
"couldn't find cool person checkbox")
|
194
|
+
assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
|
195
|
+
"couldn't find likes ham checkbox")
|
196
|
+
assert(post_form.checkboxes.find { |f| f.name == "green[eggs]" },
|
197
|
+
"couldn't find green[eggs] checkbox")
|
198
|
+
|
199
|
+
# Find the select list
|
200
|
+
s = post_form.fields.find { |f| f.name == "country" }
|
201
|
+
assert_equal(2, s.options.length)
|
202
|
+
assert_equal("USA", s.value)
|
203
|
+
assert_equal("USA", s.options.first.value)
|
204
|
+
assert_equal("USA", s.options.first.text)
|
205
|
+
assert_equal("CANADA", s.options[1].value)
|
206
|
+
assert_equal("CANADA", s.options[1].text)
|
207
|
+
|
208
|
+
# Now set all the fields
|
209
|
+
post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
|
210
|
+
post_form.radiobuttons.find { |f|
|
211
|
+
f.name == "gender" && f.value == "male"
|
212
|
+
}.checked = true
|
213
|
+
post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
|
214
|
+
post_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
|
215
|
+
page = @agent.submit(post_form, post_form.buttons.first)
|
216
|
+
|
217
|
+
# Check that the submitted fields exist
|
218
|
+
assert_equal(5, page.links.size, "Not enough links")
|
219
|
+
assert(page.links.find { |l| l.text == "likes ham:on" },
|
220
|
+
"likes ham check box missing")
|
221
|
+
assert(page.links.find { |l| l.text == "green[eggs]:on" },
|
222
|
+
"green[eggs] check box missing")
|
223
|
+
assert(page.links.find { |l| l.text == "first_name:Aaron" },
|
224
|
+
"first_name field missing")
|
225
|
+
assert(page.links.find { |l| l.text == "gender:male" },
|
226
|
+
"gender field missing")
|
227
|
+
assert(page.links.find { |l| l.text == "country:USA" },
|
228
|
+
"select box not submitted")
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_post_multipart
|
232
|
+
page = @agent.get("http://localhost/form_test.html")
|
233
|
+
post_form = page.forms.find { |f| f.name == "post_form4_multipart" }
|
234
|
+
assert(post_form, "Post form is null")
|
235
|
+
assert_equal("post", post_form.method.downcase)
|
236
|
+
assert_equal("/form_post", post_form.action)
|
237
|
+
|
238
|
+
assert_equal(1, post_form.fields.size)
|
239
|
+
assert_equal(1, post_form.buttons.size)
|
240
|
+
|
241
|
+
page = @agent.submit(post_form, post_form.buttons.first)
|
242
|
+
|
243
|
+
assert page
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_select_box
|
247
|
+
page = @agent.get("http://localhost/form_test.html")
|
248
|
+
post_form = page.forms.find { |f| f.name == "post_form1" }
|
249
|
+
|
250
|
+
assert(page.header)
|
251
|
+
assert(page.root)
|
252
|
+
assert_equal(0, page.iframes.length)
|
253
|
+
assert_equal("post", post_form.method.downcase)
|
254
|
+
assert_equal("/form_post", post_form.action)
|
255
|
+
|
256
|
+
# Find the select list
|
257
|
+
s = post_form.field_with(:name => /country/)
|
258
|
+
|
259
|
+
assert_equal(2, s.options.length)
|
260
|
+
assert_equal("USA", s.value)
|
261
|
+
assert_equal("USA", s.options.first.value)
|
262
|
+
assert_equal("USA", s.options.first.text)
|
263
|
+
assert_equal("CANADA", s.options[1].value)
|
264
|
+
assert_equal("CANADA", s.options[1].text)
|
265
|
+
|
266
|
+
# Now set all the fields
|
267
|
+
post_form.field_with(:name => /country/).value = s.options[1]
|
268
|
+
assert_equal('CANADA', post_form.country)
|
269
|
+
page = @agent.submit(post_form, post_form.buttons.first)
|
270
|
+
|
271
|
+
# Check that the submitted fields exist
|
272
|
+
assert(page.links.find { |l| l.text == "country:CANADA" },
|
273
|
+
"select box not submitted")
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_get
|
277
|
+
page = @agent.get("http://localhost/form_test.html")
|
278
|
+
get_form = page.forms.find { |f| f.name == "get_form1" }
|
279
|
+
|
280
|
+
assert_equal("get", get_form.method.downcase)
|
281
|
+
assert_equal("/form_post", get_form.action)
|
282
|
+
assert_equal(1, get_form.fields.size)
|
283
|
+
assert_equal(2, get_form.buttons.size)
|
284
|
+
assert_equal(2, get_form.radiobuttons.size)
|
285
|
+
assert_equal(3, get_form.checkboxes.size)
|
286
|
+
assert(get_form.fields.find { |f| f.name == "first_name" },
|
287
|
+
"First name field was nil")
|
288
|
+
assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
|
289
|
+
"Gender male button was nil")
|
290
|
+
|
291
|
+
assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
|
292
|
+
"Gender female button was nil")
|
293
|
+
|
294
|
+
assert(get_form.checkboxes.find { |f| f.name == "cool person" },
|
295
|
+
"couldn't find cool person checkbox")
|
296
|
+
assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
|
297
|
+
"couldn't find likes ham checkbox")
|
298
|
+
assert(get_form.checkboxes.find { |f| f.name == "green[eggs]" },
|
299
|
+
"couldn't find green[eggs] checkbox")
|
300
|
+
|
301
|
+
# Set up the image button
|
302
|
+
img = get_form.buttons.find { |f| f.name == "button" }
|
303
|
+
img.x = "9"
|
304
|
+
img.y = "10"
|
305
|
+
# Now set all the fields
|
306
|
+
get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
|
307
|
+
get_form.radiobuttons.find { |f|
|
308
|
+
f.name == "gender" && f.value == "male"
|
309
|
+
}.checked = true
|
310
|
+
get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
|
311
|
+
get_form.checkboxes.find { |f| f.name == "green[eggs]" }.checked = true
|
312
|
+
page = @agent.submit(get_form, get_form.buttons.first)
|
313
|
+
|
314
|
+
# Check that the submitted fields exist
|
315
|
+
assert_equal(6, page.links.size, "Not enough links")
|
316
|
+
assert(page.links.find { |l| l.text == "likes ham:on" },
|
317
|
+
"likes ham check box missing")
|
318
|
+
assert(page.links.find { |l| l.text == "green[eggs]:on" },
|
319
|
+
"green[eggs] check box missing")
|
320
|
+
assert(page.links.find { |l| l.text == "first_name:Aaron" },
|
321
|
+
"first_name field missing")
|
322
|
+
assert(page.links.find { |l| l.text == "gender:male" },
|
323
|
+
"gender field missing")
|
324
|
+
assert(page.links.find { |l| l.text == "button.y:10" },
|
325
|
+
"Image button missing")
|
326
|
+
assert(page.links.find { |l| l.text == "button.x:9" },
|
327
|
+
"Image button missing")
|
328
|
+
end
|
329
|
+
|
330
|
+
def test_post_with_space_in_action
|
331
|
+
page = @agent.get("http://localhost/form_test.html")
|
332
|
+
post_form = page.forms.find { |f| f.name == "post_form2" }
|
333
|
+
|
334
|
+
assert_equal("post", post_form.method.downcase)
|
335
|
+
assert_equal("/form post", post_form.action)
|
336
|
+
assert_equal(1, post_form.fields.size)
|
337
|
+
assert_equal(1, post_form.buttons.size)
|
338
|
+
assert_equal(2, post_form.radiobuttons.size)
|
339
|
+
assert_equal(2, post_form.checkboxes.size)
|
340
|
+
assert(post_form.fields.find { |f| f.name == "first_name" },
|
341
|
+
"First name field was nil")
|
342
|
+
assert(post_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
|
343
|
+
"Gender male button was nil")
|
344
|
+
|
345
|
+
assert(post_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
|
346
|
+
"Gender female button was nil")
|
347
|
+
|
348
|
+
assert(post_form.checkboxes.find { |f| f.name == "cool person" },
|
349
|
+
"couldn't find cool person checkbox")
|
350
|
+
assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
|
351
|
+
"couldn't find likes ham checkbox")
|
352
|
+
|
353
|
+
# Now set all the fields
|
354
|
+
post_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
|
355
|
+
post_form.radiobuttons.find { |f|
|
356
|
+
f.name == "gender" && f.value == "male"
|
357
|
+
}.checked = true
|
358
|
+
post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
|
359
|
+
page = @agent.submit(post_form, post_form.buttons.first)
|
360
|
+
|
361
|
+
# Check that the submitted fields exist
|
362
|
+
assert_equal(3, page.links.size, "Not enough links")
|
363
|
+
assert(page.links.find { |l| l.text == "likes ham:on" },
|
364
|
+
"likes ham check box missing")
|
365
|
+
assert(page.links.find { |l| l.text == "first_name:Aaron" },
|
366
|
+
"first_name field missing")
|
367
|
+
assert(page.links.find { |l| l.text == "gender:male" },
|
368
|
+
"gender field missing")
|
369
|
+
end
|
370
|
+
|
371
|
+
def test_get_with_space_in_action
|
372
|
+
page = @agent.get("http://localhost/form_test.html")
|
373
|
+
get_form = page.forms.find { |f| f.name == "get_form2" }
|
374
|
+
|
375
|
+
assert_equal("get", get_form.method.downcase)
|
376
|
+
assert_equal("/form post", get_form.action)
|
377
|
+
assert_equal(1, get_form.fields.size)
|
378
|
+
assert_equal(1, get_form.buttons.size)
|
379
|
+
assert_equal(2, get_form.radiobuttons.size)
|
380
|
+
assert_equal(2, get_form.checkboxes.size)
|
381
|
+
assert(get_form.fields.find { |f| f.name == "first_name" },
|
382
|
+
"First name field was nil")
|
383
|
+
assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
|
384
|
+
"Gender male button was nil")
|
385
|
+
|
386
|
+
assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
|
387
|
+
"Gender female button was nil")
|
388
|
+
|
389
|
+
assert(get_form.checkboxes.find { |f| f.name == "cool person" },
|
390
|
+
"couldn't find cool person checkbox")
|
391
|
+
assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
|
392
|
+
"couldn't find likes ham checkbox")
|
393
|
+
|
394
|
+
# Now set all the fields
|
395
|
+
get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
|
396
|
+
get_form.radiobuttons.find { |f|
|
397
|
+
f.name == "gender" && f.value == "male"
|
398
|
+
}.checked = true
|
399
|
+
get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
|
400
|
+
page = @agent.submit(get_form, get_form.buttons.first)
|
401
|
+
|
402
|
+
# Check that the submitted fields exist
|
403
|
+
assert_equal(3, page.links.size, "Not enough links")
|
404
|
+
assert(page.links.find { |l| l.text == "likes ham:on" },
|
405
|
+
"likes ham check box missing")
|
406
|
+
assert(page.links.find { |l| l.text == "first_name:Aaron" },
|
407
|
+
"first_name field missing")
|
408
|
+
assert(page.links.find { |l| l.text == "gender:male" },
|
409
|
+
"gender field missing")
|
410
|
+
end
|
411
|
+
|
412
|
+
def test_post_with_param_in_action
|
413
|
+
page = @agent.get("http://localhost/form_test.html")
|
414
|
+
post_form = page.forms.find { |f| f.name == "post_form3" }
|
415
|
+
|
416
|
+
assert_equal("post", post_form.method.downcase)
|
417
|
+
assert_equal("/form_post?great day=yes&one=two", post_form.action)
|
418
|
+
assert_equal(1, post_form.fields.size)
|
419
|
+
assert_equal(1, post_form.buttons.size)
|
420
|
+
assert_equal(2, post_form.radiobuttons.size)
|
421
|
+
assert_equal(2, post_form.checkboxes.size)
|
422
|
+
|
423
|
+
assert(post_form.fields.find { |f| f.name == "first_name" },
|
424
|
+
"First name field was nil")
|
425
|
+
|
426
|
+
male_button = post_form.radiobuttons.find { |f|
|
427
|
+
f.name == "gender" && f.value == "male"
|
428
|
+
}
|
429
|
+
assert(male_button, "Gender male button was nil")
|
430
|
+
|
431
|
+
female_button = post_form.radiobuttons.find { |f|
|
432
|
+
f.name == "gender" && f.value == "female"
|
433
|
+
}
|
434
|
+
|
435
|
+
assert(female_button, "Gender female button was nil")
|
436
|
+
|
437
|
+
assert(post_form.checkbox_with(:name => "cool person"),
|
438
|
+
"couldn't find cool person checkbox")
|
439
|
+
|
440
|
+
assert(post_form.checkboxes.find { |f| f.name == "likes ham" },
|
441
|
+
"couldn't find likes ham checkbox")
|
442
|
+
|
443
|
+
# Now set all the fields
|
444
|
+
post_form.field_with(:name => 'first_name').value = "Aaron"
|
445
|
+
post_form.radiobuttons.find { |f|
|
446
|
+
f.name == "gender" && f.value == "male"
|
447
|
+
}.checked = true
|
448
|
+
post_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
|
449
|
+
|
450
|
+
page = @agent.submit(post_form, post_form.buttons.first)
|
451
|
+
|
452
|
+
# Check that the submitted fields exist
|
453
|
+
assert_equal(3, page.links.size, "Not enough links")
|
454
|
+
|
455
|
+
assert(page.links.find { |l| l.text == "likes ham:on" },
|
456
|
+
"likes ham check box missing")
|
457
|
+
assert(page.links.find { |l| l.text == "first_name:Aaron" },
|
458
|
+
"first_name field missing")
|
459
|
+
assert(page.links.find { |l| l.text == "gender:male" },
|
460
|
+
"gender field missing")
|
461
|
+
end
|
462
|
+
|
463
|
+
def test_get_with_param_in_action
|
464
|
+
page = @agent.get("http://localhost/form_test.html")
|
465
|
+
get_form = page.forms.find { |f| f.name == "get_form3" }
|
466
|
+
|
467
|
+
assert_equal("get", get_form.method.downcase)
|
468
|
+
assert_equal("/form_post?great day=yes&one=two", get_form.action)
|
469
|
+
assert_equal(1, get_form.fields.size)
|
470
|
+
assert_equal(1, get_form.buttons.size)
|
471
|
+
assert_equal(2, get_form.radiobuttons.size)
|
472
|
+
assert_equal(2, get_form.checkboxes.size)
|
473
|
+
assert(get_form.fields.find { |f| f.name == "first_name" },
|
474
|
+
"First name field was nil")
|
475
|
+
assert(get_form.radiobuttons.find { |f| f.name == "gender" && f.value == "male"},
|
476
|
+
"Gender male button was nil")
|
477
|
+
|
478
|
+
assert(get_form.radiobuttons.find {|f| f.name == "gender" && f.value == "female"},
|
479
|
+
"Gender female button was nil")
|
480
|
+
|
481
|
+
assert(get_form.checkboxes.find { |f| f.name == "cool person" },
|
482
|
+
"couldn't find cool person checkbox")
|
483
|
+
assert(get_form.checkboxes.find { |f| f.name == "likes ham" },
|
484
|
+
"couldn't find likes ham checkbox")
|
485
|
+
|
486
|
+
# Now set all the fields
|
487
|
+
get_form.fields.find { |f| f.name == "first_name" }.value = "Aaron"
|
488
|
+
get_form.radiobuttons.find { |f|
|
489
|
+
f.name == "gender" && f.value == "male"
|
490
|
+
}.checked = true
|
491
|
+
get_form.checkboxes.find { |f| f.name == "likes ham" }.checked = true
|
492
|
+
page = @agent.submit(get_form, get_form.buttons.first)
|
493
|
+
# Check that the submitted fields exist
|
494
|
+
assert_equal(3, page.links.size, "Not enough links")
|
495
|
+
assert(page.links.find { |l| l.text == "likes ham:on" },
|
496
|
+
"likes ham check box missing")
|
497
|
+
assert(page.links.find { |l| l.text == "first_name:Aaron" },
|
498
|
+
"first_name field missing")
|
499
|
+
assert(page.links.find { |l| l.text == "gender:male" },
|
500
|
+
"gender field missing")
|
501
|
+
end
|
502
|
+
|
503
|
+
def test_field_addition
|
504
|
+
page = @agent.get("http://localhost/form_test.html")
|
505
|
+
get_form = page.forms.find { |f| f.name == "get_form1" }
|
506
|
+
get_form.field("first_name").value = "Gregory"
|
507
|
+
assert_equal( "Gregory", get_form.field("first_name").value )
|
508
|
+
end
|
509
|
+
|
510
|
+
def test_fields_as_accessors
|
511
|
+
page = @agent.get("http://localhost/form_multival.html")
|
512
|
+
form = page.form_with(:name => 'post_form')
|
513
|
+
|
514
|
+
assert_equal(2, form.fields_with(:name => 'first').length)
|
515
|
+
|
516
|
+
form.first = 'Aaron'
|
517
|
+
assert_equal('Aaron', form.first)
|
518
|
+
end
|
519
|
+
|
520
|
+
def test_form_and_fields_dom_id
|
521
|
+
# blatant copypasta of test above
|
522
|
+
page = @agent.get("http://localhost/form_test.html")
|
523
|
+
form = page.form_with(:dom_id => 'generic_form')
|
524
|
+
form_by_id = page.form_with(:id => 'generic_form')
|
525
|
+
|
526
|
+
assert_equal(1, form.fields_with(:dom_id => 'name_first').length)
|
527
|
+
assert_equal('first_name', form.field_with(:dom_id => 'name_first').name)
|
528
|
+
|
529
|
+
# *_with(:id => blah) should work exactly like (:dom_id => blah)
|
530
|
+
assert_equal(form, form_by_id)
|
531
|
+
assert_equal(form.fields_with(:dom_id => 'name_first'), form.fields_with(:id => 'name_first'))
|
532
|
+
end
|
533
|
+
|
534
|
+
def test_add_field
|
535
|
+
page = @agent.get("http://localhost/form_multival.html")
|
536
|
+
form = page.form_with(:name => 'post_form')
|
537
|
+
|
538
|
+
number_of_fields = form.fields.length
|
539
|
+
|
540
|
+
assert form.add_field!('intarweb')
|
541
|
+
assert_equal(number_of_fields + 1, form.fields.length)
|
542
|
+
end
|
543
|
+
|
544
|
+
def test_delete_field
|
545
|
+
page = @agent.get("http://localhost/form_multival.html")
|
546
|
+
form = page.form_with(:name => 'post_form')
|
547
|
+
|
548
|
+
number_of_fields = form.fields.length
|
549
|
+
assert_equal 2, number_of_fields
|
550
|
+
|
551
|
+
form.delete_field!('first')
|
552
|
+
assert_nil(form['first'])
|
553
|
+
assert_equal(number_of_fields - 2, form.fields.length)
|
554
|
+
end
|
555
|
+
|
556
|
+
def test_has_field
|
557
|
+
page = @agent.get("http://localhost/form_multival.html")
|
558
|
+
form = page.form_with(:name => 'post_form')
|
559
|
+
|
560
|
+
assert(!form.has_field?('intarweb'))
|
561
|
+
assert form.add_field!('intarweb')
|
562
|
+
assert(form.has_field?('intarweb'))
|
563
|
+
end
|
564
|
+
|
565
|
+
def test_field_error
|
566
|
+
@page = @agent.get('http://localhost/empty_form.html')
|
567
|
+
form = @page.forms.first
|
568
|
+
assert_raises(NoMethodError) {
|
569
|
+
form.foo = 'asdfasdf'
|
570
|
+
}
|
571
|
+
|
572
|
+
assert_raises(NoMethodError) {
|
573
|
+
form.foo
|
574
|
+
}
|
575
|
+
end
|
576
|
+
end
|