Path: blob/trunk/javascript/selenium-webdriver/test/lib/webdriver_script_execute_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 { suite } = require('../../lib/test')2223const ScriptManager = require('selenium-webdriver/bidi/scriptManager')24const { LocalValue, RegExpValue } = require('selenium-webdriver/bidi/protocolValue')25const { SpecialNumberType } = require('selenium-webdriver/bidi/protocolType')2627suite(28function (env) {29let driver3031beforeEach(async function () {32driver = await env.builder().build()33})3435afterEach(async function () {36await driver.quit()37})3839describe('Execute script', function () {40it('can execute script with undefined argument', async function () {41const result = await driver42.script()43.execute(44'(arg) => {{\n' +45' if(arg!==undefined)\n' +46' throw Error("Argument should be undefined, but was "+arg);\n' +47' return arg;\n' +48' }}',49undefined,50)5152assert.equal(result.type, 'undefined')53})5455it('can execute script with null argument', async function () {56const result = await driver57.script()58.execute(59'(arg) => {{\n' +60' if(arg!==null)\n' +61' throw Error("Argument should be null, but was "+arg);\n' +62' return arg;\n' +63' }}',64null,65)6667assert.equal(result.type, 'null')68})69it('can execute script with minus zero argument', async function () {70const result = await driver71.script()72.execute(73'(arg) => {{\n' +74' if(arg!==-0)\n' +75' throw Error("Argument should be -0, but was " + arg);\n' +76' return arg;\n' +77' }}',78SpecialNumberType.MINUS_ZERO,79)8081assert.equal(result.type, 'number')82assert.equal(result.value, '-0')83})8485it('can execute script with infinity argument', async function () {86const result = await driver87.script()88.execute(89'(arg) => {{\n' +90' if(arg!==Infinity)\n' +91' throw Error("Argument should be Infinity, but was "+arg);\n' +92' return arg;\n' +93' }}',94SpecialNumberType.INFINITY,95)9697assert.equal(result.type, 'number')98assert.equal(result.value, 'Infinity')99})100101it('can execute script with minus infinity argument', async function () {102const result = await driver103.script()104.execute(105'(arg) => {{\n' +106' if(arg!==-Infinity)\n' +107' throw Error("Argument should be -Infinity, but was "+arg);\n' +108' return arg;\n' +109' }}',110SpecialNumberType.MINUS_INFINITY,111)112113assert.equal(result.type, 'number')114assert.equal(result.value, '-Infinity')115})116117it('can execute script with number argument', async function () {118const result = await driver119.script()120.execute(121'(arg) => {{\n' +122' if(arg!==1.4)\n' +123' throw Error("Argument should be 1.4, but was "+arg);\n' +124' return arg;\n' +125' }}',1261.4,127)128129assert.equal(result.type, 'number')130assert.equal(result.value, 1.4)131})132133it('can execute script with boolean argument', async function () {134const result = await driver135.script()136.execute(137'(arg) => {{\n' +138' if(arg!==true)\n' +139' throw Error("Argument should be true, but was "+arg);\n' +140' return arg;\n' +141' }}',142true,143)144145assert.equal(result.type, 'boolean')146assert.equal(result.value, true)147})148149it('can execute script with big int argument', async function () {150const result = await driver151.script()152.execute(153'(arg) => {{\n' +154' if(arg!==42n)\n' +155' throw Error("Argument should be 42n, but was "+arg);\n' +156' return arg;\n' +157' }}',15842n,159)160161assert.equal(result.type, 'bigint')162assert.equal(result.value, '42')163})164165it('can execute script with array argument', async function () {166let arrayValue = ['foobar']167168const result = await driver169.script()170.execute(171'(arg) => {{\n' +172' if(! (arg instanceof Array))\n' +173' throw Error("Argument type should be Array, but was "+\n' +174' Object.prototype.toString.call(arg));\n' +175' return arg;\n' +176' }}',177arrayValue,178)179180assert.equal(result.type, 'array')181182let resultValue = result.value183assert.equal(resultValue.length, 1)184assert.equal(resultValue[0].type, 'string')185assert.equal(resultValue[0].value, 'foobar')186})187188it('can execute script with set argument', async function () {189let setValues = new Set()190setValues.add('foobar')191setValues.add('test')192193const result = await driver194.script()195.execute(196'(arg) => {{\n' +197' if(! (arg instanceof Set))\n' +198' throw Error("Argument type should be Set, but was "+\n' +199' Object.prototype.toString.call(arg));\n' +200' return arg;\n' +201' }}',202setValues,203)204205assert.equal(result.type, 'set')206207let resultValue = result.value208assert.equal(resultValue.length, 2)209assert.equal(resultValue[0].type, 'string')210assert.equal(resultValue[0].value, 'foobar')211assert.equal(resultValue[1].type, 'string')212assert.equal(resultValue[1].value, 'test')213})214215it('can execute script with date argument', async function () {216const result = await driver217.script()218.execute(219'(arg) => {{\n' +220' if(! (arg instanceof Date))\n' +221' throw Error("Argument type should be Date, but was "+\n' +222' Object.prototype.toString.call(arg));\n' +223' return arg;\n' +224' }}',225new Date('2022-05-31T13:47:29.000Z'),226)227228assert.equal(result.type, 'date')229assert.equal(result.value, '2022-05-31T13:47:29.000Z')230})231232it('can execute script with map argument', async function () {233let mapValue = new Map()234mapValue.set(1, 2)235mapValue.set('foo', 'bar')236mapValue.set(true, false)237mapValue.set('baz', [1, 2, 3])238239const result = await driver240.script()241.execute(242'(arg) => {{\n' +243' if(! (arg instanceof Map))\n' +244' throw Error("Argument type should be Map, but was "+\n' +245' Object.prototype.toString.call(arg));\n' +246' return arg;\n' +247' }}',248mapValue,249)250assert.equal(result.type, 'map')251assert.notEqual(result.value, null)252253let resultValue = result.value254255assert.equal(resultValue.length, 4)256257assert.equal(258JSON.stringify(resultValue),259'[[{"type":"number","value":1},{"type":"number","value":2}],["foo",{"type":"string","value":"bar"}],[{"type":"boolean","value":true},{"type":"boolean","value":false}],["baz",{"type":"array","value":[{"type":"number","value":1},{"type":"number","value":2},{"type":"number","value":3}]}]]',260)261})262263it('can execute script with object argument', async function () {264const result = await driver265.script()266.execute(267'(arg) => {{\n' +268' if(! (arg instanceof Object))\n' +269' throw Error("Argument type should be Object, but was "+\n' +270' Object.prototype.toString.call(arg));\n' +271' return arg;\n' +272' }}',273{ foobar: 'foobar' },274)275276assert.equal(result.type, 'object')277278let resultValue = result.value279assert.equal(resultValue['foobar'].type, 'string')280assert.equal(resultValue['foobar'].value, 'foobar')281})282283it('can execute script with regex argument', async function () {284const id = await driver.getWindowHandle()285const manager = await ScriptManager(id, driver)286let argumentValues = []287let value = LocalValue.createRegularExpressionValue(new RegExpValue('foo', 'g'))288argumentValues.push(value)289290const result = await driver291.script()292.execute(293'(arg) => {{\n' +294' if(! (arg instanceof RegExp))\n' +295' throw Error("Argument type should be RegExp, but was "+\n' +296' Object.prototype.toString.call(arg));\n' +297' return arg;\n' +298' }}',299new RegExp('foo', 'g'),300)301302let resultValue = result.value303304assert.equal(resultValue.pattern, 'foo')305assert.equal(resultValue.flags, 'g')306})307})308},309{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },310)311312313