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