Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/webdriver_network_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 } = require('selenium-webdriver')
22
const { Pages, suite } = require('../../lib/test')
23
const until = require('selenium-webdriver/lib/until')
24
const { By } = require('selenium-webdriver')
25
26
suite(
27
function (env) {
28
let driver
29
30
beforeEach(async function () {
31
driver = await env.builder().build()
32
})
33
34
afterEach(async function () {
35
await driver.quit()
36
})
37
38
describe('script()', function () {
39
it('can add authentication handler', async function () {
40
await driver.network().addAuthenticationHandler('genie', 'bottle')
41
await driver.get(Pages.basicAuth)
42
43
await driver.wait(until.elementLocated(By.css('pre')))
44
let source = await driver.getPageSource()
45
assert.equal(source.includes('Access granted'), true)
46
})
47
48
it('can add authentication handler with filter', async function () {
49
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
50
await driver.get(Pages.basicAuth)
51
52
await driver.wait(until.elementLocated(By.css('pre')))
53
let source = await driver.getPageSource()
54
assert.equal(source.includes('Access granted'), true)
55
})
56
57
it('can add multiple authentication handlers with filter', async function () {
58
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
59
await driver.network().addAuthenticationHandler('test', 'test', 'test')
60
await driver.get(Pages.basicAuth)
61
62
await driver.wait(until.elementLocated(By.css('pre')))
63
let source = await driver.getPageSource()
64
assert.equal(source.includes('Access granted'), true)
65
})
66
67
it('can add multiple authentication handlers with the same filter', async function () {
68
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
69
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
70
await driver.get(Pages.basicAuth)
71
72
await driver.wait(until.elementLocated(By.css('pre')))
73
let source = await driver.getPageSource()
74
assert.equal(source.includes('Access granted'), true)
75
})
76
77
it('can remove authentication handler', async function () {
78
const id = await driver.network().addAuthenticationHandler('genie', 'bottle')
79
80
await driver.network().removeAuthenticationHandler(id)
81
82
try {
83
await driver.get(Pages.basicAuth)
84
await driver.wait(until.elementLocated(By.css('pre')))
85
assert.fail('Page should not be loaded')
86
} catch (e) {
87
assert.strictEqual(e.name, 'UnexpectedAlertOpenError')
88
}
89
})
90
91
it('throws an error when remove authentication handler that does not exist', async function () {
92
try {
93
await driver.network().removeAuthenticationHandler(10)
94
assert.fail('Expected error not thrown. Non-existent handler cannot be removed')
95
} catch (e) {
96
assert.strictEqual(e.message, 'Callback with id 10 not found')
97
}
98
})
99
100
it('can clear authentication handlers', async function () {
101
await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth')
102
103
await driver.network().addAuthenticationHandler('bottle', 'genie')
104
105
await driver.network().clearAuthenticationHandlers()
106
107
try {
108
await driver.get(Pages.basicAuth)
109
await driver.wait(until.elementLocated(By.css('pre')))
110
assert.fail('Page should not be loaded')
111
} catch (e) {
112
assert.strictEqual(e.name, 'UnexpectedAlertOpenError')
113
}
114
})
115
})
116
},
117
{ browsers: [Browser.FIREFOX] },
118
)
119
120