Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/chrome/options_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 fs = require('node:fs')
22
const chrome = require('selenium-webdriver/chrome')
23
const symbols = require('selenium-webdriver/lib/symbols')
24
const test = require('../../lib/test')
25
const { locate } = require('../../lib/test/resources')
26
27
const WEBEXTENSION_CRX = locate('common/extensions/webextensions-selenium-example.crx')
28
29
describe('chrome.Options', function () {
30
describe('addArguments', function () {
31
it('takes var_args', function () {
32
let options = new chrome.Options()
33
assert.deepStrictEqual(options[symbols.serialize](), {
34
browserName: 'chrome',
35
'goog:chromeOptions': {},
36
})
37
38
options.addArguments('a', 'b')
39
assert.deepStrictEqual(options[symbols.serialize](), {
40
browserName: 'chrome',
41
'goog:chromeOptions': {
42
args: ['a', 'b'],
43
},
44
})
45
})
46
47
it('flattens input arrays', function () {
48
let options = new chrome.Options()
49
assert.deepStrictEqual(options[symbols.serialize](), {
50
browserName: 'chrome',
51
'goog:chromeOptions': {},
52
})
53
54
options.addArguments(['a', 'b'], 'c', [1, 2], 3)
55
assert.deepStrictEqual(options[symbols.serialize](), {
56
browserName: 'chrome',
57
'goog:chromeOptions': {
58
args: ['a', 'b', 'c', 1, 2, 3],
59
},
60
})
61
})
62
})
63
64
describe('addExtensions', function () {
65
it('takes var_args', function () {
66
let options = new chrome.Options()
67
assert.strictEqual(options.options_.extensions, undefined)
68
69
options.addExtensions('a', 'b')
70
assert.deepStrictEqual(options.options_.extensions.extensions, ['a', 'b'])
71
})
72
73
it('flattens input arrays', function () {
74
let options = new chrome.Options()
75
assert.strictEqual(options.options_.extensions, undefined)
76
77
options.addExtensions(['a', 'b'], 'c', [1, 2], 3)
78
assert.deepStrictEqual(options.options_.extensions.extensions, ['a', 'b', 'c', 1, 2, 3])
79
})
80
})
81
82
describe('serialize', function () {
83
it('base64 encodes extensions', async function () {
84
let expected = fs.readFileSync(WEBEXTENSION_CRX, 'base64')
85
let wire = new chrome.Options().addExtensions(WEBEXTENSION_CRX)[symbols.serialize]()
86
87
let extensions = wire['goog:chromeOptions'].extensions[symbols.serialize]()
88
assert.strictEqual(extensions.length, 1)
89
assert.strictEqual(await extensions[0], expected)
90
})
91
})
92
93
describe('windowTypes', function () {
94
it('takes var_args', function () {
95
let options = new chrome.Options()
96
assert.strictEqual(options.options_.windowTypes, undefined)
97
98
options.windowTypes('a', 'b')
99
assert.deepStrictEqual(options.options_.windowTypes, ['a', 'b'])
100
})
101
102
it('flattens input arrays', function () {
103
let options = new chrome.Options()
104
assert.strictEqual(options.options_.windowTypes, undefined)
105
106
options.windowTypes(['a', 'b'], 'c', [1, 2], 3)
107
assert.deepStrictEqual(options.options_.windowTypes, ['a', 'b', 'c', 1, 2, 3])
108
})
109
})
110
})
111
112
test.suite(
113
function (env) {
114
var driver
115
116
beforeEach(function () {
117
driver = null
118
})
119
120
afterEach(function () {
121
return driver && driver.quit()
122
})
123
124
describe('Chrome options', function () {
125
it('can start Chrome with custom args', async function () {
126
const options = env.builder().getChromeOptions() || new chrome.Options()
127
options.addArguments('user-agent=foo;bar')
128
129
driver = await env.builder().setChromeOptions(options).build()
130
131
await driver.get(test.Pages.ajaxyPage)
132
133
const userAgent = await driver.executeScript('return window.navigator.userAgent')
134
assert.strictEqual(userAgent, 'foo;bar')
135
})
136
137
it('can start chromium with network conditions set', async function () {
138
driver = await env.builder().build()
139
await driver.get(test.Pages.ajaxyPage)
140
await driver.setNetworkConditions({
141
offline: true,
142
latency: 0,
143
download_throughput: 0,
144
upload_throughput: 0,
145
})
146
assert.deepStrictEqual(await driver.getNetworkConditions(), {
147
download_throughput: 0,
148
latency: 0,
149
offline: true,
150
upload_throughput: 0,
151
})
152
await driver.deleteNetworkConditions()
153
})
154
155
it('can install an extension from path', async function () {
156
let options = env.builder().getChromeOptions() || new chrome.Options()
157
options.addExtensions(WEBEXTENSION_CRX)
158
159
driver = await env.builder().forBrowser('chrome').setChromeOptions(options).build()
160
161
await driver.get(test.Pages.echoPage)
162
await verifyWebExtensionWasInstalled()
163
})
164
165
it('can install an extension from Buffer', async function () {
166
let options = env.builder().getChromeOptions() || new chrome.Options()
167
options.addExtensions(fs.readFileSync(WEBEXTENSION_CRX))
168
169
driver = await env.builder().forBrowser('chrome').setChromeOptions(options).build()
170
171
await driver.get(test.Pages.echoPage)
172
await verifyWebExtensionWasInstalled()
173
})
174
175
async function verifyWebExtensionWasInstalled() {
176
let footer = await driver.findElement({
177
id: 'webextensions-selenium-example',
178
})
179
let text = await footer.getText()
180
assert.strictEqual(text, 'Content injected by webextensions-selenium-example')
181
}
182
})
183
},
184
{ browsers: ['chrome'] },
185
)
186
187