Path: blob/trunk/javascript/selenium-webdriver/test/bidi/script_test.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const { Browser } = require('selenium-webdriver')21const { Pages, suite } = require('../../lib/test')22const BrowsingContext = require('selenium-webdriver/bidi/browsingContext')23const ScriptManager = require('selenium-webdriver/bidi/scriptManager')24const {25ChannelValue,26LocalValue,27ReferenceValue,28RemoteReferenceType,29} = require('selenium-webdriver/bidi/protocolValue')30const { ArgumentValue } = require('selenium-webdriver/bidi/argumentValue')31const { EvaluateResultType } = require('selenium-webdriver/bidi/evaluateResult')32const { ResultOwnership } = require('selenium-webdriver/bidi/resultOwnership')33const { RealmType } = require('selenium-webdriver/bidi/realmInfo')34const { WebDriverError } = require('selenium-webdriver/lib/error')3536suite(37function (env) {38let driver39let manager4041beforeEach(async function () {42driver = await env.builder().build()43})4445afterEach(async function () {46await manager.close()47await driver.quit()48})4950describe('Script Manager', function () {51it('can call function with declaration', async function () {52const id = await driver.getWindowHandle()53manager = await ScriptManager(id, driver)5455const result = await manager.callFunctionInBrowsingContext(id, '()=>{return 1+2;}', false)56assert.equal(result.resultType, EvaluateResultType.SUCCESS)57assert.notEqual(result.realmId, null)58assert.equal(result.result.type, 'number')59assert.notEqual(result.result.value, null)60assert.equal(result.result.value, 3)61})6263it('can call function to get iframe browsing context', async function () {64await driver.get(Pages.iframePage)65const id = await driver.getWindowHandle()66manager = await ScriptManager(id, driver)6768const result = await manager.callFunctionInBrowsingContext(69id,70'() => document.querySelector(\'iframe[id="iframe1"]\').contentWindow',71false,72)73assert.equal(result.resultType, EvaluateResultType.SUCCESS)74assert.notEqual(result.realmId, null)75assert.equal(result.result.type, 'window')76assert.notEqual(result.result.value, null)77assert.notEqual(result.result.value.context, null)78})7980it('can call function to get element', async function () {81await driver.get(Pages.logEntryAdded)82const id = await driver.getWindowHandle()83manager = await ScriptManager(id, driver)8485const result = await manager.callFunctionInBrowsingContext(86id,87'() => document.getElementById("consoleLog")',88false,89)90assert.equal(result.resultType, EvaluateResultType.SUCCESS)91assert.notEqual(result.realmId, null)92assert.equal(result.result.type, 'node')93assert.notEqual(result.result.value, null)94assert.notEqual(result.result.value.nodeType, null)95})9697it('can call function with arguments', async function () {98const id = await driver.getWindowHandle()99manager = await ScriptManager(id, driver)100101let argumentValues = []102let value1 = LocalValue.createStringValue('ARGUMENT_STRING_VALUE')103let value2 = LocalValue.createNumberValue(42)104argumentValues.push(value1)105argumentValues.push(value2)106107const result = await manager.callFunctionInBrowsingContext(108id,109'(...args)=>{return args}',110false,111argumentValues,112)113assert.equal(result.resultType, EvaluateResultType.SUCCESS)114assert.notEqual(result.realmId, null)115assert.equal(result.result.type, 'array')116assert.notEqual(result.result.value, null)117assert.equal(result.result.value.length, 2)118})119120it('can call function with await promise', async function () {121const id = await driver.getWindowHandle()122manager = await ScriptManager(id, driver)123124const result = await manager.callFunctionInBrowsingContext(125id,126'async function() {{\n' +127' await new Promise(r => setTimeout(() => r(), 0));\n' +128' return "SOME_DELAYED_RESULT";\n' +129' }}',130true,131)132assert.equal(result.resultType, EvaluateResultType.SUCCESS)133assert.notEqual(result.realmId, null)134assert.equal(result.result.type, 'string')135assert.notEqual(result.result.value, null)136assert.equal(result.result.value, 'SOME_DELAYED_RESULT')137})138139it('can call function with await promise false', async function () {140const id = await driver.getWindowHandle()141manager = await ScriptManager(id, driver)142143const result = await manager.callFunctionInBrowsingContext(144id,145'async function() {{\n' +146' await new Promise(r => setTimeout(() => r(), 0));\n' +147' return "SOME_DELAYED_RESULT";\n' +148' }}',149false,150)151assert.equal(result.resultType, EvaluateResultType.SUCCESS)152assert.notEqual(result.realmId, null)153assert.equal(result.result.type, 'promise')154assert.equal(result.result.value, undefined)155})156157it('can call function with this parameter', async function () {158const id = await driver.getWindowHandle()159manager = await ScriptManager(id, driver)160161let mapValue = { some_property: LocalValue.createNumberValue(42) }162let thisParameter = LocalValue.createObjectValue(mapValue).asMap()163164const result = await manager.callFunctionInBrowsingContext(165id,166'function(){return this.some_property}',167false,168null,169thisParameter,170)171assert.equal(result.resultType, EvaluateResultType.SUCCESS)172assert.notEqual(result.realmId, null)173assert.equal(result.result.type, 'number')174assert.notEqual(result.result.value, null)175assert.equal(result.result.value, 42)176})177178it('can call function with ownership root', async function () {179const id = await driver.getWindowHandle()180manager = await ScriptManager(id, driver)181182const result = await manager.callFunctionInBrowsingContext(183id,184'async function(){return {a:1}}',185true,186null,187null,188ResultOwnership.ROOT,189)190assert.equal(result.resultType, EvaluateResultType.SUCCESS)191assert.notEqual(result.realmId, null)192assert.notEqual(result.result.handle, null)193assert.notEqual(result.result.value, null)194})195196it('can call function with ownership none', async function () {197const id = await driver.getWindowHandle()198manager = await ScriptManager(id, driver)199200const result = await manager.callFunctionInBrowsingContext(201id,202'async function(){return {a:1}}',203true,204null,205null,206ResultOwnership.NONE,207)208assert.equal(result.resultType, EvaluateResultType.SUCCESS)209assert.notEqual(result.realmId, null)210assert.equal(result.result.handle, undefined)211assert.notEqual(result.result.value, null)212})213214it('can call function that throws exception', async function () {215const id = await driver.getWindowHandle()216manager = await ScriptManager(id, driver)217218const result = await manager.callFunctionInBrowsingContext(id, '))) !!@@## some invalid JS script (((', false)219assert.equal(result.resultType, EvaluateResultType.EXCEPTION)220assert.notEqual(result.realmId, null)221222assert.equal(result.exceptionDetails.exception.type, 'error')223assert.equal(result.exceptionDetails.text.includes('SyntaxError:'), true)224assert.notEqual(result.exceptionDetails.columnNumber, null)225assert.equal(result.exceptionDetails.stackTrace.callFrames.length, 0)226})227228it('can call function in a sandbox', async function () {229const id = await driver.getWindowHandle()230manager = await ScriptManager(id, driver)231232// Make changes without sandbox233await manager.callFunctionInBrowsingContext(id, '() => { window.foo = 1; }', true)234235// Check changes are not present in the sandbox236const resultNotInSandbox = await manager.callFunctionInBrowsingContext(237id,238'() => window.foo',239true,240null,241null,242null,243'sandbox',244)245246assert.equal(resultNotInSandbox.resultType, EvaluateResultType.SUCCESS)247assert.equal(resultNotInSandbox.result.type, 'undefined')248249// Make changes in the sandbox250251await manager.callFunctionInBrowsingContext(id, '() => { window.foo = 2; }', true, null, null, null, 'sandbox')252253// Check if the changes are present in the sandbox254255const resultInSandbox = await manager.callFunctionInBrowsingContext(256id,257'() => window.foo',258true,259null,260null,261null,262'sandbox',263)264265assert.equal(resultInSandbox.resultType, EvaluateResultType.SUCCESS)266assert.notEqual(resultInSandbox.realmId, null)267268assert.equal(resultInSandbox.result.type, 'number')269assert.notEqual(resultInSandbox.result.value, null)270assert.equal(resultInSandbox.result.value, 2)271})272273it('can call function in a realm', async function () {274const firstTab = await driver.getWindowHandle()275await driver.switchTo().newWindow('tab')276manager = await ScriptManager(firstTab, driver)277278const realms = await manager.getAllRealms()279const firstTabRealmId = realms[0].realmId280const secondTabRealmId = realms[1].realmId281282await manager.callFunctionInRealm(firstTabRealmId, '() => { window.foo = 3; }', true)283284await manager.callFunctionInRealm(secondTabRealmId, '() => { window.foo = 5; }', true)285286const firstContextResult = await manager.callFunctionInRealm(firstTabRealmId, '() => window.foo', true)287288assert.equal(firstContextResult.resultType, EvaluateResultType.SUCCESS)289assert.equal(firstContextResult.result.type, 'number')290assert.notEqual(firstContextResult.result.value, null)291assert.equal(firstContextResult.result.value, 3)292293const secondContextResult = await manager.callFunctionInRealm(secondTabRealmId, '() => window.foo', true)294295assert.equal(secondContextResult.resultType, EvaluateResultType.SUCCESS)296assert.equal(secondContextResult.result.type, 'number')297assert.notEqual(secondContextResult.result.value, null)298assert.equal(secondContextResult.result.value, 5)299})300301it('can evaluate script', async function () {302const id = await driver.getWindowHandle()303manager = await ScriptManager(id, driver)304305const result = await manager.evaluateFunctionInBrowsingContext(id, '1 + 2', true)306307assert.equal(result.resultType, EvaluateResultType.SUCCESS)308assert.notEqual(result.realmId, null)309assert.equal(result.result.type, 'number')310assert.notEqual(result.result.value, null)311assert.equal(result.result.value, 3)312})313314it('can evaluate script that throws exception', async function () {315const id = await driver.getWindowHandle()316manager = await ScriptManager(id, driver)317318const result = await manager.evaluateFunctionInBrowsingContext(319id,320'))) !!@@## some invalid JS script (((',321false,322)323324assert.equal(result.resultType, EvaluateResultType.EXCEPTION)325assert.notEqual(result.realmId, null)326327assert.equal(result.exceptionDetails.exception.type, 'error')328assert.equal(result.exceptionDetails.text.includes('SyntaxError:'), true)329assert.notEqual(result.exceptionDetails.columnNumber, null)330assert.equal(result.exceptionDetails.stackTrace.callFrames.length, 0)331})332333it('can evaluate script with result ownership', async function () {334const id = await driver.getWindowHandle()335manager = await ScriptManager(id, driver)336337const result = await manager.evaluateFunctionInBrowsingContext(338id,339'Promise.resolve({a:1})',340true,341ResultOwnership.ROOT,342)343344assert.equal(result.resultType, EvaluateResultType.SUCCESS)345assert.notEqual(result.realmId, null)346assert.equal(result.result.type, 'object')347assert.notEqual(result.result.value, null)348assert.notEqual(result.result.handle, null)349})350351it('can evaluate in a sandbox', async function () {352const id = await driver.getWindowHandle()353manager = await ScriptManager(id, driver)354355// Make changes without sandbox356await manager.evaluateFunctionInBrowsingContext(id, 'window.foo = 1', true)357358// Check changes are not present in the sandbox359const resultNotInSandbox = await manager.evaluateFunctionInBrowsingContext(360id,361'window.foo',362true,363null,364'sandbox',365)366367assert.equal(resultNotInSandbox.resultType, EvaluateResultType.SUCCESS)368assert.equal(resultNotInSandbox.result.type, 'undefined')369370// Make changes in the sandbox371await manager.evaluateFunctionInBrowsingContext(id, 'window.foo = 2', true, null, 'sandbox')372373const resultInSandbox = await manager.evaluateFunctionInBrowsingContext(id, 'window.foo', true, null, 'sandbox')374375assert.equal(resultInSandbox.resultType, EvaluateResultType.SUCCESS)376assert.notEqual(resultInSandbox.realmId, null)377378assert.equal(resultInSandbox.result.type, 'number')379assert.notEqual(resultInSandbox.result.value, null)380assert.equal(resultInSandbox.result.value, 2)381})382383it('can evaluate in a realm', async function () {384const firstTab = await driver.getWindowHandle()385await driver.switchTo().newWindow('tab')386manager = await ScriptManager(firstTab, driver)387388const realms = await manager.getAllRealms()389const firstTabRealmId = realms[0].realmId390const secondTabRealmId = realms[1].realmId391392await manager.evaluateFunctionInRealm(firstTabRealmId, 'window.foo = 3', true)393394await manager.evaluateFunctionInRealm(secondTabRealmId, 'window.foo = 5', true)395396const firstContextResult = await manager.evaluateFunctionInRealm(firstTabRealmId, 'window.foo', true)397398assert.equal(firstContextResult.resultType, EvaluateResultType.SUCCESS)399assert.equal(firstContextResult.result.type, 'number')400assert.notEqual(firstContextResult.result.value, null)401assert.equal(firstContextResult.result.value, 3)402403const secondContextResult = await manager.evaluateFunctionInRealm(secondTabRealmId, 'window.foo', true)404405assert.equal(secondContextResult.resultType, EvaluateResultType.SUCCESS)406assert.equal(secondContextResult.result.type, 'number')407assert.notEqual(secondContextResult.result.value, null)408assert.equal(secondContextResult.result.value, 5)409})410411it('can disown handles', async function () {412const id = await driver.getWindowHandle()413manager = await ScriptManager(id, driver)414415const evaluateResult = await manager.evaluateFunctionInBrowsingContext(416id,417'({a:1})',418false,419ResultOwnership.ROOT,420)421422assert.equal(evaluateResult.resultType, EvaluateResultType.SUCCESS)423assert.notEqual(evaluateResult.realmId, null)424assert.notEqual(evaluateResult.result.handle, null)425426let argumentValues = []427let valueMap = evaluateResult.result.value428429let value1 = LocalValue.createObjectValue(valueMap)430let value2 = new ReferenceValue(RemoteReferenceType.HANDLE, evaluateResult.result.handle)431argumentValues.push(value1)432argumentValues.push(value2)433434await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues)435436assert.notEqual(evaluateResult.result.value, null)437438let handles = [evaluateResult.result.handle]439await manager.disownBrowsingContextScript(id, handles)440441await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues).catch((error) => {442assert(error instanceof TypeError)443})444})445446it('can disown handles in realm', async function () {447const id = await driver.getWindowHandle()448manager = await ScriptManager(id, driver)449450const evaluateResult = await manager.evaluateFunctionInBrowsingContext(451id,452'({a:1})',453false,454ResultOwnership.ROOT,455)456457assert.equal(evaluateResult.resultType, EvaluateResultType.SUCCESS)458assert.notEqual(evaluateResult.realmId, null)459assert.notEqual(evaluateResult.result.handle, null)460461let argumentValues = []462let valueMap = evaluateResult.result.value463464let value1 = LocalValue.createObjectValue(valueMap)465let value2 = new ReferenceValue(RemoteReferenceType.HANDLE, evaluateResult.result.handle)466argumentValues.push(value1)467argumentValues.push(value2)468469await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues)470471assert.notEqual(evaluateResult.result.value, null)472473let handles = [evaluateResult.result.handle]474await manager.disownRealmScript(evaluateResult.realmId, handles)475476await manager.callFunctionInBrowsingContext(id, 'arg => arg.a', false, argumentValues).catch((error) => {477assert(error instanceof TypeError)478})479})480481it('can get all realms', async function () {482const firstWindow = await driver.getWindowHandle()483await driver.switchTo().newWindow('window')484const secondWindow = await driver.getWindowHandle()485manager = await ScriptManager(firstWindow, driver)486487const realms = await manager.getAllRealms()488assert.equal(realms.length, 2)489490const firstWindowRealm = realms[0]491assert.equal(firstWindowRealm.realmType, RealmType.WINDOW)492assert.notEqual(firstWindowRealm.realmId, null)493assert.equal(firstWindowRealm.browsingContext, firstWindow)494495const secondWindowRealm = realms[1]496assert.equal(secondWindowRealm.realmType, RealmType.WINDOW)497assert.notEqual(secondWindowRealm.realmId, null)498assert.equal(secondWindowRealm.browsingContext, secondWindow)499})500501it('can get realm by type', async function () {502const firstWindow = await driver.getWindowHandle()503await driver.switchTo().newWindow('window')504const secondWindow = await driver.getWindowHandle()505manager = await ScriptManager(firstWindow, driver)506507const realms = await manager.getRealmsByType(RealmType.WINDOW)508assert.equal(realms.length, 2)509510const firstWindowRealm = realms[0]511assert.equal(firstWindowRealm.realmType, RealmType.WINDOW)512assert.notEqual(firstWindowRealm.realmId, null)513assert.equal(firstWindowRealm.browsingContext, firstWindow)514515const secondWindowRealm = realms[1]516assert.equal(secondWindowRealm.realmType, RealmType.WINDOW)517assert.notEqual(secondWindowRealm.realmId, null)518assert.equal(secondWindowRealm.browsingContext, secondWindow)519})520521it('can get realm in browsing context', async function () {522const windowId = await driver.getWindowHandle()523await driver.switchTo().newWindow('tab')524const tabId = await driver.getWindowHandle()525manager = await ScriptManager(windowId, driver)526527const realms = await manager.getRealmsInBrowsingContext(tabId)528529const tabRealm = realms[0]530assert.equal(tabRealm.realmType, RealmType.WINDOW)531assert.notEqual(tabRealm.realmId, null)532assert.equal(tabRealm.browsingContext, tabId)533})534535it('can get realm in browsing context by type', async function () {536const windowId = await driver.getWindowHandle()537await driver.switchTo().newWindow('tab')538manager = await ScriptManager(windowId, driver)539540const realms = await manager.getRealmsInBrowsingContextByType(windowId, RealmType.WINDOW)541542const windowRealm = realms[0]543assert.equal(windowRealm.realmType, RealmType.WINDOW)544assert.notEqual(windowRealm.realmId, null)545assert.equal(windowRealm.browsingContext, windowId)546})547548it('can add preload script test', async function () {549const id = await driver.getWindowHandle()550manager = await ScriptManager([], driver)551552await manager.addPreloadScript("() => { window.foo='bar'; }")553554// Check that preload script didn't apply the changes to the current context555let result = await manager.evaluateFunctionInBrowsingContext(id, 'window.foo', true)556assert.equal(result.result.type, 'undefined')557558await driver.switchTo().newWindow('window')559const new_window_id = await driver.getWindowHandle()560561// Check that preload script applied the changes to the window562result = await manager.evaluateFunctionInBrowsingContext(new_window_id, 'window.foo', true)563564assert.equal(result.resultType, EvaluateResultType.SUCCESS)565assert.notEqual(result.realmId, null)566assert.equal(result.result.type, 'string')567assert.notEqual(result.result.value, null)568assert.equal(result.result.value, 'bar')569570const browsingContext = await BrowsingContext(driver, {571type: 'tab',572})573574await browsingContext.navigate(Pages.logEntryAdded, 'complete')575576// Check that preload script was applied after navigation577result = await manager.evaluateFunctionInBrowsingContext(new_window_id, 'window.foo', true)578579assert.equal(result.result.type, 'string')580assert.equal(result.result.value, 'bar')581})582583it('can add same preload script twice', async function () {584const id = await driver.getWindowHandle()585manager = await ScriptManager(id, driver)586587const script_1 = await manager.addPreloadScript('() => { return 42; }')588const script_2 = await manager.addPreloadScript('() => { return 42; }')589590assert.notEqual(script_1, script_2)591})592593it('can access preload script properties', async function () {594manager = await ScriptManager([], driver)595596await manager.addPreloadScript('() => { window.preloadScriptFunction = () => window.baz = 42; }')597598await driver.switchTo().newWindow('tab')599const new_tab_id = await driver.getWindowHandle()600await driver.get(Pages.scriptTestAccessProperty)601602const result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.baz', true)603604assert.equal(result.result.type, 'number')605assert.equal(result.result.value, 42)606})607608it('can add preload script to sandbox', async function () {609manager = await ScriptManager([], driver)610611await manager.addPreloadScript('() => { window.foo = 1; }')612await manager.addPreloadScript('() => { window.bar = 2; }', [], 'sandbox')613614await driver.switchTo().newWindow('tab')615const new_tab_id = await driver.getWindowHandle()616617let result_in_sandbox = await manager.evaluateFunctionInBrowsingContext(618new_tab_id,619'window.foo',620true,621null,622'sandbox',623)624625assert.equal(result_in_sandbox.result.type, 'undefined')626627let result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.bar', true)628629assert.equal(result.result.type, 'undefined')630631result_in_sandbox = await manager.evaluateFunctionInBrowsingContext(632new_tab_id,633'window.bar',634true,635null,636'sandbox',637)638639assert.equal(result_in_sandbox.result.type, 'number')640assert.equal(result_in_sandbox.result.value, 2)641})642643it('can remove properties set by preload script', async function () {644manager = await ScriptManager([], driver)645646await manager.addPreloadScript('() => { window.foo = 42; }')647await manager.addPreloadScript('() => { window.foo = 50; }', [], 'sandbox_1')648649await driver.switchTo().newWindow('tab')650const new_tab_id = await driver.getWindowHandle()651await driver.get(Pages.scriptTestRemoveProperty)652653let result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.foo', true)654assert.equal(result.result.type, 'undefined')655656result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.foo', true, null, 'sandbox_1')657assert.equal(result.result.type, 'number')658assert.equal(result.result.value, 50)659})660661it('can remove preload script', async function () {662manager = await ScriptManager([], driver)663664let script = await manager.addPreloadScript("() => { window.foo='bar'; }")665666await driver.switchTo().newWindow('tab')667const tab_1_id = await driver.getWindowHandle()668669let result = await manager.evaluateFunctionInBrowsingContext(tab_1_id, 'window.foo', true)670671assert.equal(result.result.type, 'string')672assert.equal(result.result.value, 'bar')673674await manager.removePreloadScript(script)675676await driver.switchTo().newWindow('tab')677const tab_2_id = await driver.getWindowHandle()678679// Check that changes from preload script were not applied after script was removed680result = await manager.evaluateFunctionInBrowsingContext(tab_2_id, 'window.foo', true)681682assert.equal(result.result.type, 'undefined')683})684685it('cannot remove same preload script twice', async function () {686manager = await ScriptManager([], driver)687688let script = await manager.addPreloadScript("() => { window.foo='bar'; }")689690await manager.removePreloadScript(script)691692await manager.removePreloadScript(script).catch((error) => {693assert(error instanceof WebDriverError)694})695})696697it('can remove one of preload script', async function () {698manager = await ScriptManager([], driver)699700let script_1 = await manager.addPreloadScript("() => { window.bar='foo'; }")701702let script_2 = await manager.addPreloadScript("() => { window.baz='bar'; }")703704await manager.removePreloadScript(script_1)705706await driver.switchTo().newWindow('tab')707const new_tab_id = await driver.getWindowHandle()708709// Check that the first script didn't run710let result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.bar', true)711712assert.equal(result.result.type, 'undefined')713714// Check that the second script still applied the changes to the window715result = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.baz', true)716717assert.equal(result.result.type, 'string')718assert.equal(result.result.value, 'bar')719720// Clean up the second script721await manager.removePreloadScript(script_2)722})723724it('can remove one of preload script from sandbox', async function () {725const id = await driver.getWindowHandle()726manager = await ScriptManager(id, driver)727728let script_1 = await manager.addPreloadScript('() => { window.foo = 1; }')729730let script_2 = await manager.addPreloadScript('() => { window.bar = 2; }', [], 'sandbox')731732// Remove first preload script733await manager.removePreloadScript(script_1)734735// Remove second preload script736await manager.removePreloadScript(script_2)737738await driver.switchTo().newWindow('tab')739const new_tab_id = await driver.getWindowHandle()740741// Make sure that changes from first preload script were not applied742let result_in_window = await manager.evaluateFunctionInBrowsingContext(new_tab_id, 'window.foo', true)743744assert.equal(result_in_window.result.type, 'undefined')745746// Make sure that changes from second preload script were not applied747let result_in_sandbox = await manager.evaluateFunctionInBrowsingContext(748new_tab_id,749'window.bar',750true,751null,752'sandbox',753)754755assert.equal(result_in_sandbox.result.type, 'undefined')756})757758it('can listen to channel message', async function () {759manager = await ScriptManager(undefined, driver)760761let message = null762763await manager.onMessage((m) => {764message = m765})766767let argumentValues = []768let value = LocalValue.createChannelValue(new ChannelValue('channel_name'))769argumentValues.push(value)770771const result = await manager.callFunctionInBrowsingContext(772await driver.getWindowHandle(),773'(channel) => channel("foo")',774false,775argumentValues,776)777assert.equal(result.resultType, EvaluateResultType.SUCCESS)778assert.notEqual(message, null)779assert.equal(message.channel, 'channel_name')780assert.equal(message.data.type, 'string')781assert.equal(message.data.value, 'foo')782})783784it('can listen to realm created message', async function () {785manager = await ScriptManager(undefined, driver)786787let realmInfo = null788789await manager.onRealmCreated((result) => {790realmInfo = result791})792793const id = await driver.getWindowHandle()794const browsingContext = await BrowsingContext(driver, {795browsingContextId: id,796})797798await browsingContext.navigate(Pages.blankPage, 'complete')799800assert.notEqual(realmInfo, null)801assert.notEqual(realmInfo.realmId, null)802assert.equal(realmInfo.realmType, RealmType.WINDOW)803})804805xit('can listen to realm destroyed message', async function () {806manager = await ScriptManager(undefined, driver)807808let realmInfo = null809810await manager.onRealmDestroyed((result) => {811realmInfo = result812})813814const id = await driver.getWindowHandle()815const browsingContext = await BrowsingContext(driver, {816browsingContextId: id,817})818819await browsingContext.close()820821assert.notEqual(realmInfo, null)822assert.notEqual(realmInfo.realmId, null)823assert.equal(realmInfo.realmType, RealmType.WINDOW)824})825})826},827{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },828)829830831