Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/script_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 BrowsingContext = require('selenium-webdriver/bidi/browsingContext')
24
const ScriptManager = require('selenium-webdriver/bidi/scriptManager')
25
const {
26
ChannelValue,
27
LocalValue,
28
ReferenceValue,
29
RemoteReferenceType,
30
} = require('selenium-webdriver/bidi/protocolValue')
31
const { ArgumentValue } = require('selenium-webdriver/bidi/argumentValue')
32
const { EvaluateResultType } = require('selenium-webdriver/bidi/evaluateResult')
33
const { ResultOwnership } = require('selenium-webdriver/bidi/resultOwnership')
34
const { RealmType } = require('selenium-webdriver/bidi/realmInfo')
35
const { WebDriverError } = require('selenium-webdriver/lib/error')
36
37
suite(
38
function (env) {
39
let driver
40
let manager
41
42
beforeEach(async function () {
43
driver = await env.builder().build()
44
})
45
46
afterEach(async function () {
47
await manager.close()
48
await driver.quit()
49
})
50
51
describe('Script Manager', function () {
52
it('can call function with declaration', async function () {
53
const id = await driver.getWindowHandle()
54
manager = await ScriptManager(id, driver)
55
56
const result = await manager.callFunctionInBrowsingContext(id, '()=>{return 1+2;}', false)
57
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
58
assert.notEqual(result.realmId, null)
59
assert.equal(result.result.type, 'number')
60
assert.notEqual(result.result.value, null)
61
assert.equal(result.result.value, 3)
62
})
63
64
it('can call function to get iframe browsing context', async function () {
65
await driver.get(Pages.iframePage)
66
const id = await driver.getWindowHandle()
67
manager = await ScriptManager(id, driver)
68
69
const result = await manager.callFunctionInBrowsingContext(
70
id,
71
'() => document.querySelector(\'iframe[id="iframe1"]\').contentWindow',
72
false,
73
)
74
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
75
assert.notEqual(result.realmId, null)
76
assert.equal(result.result.type, 'window')
77
assert.notEqual(result.result.value, null)
78
assert.notEqual(result.result.value.context, null)
79
})
80
81
it('can call function to get element', async function () {
82
await driver.get(Pages.logEntryAdded)
83
const id = await driver.getWindowHandle()
84
manager = await ScriptManager(id, driver)
85
86
const result = await manager.callFunctionInBrowsingContext(
87
id,
88
'() => document.getElementById("consoleLog")',
89
false,
90
)
91
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
92
assert.notEqual(result.realmId, null)
93
assert.equal(result.result.type, 'node')
94
assert.notEqual(result.result.value, null)
95
assert.notEqual(result.result.value.nodeType, null)
96
})
97
98
it('can call function with arguments', async function () {
99
const id = await driver.getWindowHandle()
100
manager = await ScriptManager(id, driver)
101
102
let argumentValues = []
103
let value1 = LocalValue.createStringValue('ARGUMENT_STRING_VALUE')
104
let value2 = LocalValue.createNumberValue(42)
105
argumentValues.push(value1)
106
argumentValues.push(value2)
107
108
const result = await manager.callFunctionInBrowsingContext(
109
id,
110
'(...args)=>{return args}',
111
false,
112
argumentValues,
113
)
114
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
115
assert.notEqual(result.realmId, null)
116
assert.equal(result.result.type, 'array')
117
assert.notEqual(result.result.value, null)
118
assert.equal(result.result.value.length, 2)
119
})
120
121
it('can call function with await promise', async function () {
122
const id = await driver.getWindowHandle()
123
manager = await ScriptManager(id, driver)
124
125
const result = await manager.callFunctionInBrowsingContext(
126
id,
127
'async function() {{\n' +
128
' await new Promise(r => setTimeout(() => r(), 0));\n' +
129
' return "SOME_DELAYED_RESULT";\n' +
130
' }}',
131
true,
132
)
133
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
134
assert.notEqual(result.realmId, null)
135
assert.equal(result.result.type, 'string')
136
assert.notEqual(result.result.value, null)
137
assert.equal(result.result.value, 'SOME_DELAYED_RESULT')
138
})
139
140
it('can call function with await promise false', async function () {
141
const id = await driver.getWindowHandle()
142
manager = await ScriptManager(id, driver)
143
144
const result = await manager.callFunctionInBrowsingContext(
145
id,
146
'async function() {{\n' +
147
' await new Promise(r => setTimeout(() => r(), 0));\n' +
148
' return "SOME_DELAYED_RESULT";\n' +
149
' }}',
150
false,
151
)
152
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
153
assert.notEqual(result.realmId, null)
154
assert.equal(result.result.type, 'promise')
155
assert.equal(result.result.value, undefined)
156
})
157
158
it('can call function with this parameter', async function () {
159
const id = await driver.getWindowHandle()
160
manager = await ScriptManager(id, driver)
161
162
let mapValue = { some_property: LocalValue.createNumberValue(42) }
163
let thisParameter = LocalValue.createObjectValue(mapValue).asMap()
164
165
const result = await manager.callFunctionInBrowsingContext(
166
id,
167
'function(){return this.some_property}',
168
false,
169
null,
170
thisParameter,
171
)
172
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
173
assert.notEqual(result.realmId, null)
174
assert.equal(result.result.type, 'number')
175
assert.notEqual(result.result.value, null)
176
assert.equal(result.result.value, 42)
177
})
178
179
it('can call function with ownership root', async function () {
180
const id = await driver.getWindowHandle()
181
manager = await ScriptManager(id, driver)
182
183
const result = await manager.callFunctionInBrowsingContext(
184
id,
185
'async function(){return {a:1}}',
186
true,
187
null,
188
null,
189
ResultOwnership.ROOT,
190
)
191
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
192
assert.notEqual(result.realmId, null)
193
assert.notEqual(result.result.handle, null)
194
assert.notEqual(result.result.value, null)
195
})
196
197
it('can call function with ownership none', async function () {
198
const id = await driver.getWindowHandle()
199
manager = await ScriptManager(id, driver)
200
201
const result = await manager.callFunctionInBrowsingContext(
202
id,
203
'async function(){return {a:1}}',
204
true,
205
null,
206
null,
207
ResultOwnership.NONE,
208
)
209
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
210
assert.notEqual(result.realmId, null)
211
assert.equal(result.result.handle, undefined)
212
assert.notEqual(result.result.value, null)
213
})
214
215
it('can call function that throws exception', async function () {
216
const id = await driver.getWindowHandle()
217
manager = await ScriptManager(id, driver)
218
219
const result = await manager.callFunctionInBrowsingContext(id, '))) !!@@## some invalid JS script (((', false)
220
assert.equal(result.resultType, EvaluateResultType.EXCEPTION)
221
assert.notEqual(result.realmId, null)
222
223
assert.equal(result.exceptionDetails.exception.type, 'error')
224
assert.equal(result.exceptionDetails.text.includes('SyntaxError:'), true)
225
assert.notEqual(result.exceptionDetails.columnNumber, null)
226
assert.equal(result.exceptionDetails.stackTrace.callFrames.length, 0)
227
})
228
229
it('can call function in a sandbox', async function () {
230
const id = await driver.getWindowHandle()
231
manager = await ScriptManager(id, driver)
232
233
// Make changes without sandbox
234
await manager.callFunctionInBrowsingContext(id, '() => { window.foo = 1; }', true)
235
236
// Check changes are not present in the sandbox
237
const resultNotInSandbox = await manager.callFunctionInBrowsingContext(
238
id,
239
'() => window.foo',
240
true,
241
null,
242
null,
243
null,
244
'sandbox',
245
)
246
247
assert.equal(resultNotInSandbox.resultType, EvaluateResultType.SUCCESS)
248
assert.equal(resultNotInSandbox.result.type, 'undefined')
249
250
// Make changes in the sandbox
251
252
await manager.callFunctionInBrowsingContext(id, '() => { window.foo = 2; }', true, null, null, null, 'sandbox')
253
254
// Check if the changes are present in the sandbox
255
256
const resultInSandbox = await manager.callFunctionInBrowsingContext(
257
id,
258
'() => window.foo',
259
true,
260
null,
261
null,
262
null,
263
'sandbox',
264
)
265
266
assert.equal(resultInSandbox.resultType, EvaluateResultType.SUCCESS)
267
assert.notEqual(resultInSandbox.realmId, null)
268
269
assert.equal(resultInSandbox.result.type, 'number')
270
assert.notEqual(resultInSandbox.result.value, null)
271
assert.equal(resultInSandbox.result.value, 2)
272
})
273
274
it('can call function in a realm', async function () {
275
const firstTab = await driver.getWindowHandle()
276
await driver.switchTo().newWindow('tab')
277
manager = await ScriptManager(firstTab, driver)
278
279
const realms = await manager.getAllRealms()
280
const firstTabRealmId = realms[0].realmId
281
const secondTabRealmId = realms[1].realmId
282
283
await manager.callFunctionInRealm(firstTabRealmId, '() => { window.foo = 3; }', true)
284
285
await manager.callFunctionInRealm(secondTabRealmId, '() => { window.foo = 5; }', true)
286
287
const firstContextResult = await manager.callFunctionInRealm(firstTabRealmId, '() => window.foo', true)
288
289
assert.equal(firstContextResult.resultType, EvaluateResultType.SUCCESS)
290
assert.equal(firstContextResult.result.type, 'number')
291
assert.notEqual(firstContextResult.result.value, null)
292
assert.equal(firstContextResult.result.value, 3)
293
294
const secondContextResult = await manager.callFunctionInRealm(secondTabRealmId, '() => window.foo', true)
295
296
assert.equal(secondContextResult.resultType, EvaluateResultType.SUCCESS)
297
assert.equal(secondContextResult.result.type, 'number')
298
assert.notEqual(secondContextResult.result.value, null)
299
assert.equal(secondContextResult.result.value, 5)
300
})
301
302
it('can evaluate script', async function () {
303
const id = await driver.getWindowHandle()
304
manager = await ScriptManager(id, driver)
305
306
const result = await manager.evaluateFunctionInBrowsingContext(id, '1 + 2', true)
307
308
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
309
assert.notEqual(result.realmId, null)
310
assert.equal(result.result.type, 'number')
311
assert.notEqual(result.result.value, null)
312
assert.equal(result.result.value, 3)
313
})
314
315
it('can evaluate script that throws exception', async function () {
316
const id = await driver.getWindowHandle()
317
manager = await ScriptManager(id, driver)
318
319
const result = await manager.evaluateFunctionInBrowsingContext(
320
id,
321
'))) !!@@## some invalid JS script (((',
322
false,
323
)
324
325
assert.equal(result.resultType, EvaluateResultType.EXCEPTION)
326
assert.notEqual(result.realmId, null)
327
328
assert.equal(result.exceptionDetails.exception.type, 'error')
329
assert.equal(result.exceptionDetails.text.includes('SyntaxError:'), true)
330
assert.notEqual(result.exceptionDetails.columnNumber, null)
331
assert.equal(result.exceptionDetails.stackTrace.callFrames.length, 0)
332
})
333
334
it('can evaluate script with result ownership', async function () {
335
const id = await driver.getWindowHandle()
336
manager = await ScriptManager(id, driver)
337
338
const result = await manager.evaluateFunctionInBrowsingContext(
339
id,
340
'Promise.resolve({a:1})',
341
true,
342
ResultOwnership.ROOT,
343
)
344
345
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
346
assert.notEqual(result.realmId, null)
347
assert.equal(result.result.type, 'object')
348
assert.notEqual(result.result.value, null)
349
assert.notEqual(result.result.handle, null)
350
})
351
352
it('can evaluate in a sandbox', async function () {
353
const id = await driver.getWindowHandle()
354
manager = await ScriptManager(id, driver)
355
356
// Make changes without sandbox
357
await manager.evaluateFunctionInBrowsingContext(id, 'window.foo = 1', true)
358
359
// Check changes are not present in the sandbox
360
const resultNotInSandbox = await manager.evaluateFunctionInBrowsingContext(
361
id,
362
'window.foo',
363
true,
364
null,
365
'sandbox',
366
)
367
368
assert.equal(resultNotInSandbox.resultType, EvaluateResultType.SUCCESS)
369
assert.equal(resultNotInSandbox.result.type, 'undefined')
370
371
// Make changes in the sandbox
372
await manager.evaluateFunctionInBrowsingContext(id, 'window.foo = 2', true, null, 'sandbox')
373
374
const resultInSandbox = await manager.evaluateFunctionInBrowsingContext(id, 'window.foo', true, null, 'sandbox')
375
376
assert.equal(resultInSandbox.resultType, EvaluateResultType.SUCCESS)
377
assert.notEqual(resultInSandbox.realmId, null)
378
379
assert.equal(resultInSandbox.result.type, 'number')
380
assert.notEqual(resultInSandbox.result.value, null)
381
assert.equal(resultInSandbox.result.value, 2)
382
})
383
384
it('can evaluate in a realm', async function () {
385
const firstTab = await driver.getWindowHandle()
386
await driver.switchTo().newWindow('tab')
387
manager = await ScriptManager(firstTab, driver)
388
389
const realms = await manager.getAllRealms()
390
const firstTabRealmId = realms[0].realmId
391
const secondTabRealmId = realms[1].realmId
392
393
await manager.evaluateFunctionInRealm(firstTabRealmId, 'window.foo = 3', true)
394
395
await manager.evaluateFunctionInRealm(secondTabRealmId, 'window.foo = 5', true)
396
397
const firstContextResult = await manager.evaluateFunctionInRealm(firstTabRealmId, 'window.foo', true)
398
399
assert.equal(firstContextResult.resultType, EvaluateResultType.SUCCESS)
400
assert.equal(firstContextResult.result.type, 'number')
401
assert.notEqual(firstContextResult.result.value, null)
402
assert.equal(firstContextResult.result.value, 3)
403
404
const secondContextResult = await manager.evaluateFunctionInRealm(secondTabRealmId, 'window.foo', true)
405
406
assert.equal(secondContextResult.resultType, EvaluateResultType.SUCCESS)
407
assert.equal(secondContextResult.result.type, 'number')
408
assert.notEqual(secondContextResult.result.value, null)
409
assert.equal(secondContextResult.result.value, 5)
410
})
411
412
it('can disown handles', async function () {
413
const id = await driver.getWindowHandle()
414
manager = await ScriptManager(id, driver)
415
416
const evaluateResult = await manager.evaluateFunctionInBrowsingContext(
417
id,
418
'({a:1})',
419
false,
420
ResultOwnership.ROOT,
421
)
422
423
assert.equal(evaluateResult.resultType, EvaluateResultType.SUCCESS)
424
assert.notEqual(evaluateResult.realmId, null)
425
assert.notEqual(evaluateResult.result.handle, null)
426
427
let argumentValues = []
428
let valueMap = evaluateResult.result.value
429
430
let value1 = LocalValue.createObjectValue(valueMap)
431
let value2 = new ReferenceValue(RemoteReferenceType.HANDLE, evaluateResult.result.handle)
432
argumentValues.push(value1)
433
argumentValues.push(value2)
434
435
await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues)
436
437
assert.notEqual(evaluateResult.result.value, null)
438
439
let handles = [evaluateResult.result.handle]
440
await manager.disownBrowsingContextScript(id, handles)
441
442
await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues).catch((error) => {
443
assert(error instanceof TypeError)
444
})
445
})
446
447
it('can disown handles in realm', async function () {
448
const id = await driver.getWindowHandle()
449
manager = await ScriptManager(id, driver)
450
451
const evaluateResult = await manager.evaluateFunctionInBrowsingContext(
452
id,
453
'({a:1})',
454
false,
455
ResultOwnership.ROOT,
456
)
457
458
assert.equal(evaluateResult.resultType, EvaluateResultType.SUCCESS)
459
assert.notEqual(evaluateResult.realmId, null)
460
assert.notEqual(evaluateResult.result.handle, null)
461
462
let argumentValues = []
463
let valueMap = evaluateResult.result.value
464
465
let value1 = LocalValue.createObjectValue(valueMap)
466
let value2 = new ReferenceValue(RemoteReferenceType.HANDLE, evaluateResult.result.handle)
467
argumentValues.push(value1)
468
argumentValues.push(value2)
469
470
await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues)
471
472
assert.notEqual(evaluateResult.result.value, null)
473
474
let handles = [evaluateResult.result.handle]
475
await manager.disownRealmScript(evaluateResult.realmId, handles)
476
477
await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues).catch((error) => {
478
assert(error instanceof TypeError)
479
})
480
})
481
482
it('can get all realms', async function () {
483
const firstWindow = await driver.getWindowHandle()
484
await driver.switchTo().newWindow('window')
485
const secondWindow = await driver.getWindowHandle()
486
manager = await ScriptManager(firstWindow, driver)
487
488
const realms = await manager.getAllRealms()
489
assert.equal(realms.length, 2)
490
491
const firstWindowRealm = realms[0]
492
assert.equal(firstWindowRealm.realmType, RealmType.WINDOW)
493
assert.notEqual(firstWindowRealm.realmId, null)
494
assert.equal(firstWindowRealm.browsingContext, firstWindow)
495
496
const secondWindowRealm = realms[1]
497
assert.equal(secondWindowRealm.realmType, RealmType.WINDOW)
498
assert.notEqual(secondWindowRealm.realmId, null)
499
assert.equal(secondWindowRealm.browsingContext, secondWindow)
500
})
501
502
it('can get realm by type', async function () {
503
const firstWindow = await driver.getWindowHandle()
504
await driver.switchTo().newWindow('window')
505
const secondWindow = await driver.getWindowHandle()
506
manager = await ScriptManager(firstWindow, driver)
507
508
const realms = await manager.getRealmsByType(RealmType.WINDOW)
509
assert.equal(realms.length, 2)
510
511
const firstWindowRealm = realms[0]
512
assert.equal(firstWindowRealm.realmType, RealmType.WINDOW)
513
assert.notEqual(firstWindowRealm.realmId, null)
514
assert.equal(firstWindowRealm.browsingContext, firstWindow)
515
516
const secondWindowRealm = realms[1]
517
assert.equal(secondWindowRealm.realmType, RealmType.WINDOW)
518
assert.notEqual(secondWindowRealm.realmId, null)
519
assert.equal(secondWindowRealm.browsingContext, secondWindow)
520
})
521
522
it('can get realm in browsing context', async function () {
523
const windowId = await driver.getWindowHandle()
524
await driver.switchTo().newWindow('tab')
525
const tabId = await driver.getWindowHandle()
526
manager = await ScriptManager(windowId, driver)
527
528
const realms = await manager.getRealmsInBrowsingContext(tabId)
529
530
const tabRealm = realms[0]
531
assert.equal(tabRealm.realmType, RealmType.WINDOW)
532
assert.notEqual(tabRealm.realmId, null)
533
assert.equal(tabRealm.browsingContext, tabId)
534
})
535
536
it('can get realm in browsing context by type', async function () {
537
const windowId = await driver.getWindowHandle()
538
await driver.switchTo().newWindow('tab')
539
manager = await ScriptManager(windowId, driver)
540
541
const realms = await manager.getRealmsInBrowsingContextByType(windowId, RealmType.WINDOW)
542
543
const windowRealm = realms[0]
544
assert.equal(windowRealm.realmType, RealmType.WINDOW)
545
assert.notEqual(windowRealm.realmId, null)
546
assert.equal(windowRealm.browsingContext, windowId)
547
})
548
549
it('can add preload script test', async function () {
550
const id = await driver.getWindowHandle()
551
manager = await ScriptManager([], driver)
552
553
await manager.addPreloadScript("() => { window.foo='bar'; }")
554
555
// Check that preload script didn't apply the changes to the current context
556
let result = await manager.evaluateFunctionInBrowsingContext(id, 'window.foo', true)
557
assert.equal(result.result.type, 'undefined')
558
559
await driver.switchTo().newWindow('window')
560
const new_window_id = await driver.getWindowHandle()
561
562
// Check that preload script applied the changes to the window
563
result = await manager.evaluateFunctionInBrowsingContext(new_window_id, 'window.foo', true)
564
565
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
566
assert.notEqual(result.realmId, null)
567
assert.equal(result.result.type, 'string')
568
assert.notEqual(result.result.value, null)
569
assert.equal(result.result.value, 'bar')
570
571
const browsingContext = await BrowsingContext(driver, {
572
type: 'tab',
573
})
574
575
await browsingContext.navigate(Pages.logEntryAdded, 'complete')
576
577
// Check that preload script was applied after navigation
578
result = await manager.evaluateFunctionInBrowsingContext(new_window_id, 'window.foo', true)
579
580
assert.equal(result.result.type, 'string')
581
assert.equal(result.result.value, 'bar')
582
})
583
584
it('can add same preload script twice', async function () {
585
const id = await driver.getWindowHandle()
586
manager = await ScriptManager(id, driver)
587
588
const script_1 = await manager.addPreloadScript('() => { return 42; }')
589
const script_2 = await manager.addPreloadScript('() => { return 42; }')
590
591
assert.notEqual(script_1, script_2)
592
})
593
594
it('can access preload script properties', async function () {
595
manager = await ScriptManager([], driver)
596
597
await manager.addPreloadScript('() => { window.preloadScriptFunction = () => window.baz = 42; }')
598
599
await driver.switchTo().newWindow('tab')
600
const new_tab_id = await driver.getWindowHandle()
601
await driver.get(Pages.scriptTestAccessProperty)
602
603
const result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.baz', true)
604
605
assert.equal(result.result.type, 'number')
606
assert.equal(result.result.value, 42)
607
})
608
609
it('can add preload script to sandbox', async function () {
610
manager = await ScriptManager([], driver)
611
612
await manager.addPreloadScript('() => { window.foo = 1; }')
613
await manager.addPreloadScript('() => { window.bar = 2; }', [], 'sandbox')
614
615
await driver.switchTo().newWindow('tab')
616
const new_tab_id = await driver.getWindowHandle()
617
618
let result_in_sandbox = await manager.evaluateFunctionInBrowsingContext(
619
new_tab_id,
620
'window.foo',
621
true,
622
null,
623
'sandbox',
624
)
625
626
assert.equal(result_in_sandbox.result.type, 'undefined')
627
628
let result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.bar', true)
629
630
assert.equal(result.result.type, 'undefined')
631
632
result_in_sandbox = await manager.evaluateFunctionInBrowsingContext(
633
new_tab_id,
634
'window.bar',
635
true,
636
null,
637
'sandbox',
638
)
639
640
assert.equal(result_in_sandbox.result.type, 'number')
641
assert.equal(result_in_sandbox.result.value, 2)
642
})
643
644
it('can remove properties set by preload script', async function () {
645
manager = await ScriptManager([], driver)
646
647
await manager.addPreloadScript('() => { window.foo = 42; }')
648
await manager.addPreloadScript('() => { window.foo = 50; }', [], 'sandbox_1')
649
650
await driver.switchTo().newWindow('tab')
651
const new_tab_id = await driver.getWindowHandle()
652
await driver.get(Pages.scriptTestRemoveProperty)
653
654
let result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.foo', true)
655
assert.equal(result.result.type, 'undefined')
656
657
result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.foo', true, null, 'sandbox_1')
658
assert.equal(result.result.type, 'number')
659
assert.equal(result.result.value, 50)
660
})
661
662
it('can remove preload script', async function () {
663
manager = await ScriptManager([], driver)
664
665
let script = await manager.addPreloadScript("() => { window.foo='bar'; }")
666
667
await driver.switchTo().newWindow('tab')
668
const tab_1_id = await driver.getWindowHandle()
669
670
let result = await manager.evaluateFunctionInBrowsingContext(tab_1_id, 'window.foo', true)
671
672
assert.equal(result.result.type, 'string')
673
assert.equal(result.result.value, 'bar')
674
675
await manager.removePreloadScript(script)
676
677
await driver.switchTo().newWindow('tab')
678
const tab_2_id = await driver.getWindowHandle()
679
680
// Check that changes from preload script were not applied after script was removed
681
result = await manager.evaluateFunctionInBrowsingContext(tab_2_id, 'window.foo', true)
682
683
assert.equal(result.result.type, 'undefined')
684
})
685
686
it('cannot remove same preload script twice', async function () {
687
manager = await ScriptManager([], driver)
688
689
let script = await manager.addPreloadScript("() => { window.foo='bar'; }")
690
691
await manager.removePreloadScript(script)
692
693
await manager.removePreloadScript(script).catch((error) => {
694
assert(error instanceof WebDriverError)
695
})
696
})
697
698
it('can remove one of preload script', async function () {
699
manager = await ScriptManager([], driver)
700
701
let script_1 = await manager.addPreloadScript("() => { window.bar='foo'; }")
702
703
let script_2 = await manager.addPreloadScript("() => { window.baz='bar'; }")
704
705
await manager.removePreloadScript(script_1)
706
707
await driver.switchTo().newWindow('tab')
708
const new_tab_id = await driver.getWindowHandle()
709
710
// Check that the first script didn't run
711
let result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.bar', true)
712
713
assert.equal(result.result.type, 'undefined')
714
715
// Check that the second script still applied the changes to the window
716
result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.baz', true)
717
718
assert.equal(result.result.type, 'string')
719
assert.equal(result.result.value, 'bar')
720
721
// Clean up the second script
722
await manager.removePreloadScript(script_2)
723
})
724
725
it('can remove one of preload script from sandbox', async function () {
726
const id = await driver.getWindowHandle()
727
manager = await ScriptManager(id, driver)
728
729
let script_1 = await manager.addPreloadScript('() => { window.foo = 1; }')
730
731
let script_2 = await manager.addPreloadScript('() => { window.bar = 2; }', [], 'sandbox')
732
733
// Remove first preload script
734
await manager.removePreloadScript(script_1)
735
736
// Remove second preload script
737
await manager.removePreloadScript(script_2)
738
739
await driver.switchTo().newWindow('tab')
740
const new_tab_id = await driver.getWindowHandle()
741
742
// Make sure that changes from first preload script were not applied
743
let result_in_window = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.foo', true)
744
745
assert.equal(result_in_window.result.type, 'undefined')
746
747
// Make sure that changes from second preload script were not applied
748
let result_in_sandbox = await manager.evaluateFunctionInBrowsingContext(
749
new_tab_id,
750
'window.bar',
751
true,
752
null,
753
'sandbox',
754
)
755
756
assert.equal(result_in_sandbox.result.type, 'undefined')
757
})
758
759
it('can listen to channel message', async function () {
760
manager = await ScriptManager(undefined, driver)
761
762
let message = null
763
764
await manager.onMessage((m) => {
765
message = m
766
})
767
768
let argumentValues = []
769
let value = LocalValue.createChannelValue(new ChannelValue('channel_name'))
770
argumentValues.push(value)
771
772
const result = await manager.callFunctionInBrowsingContext(
773
await driver.getWindowHandle(),
774
'(channel) => channel("foo")',
775
false,
776
argumentValues,
777
)
778
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
779
assert.notEqual(message, null)
780
assert.equal(message.channel, 'channel_name')
781
assert.equal(message.data.type, 'string')
782
assert.equal(message.data.value, 'foo')
783
})
784
785
it('can listen to realm created message', async function () {
786
manager = await ScriptManager(undefined, driver)
787
788
let realmInfo = null
789
790
await manager.onRealmCreated((result) => {
791
realmInfo = result
792
})
793
794
const id = await driver.getWindowHandle()
795
const browsingContext = await BrowsingContext(driver, {
796
browsingContextId: id,
797
})
798
799
await browsingContext.navigate(Pages.blankPage, 'complete')
800
801
assert.notEqual(realmInfo, null)
802
assert.notEqual(realmInfo.realmId, null)
803
assert.equal(realmInfo.realmType, RealmType.WINDOW)
804
})
805
806
xit('can listen to realm destroyed message', async function () {
807
manager = await ScriptManager(undefined, driver)
808
809
let realmInfo = null
810
811
await manager.onRealmDestroyed((result) => {
812
realmInfo = result
813
})
814
815
const id = await driver.getWindowHandle()
816
const browsingContext = await BrowsingContext(driver, {
817
browsingContextId: id,
818
})
819
820
await browsingContext.close()
821
822
assert.notEqual(realmInfo, null)
823
assert.notEqual(realmInfo.realmId, null)
824
assert.equal(realmInfo.realmType, RealmType.WINDOW)
825
})
826
})
827
},
828
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
829
)
830
831