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
2885 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
31
suite(
32
function (env) {
33
let driver
34
let network
35
36
beforeEach(async function () {
37
driver = await env.builder().build()
38
39
network = await Network(driver)
40
})
41
42
afterEach(async function () {
43
await network.close()
44
await driver.quit()
45
})
46
47
describe('Network commands', function () {
48
it('can add intercept', async function () {
49
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
50
assert.notEqual(intercept, null)
51
})
52
53
it('can remove intercept', async function () {
54
const network = await Network(driver)
55
const intercept = await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
56
assert.notEqual(intercept, null)
57
58
await network.removeIntercept(intercept)
59
})
60
61
it('can continue with auth credentials ', async function () {
62
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
63
64
await network.authRequired(async (event) => {
65
await network.continueWithAuth(event.request.request, 'genie', 'bottle')
66
})
67
await driver.get(Pages.basicAuth)
68
69
await driver.wait(until.elementLocated(By.css('pre')))
70
let source = await driver.getPageSource()
71
assert.equal(source.includes('Access granted'), true)
72
})
73
74
it('can continue without auth credentials ', async function () {
75
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
76
77
await network.authRequired(async (event) => {
78
await network.continueWithAuthNoCredentials(event.request.request)
79
})
80
81
await driver.get(Pages.basicAuth)
82
const alert = await driver.wait(until.alertIsPresent())
83
await alert.dismiss()
84
85
await driver.wait(until.elementLocated(By.css('pre')))
86
let source = await driver.getPageSource()
87
assert.equal(source.includes('Access denied'), true)
88
})
89
90
it('can cancel auth ', async function () {
91
await network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))
92
93
await network.authRequired(async (event) => {
94
await network.cancelAuth(event.request.request)
95
})
96
try {
97
await driver.wait(until.alertIsPresent(), 3000)
98
assert.fail('Alert should not be present')
99
} catch (e) {
100
assert.strictEqual(e.name, 'TimeoutError')
101
}
102
})
103
104
it('can fail request', async function () {
105
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
106
107
await network.beforeRequestSent(async (event) => {
108
await network.failRequest(event.request.request)
109
})
110
111
await driver.manage().setTimeouts({ pageLoad: 5000 })
112
113
try {
114
await driver.get(Pages.basicAuth)
115
assert.fail('Page should not be loaded')
116
} catch (e) {
117
assert.strictEqual(e.name, 'TimeoutError')
118
}
119
})
120
121
it('can continue request', async function () {
122
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
123
124
let counter = 0
125
126
await network.beforeRequestSent(async (event) => {
127
await network.continueRequest(new ContinueRequestParameters(event.request.request))
128
counter = counter + 1
129
})
130
131
await driver.get(Pages.logEntryAdded)
132
133
assert.strictEqual(counter >= 1, true)
134
})
135
136
it('can continue response', async function () {
137
await network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED))
138
139
let counter = 0
140
141
await network.responseStarted(async (event) => {
142
await network.continueResponse(new ContinueResponseParameters(event.request.request))
143
counter = counter + 1
144
})
145
146
await driver.get(Pages.logEntryAdded)
147
148
assert.strictEqual(counter >= 1, true)
149
})
150
151
it('can provide response', async function () {
152
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
153
154
let counter = 0
155
156
await network.beforeRequestSent(async (event) => {
157
await network.provideResponse(new ProvideResponseParameters(event.request.request))
158
counter = counter + 1
159
})
160
161
await driver.get(Pages.logEntryAdded)
162
163
assert.strictEqual(counter >= 1, true)
164
})
165
})
166
},
167
{ browsers: [Browser.FIREFOX] },
168
)
169
170