Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/integration/selenium/webdriver/network_spec.rb
1864 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require_relative 'spec_helper'
21
22
module Selenium
23
module WebDriver
24
describe Network, exclude: {version: 'beta'},
25
exclusive: {bidi: true, reason: 'only executed when bidi is enabled'},
26
only: {browser: %i[chrome edge firefox]} do
27
let(:username) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.first }
28
let(:password) { SpecSupport::RackServer::TestApp::BASIC_AUTH_CREDENTIALS.last }
29
30
it 'adds an auth handler' do
31
reset_driver!(web_socket_url: true) do |driver|
32
network = described_class.new(driver)
33
network.add_authentication_handler(username, password)
34
driver.navigate.to url_for('basicAuth')
35
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
36
expect(network.callbacks.count).to be 1
37
end
38
end
39
40
it 'adds an auth handler with a filter' do
41
reset_driver!(web_socket_url: true) do |driver|
42
network = described_class.new(driver)
43
network.add_authentication_handler(username, password, url_for('basicAuth'))
44
driver.navigate.to url_for('basicAuth')
45
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
46
expect(network.callbacks.count).to be 1
47
end
48
end
49
50
it 'adds an auth handler with multiple filters' do
51
reset_driver!(web_socket_url: true) do |driver|
52
network = described_class.new(driver)
53
network.add_authentication_handler(username, password, url_for('basicAuth'), url_for('formPage.html'))
54
driver.navigate.to url_for('basicAuth')
55
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
56
expect(network.callbacks.count).to be 1
57
end
58
end
59
60
it 'adds an auth handler with a pattern type' do
61
reset_driver!(web_socket_url: true) do |driver|
62
network = described_class.new(driver)
63
network.add_authentication_handler(username, password, url_for('basicAuth'), pattern_type: :url)
64
driver.navigate.to url_for('basicAuth')
65
expect(driver.find_element(tag_name: 'h1').text).to eq('authorized')
66
expect(network.callbacks.count).to be 1
67
end
68
end
69
70
it 'removes an auth handler' do
71
reset_driver!(web_socket_url: true) do |driver|
72
network = described_class.new(driver)
73
id = network.add_authentication_handler(username, password)
74
network.remove_handler(id)
75
expect(network.callbacks.count).to be 0
76
end
77
end
78
79
it 'clears all auth handlers' do
80
reset_driver!(web_socket_url: true) do |driver|
81
network = described_class.new(driver)
82
2.times { network.add_authentication_handler(username, password) }
83
network.clear_handlers
84
expect(network.callbacks.count).to be 0
85
end
86
end
87
88
it 'continues without auth' do
89
reset_driver!(web_socket_url: true) do |driver|
90
network = described_class.new(driver)
91
network.add_authentication_handler(&:skip)
92
expect { driver.navigate.to url_for('basicAuth') }.to raise_error(Error::WebDriverError)
93
end
94
end
95
96
it 'cancels auth' do
97
reset_driver!(web_socket_url: true) do |driver|
98
network = described_class.new(driver)
99
network.add_authentication_handler(&:cancel)
100
driver.navigate.to url_for('basicAuth')
101
expect(driver.find_element(tag_name: 'pre').text).to eq('Login please')
102
end
103
end
104
105
it 'adds a request handler' do
106
reset_driver!(web_socket_url: true) do |driver|
107
network = described_class.new(driver)
108
network.add_request_handler(&:continue)
109
driver.navigate.to url_for('formPage.html')
110
expect(driver.current_url).to eq(url_for('formPage.html'))
111
expect(network.callbacks.count).to be 1
112
end
113
end
114
115
it 'adds a request handler with a filter' do
116
reset_driver!(web_socket_url: true) do |driver|
117
network = described_class.new(driver)
118
network.add_request_handler(url_for('formPage.html'), &:continue)
119
driver.navigate.to url_for('formPage.html')
120
expect(driver.current_url).to eq(url_for('formPage.html'))
121
expect(network.callbacks.count).to be 1
122
end
123
end
124
125
it 'adds a request handler with multiple filters' do
126
reset_driver!(web_socket_url: true) do |driver|
127
network = described_class.new(driver)
128
network.add_request_handler(url_for('formPage.html'), url_for('basicAuth'), &:continue)
129
driver.navigate.to url_for('formPage.html')
130
expect(driver.current_url).to eq(url_for('formPage.html'))
131
expect(network.callbacks.count).to be 1
132
end
133
end
134
135
it 'adds a request handler with a pattern type' do
136
reset_driver!(web_socket_url: true) do |driver|
137
network = described_class.new(driver)
138
network.add_request_handler(url_for('formPage.html'), pattern_type: :url, &:continue)
139
driver.navigate.to url_for('formPage.html')
140
expect(driver.current_url).to eq(url_for('formPage.html'))
141
expect(network.callbacks.count).to be 1
142
end
143
end
144
145
it 'adds a request handler with attributes' do
146
reset_driver!(web_socket_url: true) do |driver|
147
network = described_class.new(driver)
148
network.add_request_handler do |request|
149
request.method = 'GET'
150
request.url = url_for('formPage.html')
151
request.headers['foo'] = 'bar'
152
request.headers['baz'] = 'qux'
153
request.cookies({
154
name: 'test',
155
value: 'value4',
156
domain: 'example.com',
157
path: '/path',
158
size: 1234,
159
httpOnly: true,
160
secure: true,
161
sameSite: 'Strict',
162
expiry: 1234
163
})
164
request.body = ({test: 'example'})
165
request.continue
166
end
167
driver.navigate.to url_for('formPage.html')
168
expect(driver.current_url).to eq(url_for('formPage.html'))
169
expect(network.callbacks.count).to be 1
170
end
171
end
172
173
it 'fails a request' do
174
reset_driver!(web_socket_url: true) do |driver|
175
network = described_class.new(driver)
176
network.add_request_handler(&:fail)
177
expect(network.callbacks.count).to be 1
178
expect { driver.navigate.to url_for('formPage.html') }.to raise_error(Error::WebDriverError)
179
end
180
end
181
182
it 'removes a request handler' do
183
reset_driver!(web_socket_url: true) do |driver|
184
network = described_class.new(driver)
185
id = network.add_request_handler(&:continue)
186
network.remove_handler(id)
187
expect(network.callbacks.count).to be 0
188
end
189
end
190
191
it 'clears all request handlers' do
192
reset_driver!(web_socket_url: true) do |driver|
193
network = described_class.new(driver)
194
2.times { network.add_request_handler(&:continue) }
195
network.clear_handlers
196
expect(network.callbacks.count).to be 0
197
end
198
end
199
200
it 'adds a response handler' do
201
reset_driver!(web_socket_url: true) do |driver|
202
network = described_class.new(driver)
203
network.add_response_handler(&:continue)
204
driver.navigate.to url_for('formPage.html')
205
expect(driver.current_url).to eq(url_for('formPage.html'))
206
expect(network.callbacks.count).to be 1
207
end
208
end
209
210
it 'adds a response handler with a filter' do
211
reset_driver!(web_socket_url: true) do |driver|
212
network = described_class.new(driver)
213
network.add_response_handler(url_for('formPage.html'), &:continue)
214
driver.navigate.to url_for('formPage.html')
215
expect(driver.find_element(name: 'login')).to be_displayed
216
expect(network.callbacks.count).to be 1
217
end
218
end
219
220
it 'adds a response handler with multiple filters' do
221
reset_driver!(web_socket_url: true) do |driver|
222
network = described_class.new(driver)
223
network.add_response_handler(url_for('formPage.html'), url_for('basicAuth'), &:continue)
224
driver.navigate.to url_for('formPage.html')
225
expect(driver.find_element(name: 'login')).to be_displayed
226
expect(network.callbacks.count).to be 1
227
end
228
end
229
230
it 'adds a response handler with a pattern type' do
231
reset_driver!(web_socket_url: true) do |driver|
232
network = described_class.new(driver)
233
network.add_response_handler(url_for('formPage.html'), pattern_type: :url, &:continue)
234
driver.navigate.to url_for('formPage.html')
235
expect(driver.current_url).to eq(url_for('formPage.html'))
236
expect(network.callbacks.count).to be 1
237
end
238
end
239
240
it 'adds a response handler with attributes' do
241
reset_driver!(web_socket_url: true) do |driver|
242
network = described_class.new(driver)
243
network.add_response_handler do |response|
244
response.reason = 'OK'
245
response.headers['foo'] = 'bar'
246
response.credentials.username = 'foo'
247
response.credentials.password = 'bar'
248
response.cookies({
249
name: 'foo',
250
domain: 'localhost',
251
httpOnly: true,
252
expiry: '1_000_000',
253
maxAge: 1_000,
254
path: '/',
255
sameSite: 'none',
256
secure: false
257
})
258
response.continue
259
end
260
driver.navigate.to url_for('formPage.html')
261
expect(driver.current_url).to eq(url_for('formPage.html'))
262
expect(network.callbacks.count).to be 1
263
end
264
end
265
266
it 'adds a response handler that provides a response',
267
except: {browser: :firefox,
268
reason: 'https://github.com/w3c/webdriver-bidi/issues/747'} do
269
reset_driver!(web_socket_url: true) do |driver|
270
network = described_class.new(driver)
271
network.add_response_handler do |response|
272
response.status = 200
273
response.headers['foo'] = 'bar'
274
response.body = '<html><head><title>Hello World!</title></head><body/></html>'
275
response.provide_response
276
end
277
driver.navigate.to url_for('formPage.html')
278
source = driver.page_source
279
expect(source).not_to include('There should be a form here:')
280
expect(source).to include('Hello World!')
281
end
282
end
283
284
it 'removes a response handler' do
285
reset_driver!(web_socket_url: true) do |driver|
286
network = described_class.new(driver)
287
id = network.add_response_handler(&:continue)
288
network.remove_handler(id)
289
network.clear_handlers
290
expect(network.callbacks.count).to be 0
291
end
292
end
293
294
it 'clears all response handlers' do
295
reset_driver!(web_socket_url: true) do |driver|
296
network = described_class.new(driver)
297
2.times { network.add_response_handler(&:continue) }
298
network.clear_handlers
299
expect(network.callbacks.count).to be 0
300
end
301
end
302
end
303
end
304
end
305
306