Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/generated/emulation_test.js
11822 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 { suite, ignore, Pages } = require('../../../lib/test')
22
const { Browser } = require('selenium-webdriver')
23
24
const { Emulation } = require('selenium-webdriver/bidi/generated/emulation')
25
const { Script } = require('selenium-webdriver/bidi/generated/script')
26
const { BrowsingContext } = require('selenium-webdriver/bidi/generated/browsing_context')
27
28
suite(
29
function (env) {
30
let driver
31
let emulation
32
let script
33
let browsingContext
34
35
beforeEach(async function () {
36
driver = await env.builder().build()
37
emulation = await Emulation.create(driver)
38
script = await Script.create(driver)
39
browsingContext = await BrowsingContext.create(driver)
40
await driver.get(Pages.emptyPage)
41
})
42
43
afterEach(function () {
44
return driver.quit()
45
})
46
47
describe('setUserAgentOverride', function () {
48
it('can override the user agent', async function () {
49
const contextId = await driver.getWindowHandle()
50
const customUA = 'MyCustomUA/1.0'
51
52
await emulation.setUserAgentOverride({
53
userAgent: customUA,
54
contexts: [contextId],
55
})
56
57
// Navigate after setting override so the new UA is used for the request
58
await driver.get(Pages.emptyPage)
59
const ua = await driver.executeScript('return navigator.userAgent')
60
assert.strictEqual(ua, customUA)
61
})
62
63
it('can clear the user agent override', async function () {
64
const contextId = await driver.getWindowHandle()
65
66
await emulation.setUserAgentOverride({ userAgent: 'test-ua', contexts: [contextId] })
67
await driver.get(Pages.emptyPage)
68
69
// Clear by passing null
70
await emulation.setUserAgentOverride({ userAgent: null, contexts: [contextId] })
71
await driver.get(Pages.emptyPage)
72
73
const ua = await driver.executeScript('return navigator.userAgent')
74
// After clearing, user agent should not be 'test-ua'
75
assert.notStrictEqual(ua, 'test-ua')
76
})
77
})
78
79
describe('setTimezoneOverride', function () {
80
it('can override the timezone', async function () {
81
// Open a fresh tab, set the override for it, navigate, then evaluate.
82
// Using a new context ensures the override is applied before any page
83
// load so the new realm picks it up from the start.
84
const { context: newContextId } = await browsingContext.create({ type: 'tab' })
85
86
await emulation.setTimezoneOverride({
87
timezone: 'America/New_York',
88
contexts: [newContextId],
89
})
90
91
await browsingContext.navigate({
92
context: newContextId,
93
url: Pages.emptyPage,
94
wait: 'complete',
95
})
96
97
const evalResult = await script.evaluate({
98
expression: 'Intl.DateTimeFormat().resolvedOptions().timeZone',
99
target: { context: newContextId },
100
awaitPromise: false,
101
})
102
103
await browsingContext.close({ context: newContextId })
104
assert.strictEqual(evalResult.result.value, 'America/New_York')
105
})
106
107
it('can set UTC timezone', async function () {
108
const { context: newContextId } = await browsingContext.create({ type: 'tab' })
109
110
await emulation.setTimezoneOverride({
111
timezone: 'UTC',
112
contexts: [newContextId],
113
})
114
115
await browsingContext.navigate({
116
context: newContextId,
117
url: Pages.emptyPage,
118
wait: 'complete',
119
})
120
121
const evalResult = await script.evaluate({
122
expression: 'Intl.DateTimeFormat().resolvedOptions().timeZone',
123
target: { context: newContextId },
124
awaitPromise: false,
125
})
126
127
await browsingContext.close({ context: newContextId })
128
assert.strictEqual(evalResult.result.value, 'UTC')
129
})
130
})
131
132
describe('setLocaleOverride', function () {
133
it('can override the locale', async function () {
134
// Same fresh-tab approach as setTimezoneOverride — set override before
135
// any page load so the new realm picks it up from the start.
136
const { context: newContextId } = await browsingContext.create({ type: 'tab' })
137
138
await emulation.setLocaleOverride({
139
locale: 'fr-FR',
140
contexts: [newContextId],
141
})
142
143
await browsingContext.navigate({
144
context: newContextId,
145
url: Pages.emptyPage,
146
wait: 'complete',
147
})
148
149
const evalResult = await script.evaluate({
150
expression: 'Intl.DateTimeFormat().resolvedOptions().locale',
151
target: { context: newContextId },
152
awaitPromise: false,
153
})
154
155
await browsingContext.close({ context: newContextId })
156
assert.ok(
157
evalResult.result.value.startsWith('fr'),
158
`locale should start with 'fr', got '${evalResult.result.value}'`,
159
)
160
})
161
162
it('can clear the locale override', async function () {
163
const contextId = await driver.getWindowHandle()
164
165
await emulation.setLocaleOverride({ locale: 'ja-JP', contexts: [contextId] })
166
// Clear by passing null
167
await emulation.setLocaleOverride({ locale: null, contexts: [contextId] })
168
})
169
})
170
171
describe('setNetworkConditions', function () {
172
it('can set network conditions to offline', async function () {
173
const contextId = await driver.getWindowHandle()
174
175
await emulation.setNetworkConditions({
176
networkConditions: { type: 'offline' },
177
contexts: [contextId],
178
})
179
180
// Restore
181
await emulation.setNetworkConditions({
182
networkConditions: null,
183
contexts: [contextId],
184
})
185
})
186
187
it('can clear network conditions', async function () {
188
const contextId = await driver.getWindowHandle()
189
190
await emulation.setNetworkConditions({
191
networkConditions: { type: 'offline' },
192
contexts: [contextId],
193
})
194
await emulation.setNetworkConditions({
195
networkConditions: null,
196
contexts: [contextId],
197
})
198
})
199
})
200
201
describe('setGeolocationOverride', function () {
202
it('can override geolocation coordinates', async function () {
203
const contextId = await driver.getWindowHandle()
204
205
await emulation.setGeolocationOverride({
206
coordinates: {
207
latitude: 37.7749,
208
longitude: -122.4194,
209
accuracy: 10,
210
},
211
contexts: [contextId],
212
})
213
})
214
215
it('can clear geolocation override', async function () {
216
const contextId = await driver.getWindowHandle()
217
218
await emulation.setGeolocationOverride({
219
coordinates: { latitude: 0, longitude: 0, accuracy: 1 },
220
contexts: [contextId],
221
})
222
await emulation.setGeolocationOverride({
223
coordinates: null,
224
contexts: [contextId],
225
})
226
})
227
})
228
229
describe('setScreenOrientationOverride', function () {
230
it('can override screen orientation to portrait', async function () {
231
const contextId = await driver.getWindowHandle()
232
233
await emulation.setScreenOrientationOverride({
234
screenOrientation: { natural: 'portrait', type: 'portrait-primary' },
235
contexts: [contextId],
236
})
237
})
238
239
it('can override screen orientation to landscape', async function () {
240
const contextId = await driver.getWindowHandle()
241
242
await emulation.setScreenOrientationOverride({
243
screenOrientation: { natural: 'landscape', type: 'landscape-primary' },
244
contexts: [contextId],
245
})
246
})
247
248
it('can clear screen orientation override', async function () {
249
const contextId = await driver.getWindowHandle()
250
251
await emulation.setScreenOrientationOverride({
252
screenOrientation: { natural: 'portrait', type: 'portrait-primary' },
253
contexts: [contextId],
254
})
255
await emulation.setScreenOrientationOverride({
256
screenOrientation: null,
257
contexts: [contextId],
258
})
259
})
260
})
261
262
ignore(env.browsers(Browser.FIREFOX)).describe('setScriptingEnabled', function () {
263
it('can disable scripting for a context', async function () {
264
const contextId = await driver.getWindowHandle()
265
266
await emulation.setScriptingEnabled({
267
enabled: false,
268
contexts: [contextId],
269
})
270
271
// Restore by passing null
272
await emulation.setScriptingEnabled({
273
enabled: null,
274
contexts: [contextId],
275
})
276
})
277
})
278
279
ignore(env.browsers(Browser.FIREFOX)).describe('setTouchOverride', function () {
280
it('can enable touch emulation', async function () {
281
const contextId = await driver.getWindowHandle()
282
283
await emulation.setTouchOverride({
284
maxTouchPoints: 5,
285
contexts: [contextId],
286
})
287
288
// Restore by passing null
289
await emulation.setTouchOverride({
290
maxTouchPoints: null,
291
contexts: [contextId],
292
})
293
})
294
})
295
},
296
{ browsers: [Browser.CHROME, Browser.FIREFOX] },
297
)
298
299