Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/lib/webdriver_script_execute_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 { suite } = require('../../lib/test')
23
24
const ScriptManager = require('selenium-webdriver/bidi/scriptManager')
25
const { LocalValue, RegExpValue } = require('selenium-webdriver/bidi/protocolValue')
26
const { SpecialNumberType } = require('selenium-webdriver/bidi/protocolType')
27
28
suite(
29
function (env) {
30
let driver
31
32
beforeEach(async function () {
33
driver = await env.builder().build()
34
})
35
36
afterEach(async function () {
37
await driver.quit()
38
})
39
40
describe('Execute script', function () {
41
it('can execute script with undefined argument', async function () {
42
const result = await driver
43
.script()
44
.execute(
45
'(arg) => {{\n' +
46
' if(arg!==undefined)\n' +
47
' throw Error("Argument should be undefined, but was "+arg);\n' +
48
' return arg;\n' +
49
' }}',
50
undefined,
51
)
52
53
assert.equal(result.type, 'undefined')
54
})
55
56
it('can execute script with null argument', async function () {
57
const result = await driver
58
.script()
59
.execute(
60
'(arg) => {{\n' +
61
' if(arg!==null)\n' +
62
' throw Error("Argument should be null, but was "+arg);\n' +
63
' return arg;\n' +
64
' }}',
65
null,
66
)
67
68
assert.equal(result.type, 'null')
69
})
70
it('can execute script with minus zero argument', async function () {
71
const result = await driver
72
.script()
73
.execute(
74
'(arg) => {{\n' +
75
' if(arg!==-0)\n' +
76
' throw Error("Argument should be -0, but was " + arg);\n' +
77
' return arg;\n' +
78
' }}',
79
SpecialNumberType.MINUS_ZERO,
80
)
81
82
assert.equal(result.type, 'number')
83
assert.equal(result.value, '-0')
84
})
85
86
it('can execute script with infinity argument', async function () {
87
const result = await driver
88
.script()
89
.execute(
90
'(arg) => {{\n' +
91
' if(arg!==Infinity)\n' +
92
' throw Error("Argument should be Infinity, but was "+arg);\n' +
93
' return arg;\n' +
94
' }}',
95
SpecialNumberType.INFINITY,
96
)
97
98
assert.equal(result.type, 'number')
99
assert.equal(result.value, 'Infinity')
100
})
101
102
it('can execute script with minus infinity argument', async function () {
103
const result = await driver
104
.script()
105
.execute(
106
'(arg) => {{\n' +
107
' if(arg!==-Infinity)\n' +
108
' throw Error("Argument should be -Infinity, but was "+arg);\n' +
109
' return arg;\n' +
110
' }}',
111
SpecialNumberType.MINUS_INFINITY,
112
)
113
114
assert.equal(result.type, 'number')
115
assert.equal(result.value, '-Infinity')
116
})
117
118
it('can execute script with number argument', async function () {
119
const result = await driver
120
.script()
121
.execute(
122
'(arg) => {{\n' +
123
' if(arg!==1.4)\n' +
124
' throw Error("Argument should be 1.4, but was "+arg);\n' +
125
' return arg;\n' +
126
' }}',
127
1.4,
128
)
129
130
assert.equal(result.type, 'number')
131
assert.equal(result.value, 1.4)
132
})
133
134
it('can execute script with boolean argument', async function () {
135
const result = await driver
136
.script()
137
.execute(
138
'(arg) => {{\n' +
139
' if(arg!==true)\n' +
140
' throw Error("Argument should be true, but was "+arg);\n' +
141
' return arg;\n' +
142
' }}',
143
true,
144
)
145
146
assert.equal(result.type, 'boolean')
147
assert.equal(result.value, true)
148
})
149
150
it('can execute script with big int argument', async function () {
151
const result = await driver
152
.script()
153
.execute(
154
'(arg) => {{\n' +
155
' if(arg!==42n)\n' +
156
' throw Error("Argument should be 42n, but was "+arg);\n' +
157
' return arg;\n' +
158
' }}',
159
42n,
160
)
161
162
assert.equal(result.type, 'bigint')
163
assert.equal(result.value, '42')
164
})
165
166
it('can execute script with array argument', async function () {
167
let arrayValue = ['foobar']
168
169
const result = await driver
170
.script()
171
.execute(
172
'(arg) => {{\n' +
173
' if(! (arg instanceof Array))\n' +
174
' throw Error("Argument type should be Array, but was "+\n' +
175
' Object.prototype.toString.call(arg));\n' +
176
' return arg;\n' +
177
' }}',
178
arrayValue,
179
)
180
181
assert.equal(result.type, 'array')
182
183
let resultValue = result.value
184
assert.equal(resultValue.length, 1)
185
assert.equal(resultValue[0].type, 'string')
186
assert.equal(resultValue[0].value, 'foobar')
187
})
188
189
it('can execute script with set argument', async function () {
190
let setValues = new Set()
191
setValues.add('foobar')
192
setValues.add('test')
193
194
const result = await driver
195
.script()
196
.execute(
197
'(arg) => {{\n' +
198
' if(! (arg instanceof Set))\n' +
199
' throw Error("Argument type should be Set, but was "+\n' +
200
' Object.prototype.toString.call(arg));\n' +
201
' return arg;\n' +
202
' }}',
203
setValues,
204
)
205
206
assert.equal(result.type, 'set')
207
208
let resultValue = result.value
209
assert.equal(resultValue.length, 2)
210
assert.equal(resultValue[0].type, 'string')
211
assert.equal(resultValue[0].value, 'foobar')
212
assert.equal(resultValue[1].type, 'string')
213
assert.equal(resultValue[1].value, 'test')
214
})
215
216
it('can execute script with date argument', async function () {
217
const result = await driver
218
.script()
219
.execute(
220
'(arg) => {{\n' +
221
' if(! (arg instanceof Date))\n' +
222
' throw Error("Argument type should be Date, but was "+\n' +
223
' Object.prototype.toString.call(arg));\n' +
224
' return arg;\n' +
225
' }}',
226
new Date('2022-05-31T13:47:29.000Z'),
227
)
228
229
assert.equal(result.type, 'date')
230
assert.equal(result.value, '2022-05-31T13:47:29.000Z')
231
})
232
233
it('can execute script with map argument', async function () {
234
let mapValue = new Map()
235
mapValue.set(1, 2)
236
mapValue.set('foo', 'bar')
237
mapValue.set(true, false)
238
mapValue.set('baz', [1, 2, 3])
239
240
const result = await driver
241
.script()
242
.execute(
243
'(arg) => {{\n' +
244
' if(! (arg instanceof Map))\n' +
245
' throw Error("Argument type should be Map, but was "+\n' +
246
' Object.prototype.toString.call(arg));\n' +
247
' return arg;\n' +
248
' }}',
249
mapValue,
250
)
251
assert.equal(result.type, 'map')
252
assert.notEqual(result.value, null)
253
254
let resultValue = result.value
255
256
assert.equal(resultValue.length, 4)
257
258
assert.equal(
259
JSON.stringify(resultValue),
260
'[[{"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}]}]]',
261
)
262
})
263
264
it('can execute script with object argument', async function () {
265
const result = await driver
266
.script()
267
.execute(
268
'(arg) => {{\n' +
269
' if(! (arg instanceof Object))\n' +
270
' throw Error("Argument type should be Object, but was "+\n' +
271
' Object.prototype.toString.call(arg));\n' +
272
' return arg;\n' +
273
' }}',
274
{ foobar: 'foobar' },
275
)
276
277
assert.equal(result.type, 'object')
278
279
let resultValue = result.value
280
assert.equal(resultValue['foobar'].type, 'string')
281
assert.equal(resultValue['foobar'].value, 'foobar')
282
})
283
284
it('can execute script with regex argument', async function () {
285
const id = await driver.getWindowHandle()
286
const manager = await ScriptManager(id, driver)
287
let argumentValues = []
288
let value = LocalValue.createRegularExpressionValue(new RegExpValue('foo', 'g'))
289
argumentValues.push(value)
290
291
const result = await driver
292
.script()
293
.execute(
294
'(arg) => {{\n' +
295
' if(! (arg instanceof RegExp))\n' +
296
' throw Error("Argument type should be RegExp, but was "+\n' +
297
' Object.prototype.toString.call(arg));\n' +
298
' return arg;\n' +
299
' }}',
300
new RegExp('foo', 'g'),
301
)
302
303
let resultValue = result.value
304
305
assert.equal(resultValue.pattern, 'foo')
306
assert.equal(resultValue.flags, 'g')
307
})
308
})
309
},
310
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
311
)
312
313