Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/network_commands_test.js
3991 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
'use strict'
19
20
const assert = require('node:assert')
21
const { Browser, By } = require('selenium-webdriver')
22
const { Pages, suite } = require('../../lib/test')
23
const { Network } = require('selenium-webdriver/bidi/network')
24
const { AddInterceptParameters } = require('selenium-webdriver/bidi/addInterceptParameters')
25
const { InterceptPhase } = require('selenium-webdriver/bidi/interceptPhase')
26
const { until } = require('selenium-webdriver/index')
27
const { ContinueRequestParameters } = require('selenium-webdriver/bidi/continueRequestParameters')
28
const { ContinueResponseParameters } = require('selenium-webdriver/bidi/continueResponseParameters')
29
const { ProvideResponseParameters } = require('selenium-webdriver/bidi/provideResponseParameters')
30
const { BytesValue, Header } = require('selenium-webdriver/bidi/networkTypes')
31
32
suite(
33
function (env) {
34
let driver
35
let network
36
37
beforeEach(async function () {
38
driver = await env.builder().build()
39
40
network = await Network(driver)
41
})
42
43
afterEach(async function () {
44
await network.close()
45
await driver.quit()
46
})
47
48
describe('Network commands', function () {
49
it('can add intercept', async function () {
50
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
51
assert.notEqual(intercept, null)
52
})
53
54
it('can remove intercept', async function () {
55
const network = await Network(driver)
56
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
57
assert.notEqual(intercept, null)
58
59
await network.removeIntercept(intercept)
60
})
61
62
it('can continue with auth credentials ', async function () {
63
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
64
65
await network.authRequired(async (event) => {
66
await network.continueWithAuth(event.request.request, 'genie', 'bottle')
67
})
68
await driver.get(Pages.basicAuth)
69
70
await driver.wait(until.elementLocated(By.css('pre')))
71
let source = await driver.getPageSource()
72
assert.equal(source.includes('Access granted'), true)
73
})
74
75
it('can continue without auth credentials ', async function () {
76
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
77
78
await network.authRequired(async (event) => {
79
await network.continueWithAuthNoCredentials(event.request.request)
80
})
81
82
await driver.get(Pages.basicAuth)
83
const alert = await driver.wait(until.alertIsPresent())
84
await alert.dismiss()
85
86
await driver.wait(until.elementLocated(By.css('pre')))
87
let source = await driver.getPageSource()
88
assert.equal(source.includes('Access denied'), true)
89
})
90
91
it('can cancel auth ', async function () {
92
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
93
94
await network.authRequired(async (event) => {
95
await network.cancelAuth(event.request.request)
96
})
97
try {
98
await driver.wait(until.alertIsPresent(), 3000)
99
assert.fail('Alert should not be present')
100
} catch (e) {
101
assert.strictEqual(e.name, 'TimeoutError')
102
}
103
})
104
105
it('can fail request', async function () {
106
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
107
108
await network.beforeRequestSent(async (event) => {
109
await network.failRequest(event.request.request)
110
})
111
112
await driver.manage().setTimeouts({ pageLoad: 5000 })
113
114
try {
115
await driver.get(Pages.basicAuth)
116
assert.fail('Page should not be loaded')
117
} catch (e) {
118
assert.strictEqual(e.name, 'TimeoutError')
119
}
120
})
121
122
it('can continue request', async function () {
123
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
124
125
let counter = 0
126
127
await network.beforeRequestSent(async (event) => {
128
await network.continueRequest(new ContinueRequestParameters(event.request.request))
129
counter = counter + 1
130
})
131
132
await driver.get(Pages.logEntryAdded)
133
134
assert.strictEqual(counter >= 1, true)
135
})
136
137
it('can continue response', async function () {
138
await network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED))
139
140
let counter = 0
141
142
await network.responseStarted(async (event) => {
143
await network.continueResponse(new ContinueResponseParameters(event.request.request))
144
counter = counter + 1
145
})
146
147
await driver.get(Pages.logEntryAdded)
148
149
assert.strictEqual(counter >= 1, true)
150
})
151
152
it('can provide response', async function () {
153
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
154
155
let counter = 0
156
157
await network.beforeRequestSent(async (event) => {
158
await network.provideResponse(new ProvideResponseParameters(event.request.request))
159
counter = counter + 1
160
})
161
162
await driver.get(Pages.logEntryAdded)
163
164
assert.strictEqual(counter >= 1, true)
165
})
166
167
it('can provide response with headers', async function () {
168
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
169
170
let counter = 0
171
172
await network.beforeRequestSent(async (event) => {
173
const headers = [
174
new Header('content-type', new BytesValue(BytesValue.Type.STRING, 'application/json')),
175
new Header('x-custom-header', new BytesValue(BytesValue.Type.STRING, 'test-value')),
176
]
177
178
const body = new BytesValue(
179
BytesValue.Type.BASE64,
180
Buffer.from(JSON.stringify({ status: 'ok' })).toString('base64'),
181
)
182
183
const params = new ProvideResponseParameters(event.request.request)
184
.statusCode(200)
185
.body(body)
186
.headers(headers)
187
.reasonPhrase('OK')
188
189
await network.provideResponse(params)
190
counter = counter + 1
191
})
192
193
await driver.get(Pages.logEntryAdded)
194
195
assert.strictEqual(counter >= 1, true)
196
})
197
})
198
},
199
{ browsers: [Browser.FIREFOX] },
200
)
201
202