capybara-mechanize 1.10.1 → 1.12.1
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.
- checksums.yaml +5 -5
- data/README.mdown +24 -2
- data/lib/capybara/mechanize/browser.rb +41 -37
- data/lib/capybara/mechanize/cucumber.rb +3 -1
- data/lib/capybara/mechanize/driver.rb +9 -3
- data/lib/capybara/mechanize/form.rb +11 -20
- data/lib/capybara/mechanize/node.rb +8 -10
- data/lib/capybara/mechanize/version.rb +3 -1
- data/lib/capybara/mechanize.rb +6 -7
- data/lib/capybara/spec/extended_test_app.rb +19 -16
- data/spec/driver/mechanize_driver_spec.rb +72 -71
- data/spec/driver/remote_mechanize_driver_spec.rb +17 -15
- data/spec/session/mechanize_spec.rb +59 -43
- data/spec/session/remote_mechanize_spec.rb +57 -53
- data/spec/spec_helper.rb +4 -5
- data/spec/support/disable_external_tests.rb +4 -3
- data/spec/support/extended_test_app_setup.rb +2 -0
- data/spec/support/remote_test_url.rb +3 -1
- metadata +73 -18
@@ -1,104 +1,106 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Capybara::Mechanize::Driver, 'local' do
|
4
6
|
let(:driver) { Capybara::Mechanize::Driver.new(ExtendedTestApp) }
|
5
7
|
|
6
|
-
describe
|
7
|
-
it
|
8
|
-
::Mechanize.
|
8
|
+
describe '#configure' do
|
9
|
+
it 'allows extended configuration of the agent' do
|
10
|
+
expect_any_instance_of(::Mechanize).to receive(:foo=).with('test')
|
9
11
|
driver.configure do |agent|
|
10
|
-
agent.foo =
|
12
|
+
agent.foo = 'test'
|
11
13
|
end
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
17
|
describe ':headers option' do
|
16
18
|
it 'should always set headers' do
|
17
|
-
driver = Capybara::RackTest::Driver.new(TestApp, :
|
19
|
+
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
18
20
|
driver.visit('/get_header')
|
19
|
-
driver.html.
|
21
|
+
expect(driver.html).to include('foobar')
|
20
22
|
end
|
21
23
|
|
22
24
|
it 'should keep headers on link clicks' do
|
23
|
-
driver = Capybara::RackTest::Driver.new(TestApp, :
|
25
|
+
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
24
26
|
driver.visit('/header_links')
|
25
27
|
driver.find_xpath('.//a').first.click
|
26
|
-
driver.html.
|
28
|
+
expect(driver.html).to include('foobar')
|
27
29
|
end
|
28
30
|
|
29
31
|
it 'should keep headers on form submit' do
|
30
|
-
driver = Capybara::RackTest::Driver.new(TestApp, :
|
32
|
+
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
31
33
|
driver.visit('/header_links')
|
32
34
|
driver.find_xpath('.//input').first.click
|
33
|
-
driver.html.
|
35
|
+
expect(driver.html).to include('foobar')
|
34
36
|
end
|
35
37
|
|
36
38
|
it 'should keep headers on redirects' do
|
37
|
-
driver = Capybara::RackTest::Driver.new(TestApp, :
|
39
|
+
driver = Capybara::RackTest::Driver.new(TestApp, headers: { 'HTTP_FOO' => 'foobar' })
|
38
40
|
driver.visit('/get_header_via_redirect')
|
39
|
-
driver.html.
|
41
|
+
expect(driver.html).to include('foobar')
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
43
45
|
describe ':follow_redirects option' do
|
44
|
-
it
|
46
|
+
it 'defaults to following redirects' do
|
45
47
|
driver = Capybara::RackTest::Driver.new(TestApp)
|
46
48
|
|
47
49
|
driver.visit('/redirect')
|
48
|
-
driver.response.header['Location'].
|
49
|
-
driver.browser.current_url.
|
50
|
+
expect(driver.response.header['Location']).to be_nil
|
51
|
+
expect(driver.browser.current_url).to match %r{/landed$}
|
50
52
|
end
|
51
53
|
|
52
|
-
it
|
53
|
-
driver = Capybara::RackTest::Driver.new(TestApp, :
|
54
|
+
it 'is possible to not follow redirects' do
|
55
|
+
driver = Capybara::RackTest::Driver.new(TestApp, follow_redirects: false)
|
54
56
|
|
55
57
|
driver.visit('/redirect')
|
56
|
-
driver.response.header['Location'].
|
57
|
-
driver.browser.current_url.
|
58
|
+
expect(driver.response.header['Location']).to match %r{/redirect_again$}
|
59
|
+
expect(driver.browser.current_url).to match %r{/redirect$}
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
63
|
describe ':redirect_limit option' do
|
62
|
-
context
|
64
|
+
context 'with default redirect limit' do
|
63
65
|
let(:driver) { Capybara::RackTest::Driver.new(TestApp) }
|
64
66
|
|
65
|
-
it
|
66
|
-
driver.visit(
|
67
|
-
driver.html.
|
67
|
+
it 'should follow 5 redirects' do
|
68
|
+
driver.visit('/redirect/5/times')
|
69
|
+
expect(driver.html).to include('redirection complete')
|
68
70
|
end
|
69
71
|
|
70
|
-
it
|
72
|
+
it 'should not follow more than 6 redirects' do
|
71
73
|
expect do
|
72
|
-
driver.visit(
|
74
|
+
driver.visit('/redirect/6/times')
|
73
75
|
end.to raise_error(Capybara::InfiniteRedirectError)
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
77
|
-
context
|
78
|
-
let(:driver) { Capybara::RackTest::Driver.new(TestApp, :
|
79
|
+
context 'with 21 redirect limit' do
|
80
|
+
let(:driver) { Capybara::RackTest::Driver.new(TestApp, redirect_limit: 21) }
|
79
81
|
|
80
|
-
it
|
81
|
-
driver.visit(
|
82
|
-
driver.html.
|
82
|
+
it 'should follow 21 redirects' do
|
83
|
+
driver.visit('/redirect/21/times')
|
84
|
+
expect(driver.html).to include('redirection complete')
|
83
85
|
end
|
84
86
|
|
85
|
-
it
|
87
|
+
it 'should not follow more than 21 redirects' do
|
86
88
|
expect do
|
87
|
-
driver.visit(
|
89
|
+
driver.visit('/redirect/22/times')
|
88
90
|
end.to raise_error(Capybara::InfiniteRedirectError)
|
89
91
|
end
|
90
92
|
end
|
91
93
|
end
|
92
94
|
|
93
|
-
it
|
94
|
-
driver.
|
95
|
+
it 'should default to local mode for relative paths' do
|
96
|
+
expect(driver).not_to be_remote('/')
|
95
97
|
end
|
96
98
|
|
97
|
-
it
|
98
|
-
driver.
|
99
|
+
it 'should default to local mode for the default host' do
|
100
|
+
expect(driver).not_to be_remote('http://www.example.com')
|
99
101
|
end
|
100
102
|
|
101
|
-
context
|
103
|
+
context 'with an app_host' do
|
102
104
|
before do
|
103
105
|
Capybara.app_host = 'http://www.remote.com'
|
104
106
|
end
|
@@ -107,12 +109,12 @@ describe Capybara::Mechanize::Driver, 'local' do
|
|
107
109
|
Capybara.app_host = nil
|
108
110
|
end
|
109
111
|
|
110
|
-
it
|
111
|
-
driver.
|
112
|
+
it 'should treat urls as remote' do
|
113
|
+
expect(driver).to be_remote('http://www.remote.com')
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
115
|
-
context
|
117
|
+
context 'with a default url, no app host' do
|
116
118
|
before do
|
117
119
|
Capybara.default_host = 'http://www.local.com'
|
118
120
|
end
|
@@ -121,7 +123,7 @@ describe Capybara::Mechanize::Driver, 'local' do
|
|
121
123
|
Capybara.default_host = CAPYBARA_DEFAULT_HOST
|
122
124
|
end
|
123
125
|
|
124
|
-
context
|
126
|
+
context 'local hosts' do
|
125
127
|
before do
|
126
128
|
Capybara::Mechanize.local_hosts = ['subdomain.local.com']
|
127
129
|
end
|
@@ -130,51 +132,51 @@ describe Capybara::Mechanize::Driver, 'local' do
|
|
130
132
|
Capybara::Mechanize.local_hosts = nil
|
131
133
|
end
|
132
134
|
|
133
|
-
it
|
134
|
-
driver.
|
135
|
+
it 'should allow local hosts to be set' do
|
136
|
+
expect(driver).not_to be_remote('http://subdomain.local.com')
|
135
137
|
end
|
136
138
|
end
|
137
139
|
|
138
|
-
it
|
139
|
-
driver.
|
140
|
+
it 'should treat urls with the same host names as local' do
|
141
|
+
expect(driver).not_to be_remote('http://www.local.com')
|
140
142
|
end
|
141
143
|
|
142
|
-
it
|
143
|
-
driver.
|
144
|
+
it 'should treat other urls as remote' do
|
145
|
+
expect(driver).to be_remote('http://www.remote.com')
|
144
146
|
end
|
145
147
|
|
146
|
-
it
|
148
|
+
it 'should treat relative paths as remote if the previous request was remote' do
|
147
149
|
driver.visit(remote_test_url)
|
148
|
-
driver.
|
150
|
+
expect(driver).to be_remote('/some_relative_link')
|
149
151
|
end
|
150
152
|
|
151
|
-
it
|
153
|
+
it 'should treat relative paths as local if the previous request was local' do
|
152
154
|
driver.visit('http://www.local.com')
|
153
|
-
driver.
|
155
|
+
expect(driver).not_to be_remote('/some_relative_link')
|
154
156
|
end
|
155
157
|
|
156
|
-
it
|
158
|
+
it 'should receive the right host' do
|
157
159
|
driver.visit('http://www.local.com/host')
|
158
160
|
should_be_a_local_get
|
159
161
|
end
|
160
162
|
|
161
|
-
it
|
163
|
+
it 'should consider relative paths to be local when the previous request was local' do
|
162
164
|
driver.visit('http://www.local.com/host')
|
163
165
|
driver.visit('/host')
|
164
166
|
|
165
167
|
should_be_a_local_get
|
166
|
-
driver.
|
168
|
+
expect(driver).not_to be_remote('/first_local')
|
167
169
|
end
|
168
170
|
|
169
|
-
it
|
171
|
+
it 'should consider relative paths to be remote when the previous request was remote' do
|
170
172
|
driver.visit("#{remote_test_url}/host")
|
171
173
|
driver.get('/host')
|
172
174
|
|
173
175
|
should_be_a_remote_get
|
174
|
-
driver.
|
176
|
+
expect(driver).to be_remote('/second_remote')
|
175
177
|
end
|
176
178
|
|
177
|
-
it
|
179
|
+
it 'should always switch to the right context' do
|
178
180
|
driver.visit('http://www.local.com/host')
|
179
181
|
driver.get('/host')
|
180
182
|
driver.get("#{remote_test_url}/host")
|
@@ -182,44 +184,44 @@ describe Capybara::Mechanize::Driver, 'local' do
|
|
182
184
|
driver.get('http://www.local.com/host')
|
183
185
|
|
184
186
|
should_be_a_local_get
|
185
|
-
driver.
|
187
|
+
expect(driver).not_to be_remote('/second_local')
|
186
188
|
end
|
187
189
|
|
188
|
-
it
|
190
|
+
it 'should follow redirects from local to remote' do
|
189
191
|
driver.visit("http://www.local.com/redirect_to/#{remote_test_url}/host")
|
190
192
|
should_be_a_remote_get
|
191
193
|
end
|
192
194
|
|
193
|
-
it
|
195
|
+
it 'should follow redirects from remote to local' do
|
194
196
|
driver.visit("#{remote_test_url}/redirect_to/http://www.local.com/host")
|
195
197
|
should_be_a_local_get
|
196
198
|
end
|
197
199
|
|
198
|
-
it
|
200
|
+
it 'passes the status code of remote calls back to be validated' do
|
199
201
|
quietly do
|
200
202
|
driver.visit(remote_test_url)
|
201
203
|
driver.get('/asdfafadfsdfs')
|
202
|
-
driver.response.status.
|
204
|
+
expect(driver.response.status).to be >= 400
|
203
205
|
end
|
204
206
|
end
|
205
207
|
|
206
|
-
context
|
207
|
-
it
|
208
|
+
context 'when errors are set to true' do
|
209
|
+
it 'raises an useful error because it is probably a misconfiguration' do
|
208
210
|
quietly do
|
209
211
|
original = Capybara.raise_server_errors
|
210
212
|
|
211
|
-
expect
|
213
|
+
expect do
|
212
214
|
driver.visit(remote_test_url)
|
213
215
|
Capybara.raise_server_errors = true
|
214
216
|
driver.get('/asdfafadfsdfs')
|
215
|
-
|
217
|
+
end.to raise_error(%r{Received the following error for a GET request to /asdfafadfsdfs:})
|
216
218
|
Capybara.raise_server_errors = original
|
217
219
|
end
|
218
220
|
end
|
219
221
|
end
|
220
222
|
end
|
221
223
|
|
222
|
-
it
|
224
|
+
it 'should include the right host when remote' do
|
223
225
|
driver.visit("#{remote_test_url}/host")
|
224
226
|
should_be_a_remote_get
|
225
227
|
end
|
@@ -237,17 +239,16 @@ describe Capybara::Mechanize::Driver, 'local' do
|
|
237
239
|
driver.visit("#{remote_test_url}/host")
|
238
240
|
should_be_a_remote_get
|
239
241
|
driver.reset!
|
240
|
-
driver.visit(
|
242
|
+
driver.visit('/host')
|
241
243
|
should_be_a_local_get
|
242
244
|
end
|
243
245
|
end
|
244
246
|
|
245
247
|
def should_be_a_remote_get
|
246
|
-
driver.current_url.
|
248
|
+
expect(driver.current_url).to include(remote_test_url)
|
247
249
|
end
|
248
250
|
|
249
251
|
def should_be_a_local_get
|
250
|
-
driver.current_url.
|
252
|
+
expect(driver.current_url).to include('www.local.com')
|
251
253
|
end
|
252
|
-
|
253
254
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe Capybara::Mechanize::Driver, 'remote' do
|
@@ -11,35 +13,35 @@ describe Capybara::Mechanize::Driver, 'remote' do
|
|
11
13
|
|
12
14
|
let(:driver) { Capybara::Mechanize::Driver.new(ExtendedTestApp) }
|
13
15
|
|
14
|
-
context
|
15
|
-
it
|
16
|
-
driver.visit("#{remote_test_url}/form/get", {:
|
17
|
-
driver.html.
|
16
|
+
context 'in remote mode' do
|
17
|
+
it 'should pass arguments through to a get request' do
|
18
|
+
driver.visit("#{remote_test_url}/form/get", form: { value: 'success' })
|
19
|
+
expect(driver.html).to include('success')
|
18
20
|
end
|
19
21
|
|
20
|
-
it
|
21
|
-
driver.post("#{remote_test_url}/form", {:
|
22
|
-
driver.html.
|
22
|
+
it 'should pass arguments through to a post request' do
|
23
|
+
driver.post("#{remote_test_url}/form", form: { value: 'success' })
|
24
|
+
expect(driver.html).to include('success')
|
23
25
|
end
|
24
26
|
|
25
|
-
describe
|
26
|
-
it
|
27
|
+
describe 'redirect' do
|
28
|
+
it 'should handle redirects with http-params' do
|
27
29
|
driver.visit "#{remote_test_url}/redirect_with_http_param"
|
28
|
-
driver.html.
|
30
|
+
expect(driver.html).to include('correct redirect')
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
context
|
34
|
+
context 'for a post request' do
|
33
35
|
it 'transforms nested map in post data' do
|
34
|
-
driver.post("#{remote_test_url}/form",
|
35
|
-
driver.html.
|
36
|
+
driver.post("#{remote_test_url}/form", form: { key: 'value' })
|
37
|
+
expect(driver.html).to match(/:key=>"value"|key: value/)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
41
|
context 'process remote request' do
|
40
42
|
it 'transforms nested map in post data' do
|
41
|
-
driver.submit(:post, "#{remote_test_url}/form",
|
42
|
-
driver.html.
|
43
|
+
driver.submit(:post, "#{remote_test_url}/form", form: { key: 'value' })
|
44
|
+
expect(driver.html).to match(/:key=>"value"|key: value/)
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -1,21 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
module TestSessions
|
4
6
|
Mechanize = Capybara::Session.new(:mechanize, TestApp)
|
5
7
|
end
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
9
|
+
skipped_tests = %i[
|
10
|
+
about_scheme
|
11
|
+
active_element
|
12
|
+
css
|
13
|
+
download
|
14
|
+
frames
|
15
|
+
hover
|
16
|
+
html_validation
|
17
|
+
js
|
18
|
+
modals
|
19
|
+
screenshot
|
20
|
+
scroll
|
21
|
+
send_keys
|
22
|
+
server
|
23
|
+
shadow_dom
|
24
|
+
spatial
|
25
|
+
windows
|
17
26
|
]
|
18
27
|
|
28
|
+
Capybara::SpecHelper.run_specs(TestSessions::Mechanize, 'Mechanize', capybara_skip: skipped_tests) do |example|
|
29
|
+
case example.metadata[:full_description]
|
30
|
+
when /has_css\? should support case insensitive :class and :id options/
|
31
|
+
skip "Nokogiri doesn't support case insensitive CSS attribute matchers"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
19
35
|
describe Capybara::Session do
|
20
36
|
context 'with mechanize driver' do
|
21
37
|
let(:session) { Capybara::Session.new(:mechanize, ExtendedTestApp) }
|
@@ -29,37 +45,37 @@ describe Capybara::Session do
|
|
29
45
|
end
|
30
46
|
|
31
47
|
describe '#driver' do
|
32
|
-
it
|
33
|
-
session.driver.
|
48
|
+
it 'should be a mechanize driver' do
|
49
|
+
expect(session.driver).to be_an_instance_of(Capybara::Mechanize::Driver)
|
34
50
|
end
|
35
51
|
end
|
36
52
|
|
37
53
|
describe '#mode' do
|
38
|
-
it
|
39
|
-
session.mode.
|
54
|
+
it 'should remember the mode' do
|
55
|
+
expect(session.mode).to eq(:mechanize)
|
40
56
|
end
|
41
57
|
end
|
42
58
|
|
43
59
|
describe '#click_link' do
|
44
|
-
it
|
60
|
+
it 'should use data-method if option is true' do
|
45
61
|
session.driver.options[:respect_data_method] = true
|
46
|
-
session.visit
|
47
|
-
session.click_link
|
48
|
-
session.html.
|
62
|
+
session.visit '/with_html'
|
63
|
+
session.click_link 'A link with data-method'
|
64
|
+
expect(session.html).to include('The requested object was deleted')
|
49
65
|
end
|
50
66
|
|
51
|
-
it
|
67
|
+
it 'should not use data-method if option is false' do
|
52
68
|
session.driver.options[:respect_data_method] = false
|
53
|
-
session.visit
|
54
|
-
session.click_link
|
55
|
-
session.html.
|
69
|
+
session.visit '/with_html'
|
70
|
+
session.click_link 'A link with data-method'
|
71
|
+
expect(session.html).to include('Not deleted')
|
56
72
|
end
|
57
73
|
|
58
74
|
it "should use data-method if available even if it's capitalized" do
|
59
75
|
session.driver.options[:respect_data_method] = true
|
60
|
-
session.visit
|
61
|
-
session.click_link
|
62
|
-
session.html.
|
76
|
+
session.visit '/with_html'
|
77
|
+
session.click_link 'A link with capitalized data-method'
|
78
|
+
expect(session.html).to include('The requested object was deleted')
|
63
79
|
end
|
64
80
|
|
65
81
|
after do
|
@@ -67,44 +83,44 @@ describe Capybara::Session do
|
|
67
83
|
end
|
68
84
|
end
|
69
85
|
|
70
|
-
describe
|
71
|
-
context
|
72
|
-
it
|
73
|
-
session.visit(
|
74
|
-
session.click_button(
|
75
|
-
session.html.
|
86
|
+
describe '#attach_file' do
|
87
|
+
context 'with multipart form' do
|
88
|
+
it 'should submit an empty form-data section if no file is submitted' do
|
89
|
+
session.visit('/form')
|
90
|
+
session.click_button('Upload Empty')
|
91
|
+
expect(session.html).to include('Successfully ignored empty file field.')
|
76
92
|
end
|
77
93
|
end
|
78
94
|
end
|
79
95
|
|
80
|
-
it
|
96
|
+
it 'should use the last remote url when following relative links' do
|
81
97
|
session.visit("#{remote_test_url}/relative_link_to_host")
|
82
|
-
session.click_link
|
83
|
-
session.body.
|
98
|
+
session.click_link 'host'
|
99
|
+
expect(session.body).to include("Current host is #{remote_test_url}/request_info/host, method get")
|
84
100
|
end
|
85
101
|
|
86
|
-
it
|
102
|
+
it 'should use the last remote url when submitting a form with a relative action' do
|
87
103
|
session.visit("#{remote_test_url}/form_with_relative_action_to_host")
|
88
|
-
session.click_button
|
89
|
-
session.body.
|
104
|
+
session.click_button 'submit'
|
105
|
+
expect(session.body).to include("Current host is #{remote_test_url}/request_info/host, method post")
|
90
106
|
end
|
91
107
|
|
92
|
-
it
|
108
|
+
it 'should use the last url when submitting a form with no action' do
|
93
109
|
session.visit("#{remote_test_url}/request_info/form_with_no_action")
|
94
|
-
session.click_button
|
95
|
-
session.body.
|
110
|
+
session.click_button 'submit'
|
111
|
+
expect(session.body).to include("Current host is #{remote_test_url}/request_info/form_with_no_action, method post")
|
96
112
|
end
|
97
113
|
|
98
|
-
it
|
114
|
+
it 'should send correct user agent' do
|
99
115
|
session.visit("#{remote_test_url}/request_info/user_agent")
|
100
|
-
session.body.
|
116
|
+
expect(session.body).to include('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.853.0 Safari/535.2')
|
101
117
|
end
|
102
118
|
|
103
119
|
context 'form referer when switching from local to remote' do
|
104
120
|
it 'sends the referer' do
|
105
121
|
session.visit "/form_posts_to/#{remote_test_url}/get_referer"
|
106
122
|
session.click_button 'submit'
|
107
|
-
session.body.
|
123
|
+
expect(session.body).to include 'Got referer'
|
108
124
|
end
|
109
125
|
end
|
110
126
|
end
|