Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/bidi/local_value_test.js
2887 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 { EvaluateResultType } = require('selenium-webdriver/bidi/evaluateResult')
27
const { SpecialNumberType } = require('selenium-webdriver/bidi/protocolType')
28
29
suite(
30
function (env) {
31
let driver
32
33
beforeEach(async function () {
34
driver = await env.builder().build()
35
})
36
37
afterEach(async function () {
38
await driver.quit()
39
})
40
41
describe('Local Value', function () {
42
it('can call function with undefined argument', async function () {
43
const id = await driver.getWindowHandle()
44
const manager = await ScriptManager(id, driver)
45
let argumentValues = []
46
let value = LocalValue.createUndefinedValue()
47
argumentValues.push(value)
48
49
const result = await manager.callFunctionInBrowsingContext(
50
id,
51
'(arg) => {{\n' +
52
' if(arg!==undefined)\n' +
53
' throw Error("Argument should be undefined, but was "+arg);\n' +
54
' return arg;\n' +
55
' }}',
56
false,
57
argumentValues,
58
)
59
60
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
61
assert.notEqual(result.realmId, null)
62
assert.equal(result.result.type, 'undefined')
63
})
64
65
it('can call function with null argument', async function () {
66
const id = await driver.getWindowHandle()
67
const manager = await ScriptManager(id, driver)
68
let argumentValues = []
69
let value = LocalValue.createNullValue()
70
argumentValues.push(value)
71
72
const result = await manager.callFunctionInBrowsingContext(
73
id,
74
'(arg) => {{\n' +
75
' if(arg!==null)\n' +
76
' throw Error("Argument should be null, but was "+arg);\n' +
77
' return arg;\n' +
78
' }}',
79
false,
80
argumentValues,
81
)
82
83
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
84
assert.notEqual(result.realmId, null)
85
assert.equal(result.result.type, 'null')
86
})
87
88
it('can call function with minus zero argument', async function () {
89
const id = await driver.getWindowHandle()
90
const manager = await ScriptManager(id, driver)
91
let argumentValues = []
92
let value = LocalValue.createSpecialNumberValue(SpecialNumberType.MINUS_ZERO)
93
argumentValues.push(value)
94
95
const result = await manager.callFunctionInBrowsingContext(
96
id,
97
'(arg) => {{\n' +
98
' if(arg!==-0)\n' +
99
' throw Error("Argument should be -0, but was "+arg);\n' +
100
' return arg;\n' +
101
' }}',
102
false,
103
argumentValues,
104
)
105
106
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
107
assert.notEqual(result.realmId, null)
108
assert.equal(result.result.type, 'number')
109
assert.notEqual(result.result.value, null)
110
assert.equal(result.result.value, '-0')
111
})
112
113
it('can call function with infinity argument', async function () {
114
const id = await driver.getWindowHandle()
115
const manager = await ScriptManager(id, driver)
116
let argumentValues = []
117
let value = LocalValue.createSpecialNumberValue(SpecialNumberType.INFINITY)
118
argumentValues.push(value)
119
120
const result = await manager.callFunctionInBrowsingContext(
121
id,
122
'(arg) => {{\n' +
123
' if(arg!==Infinity)\n' +
124
' throw Error("Argument should be Infinity, but was "+arg);\n' +
125
' return arg;\n' +
126
' }}',
127
false,
128
argumentValues,
129
)
130
131
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
132
assert.notEqual(result.realmId, null)
133
assert.equal(result.result.type, 'number')
134
assert.notEqual(result.result.value, null)
135
assert.equal(result.result.value, 'Infinity')
136
})
137
138
it('can call function with minus infinity argument', async function () {
139
const id = await driver.getWindowHandle()
140
const manager = await ScriptManager(id, driver)
141
let argumentValues = []
142
let value = LocalValue.createSpecialNumberValue(SpecialNumberType.MINUS_INFINITY)
143
argumentValues.push(value)
144
145
const result = await manager.callFunctionInBrowsingContext(
146
id,
147
'(arg) => {{\n' +
148
' if(arg!==-Infinity)\n' +
149
' throw Error("Argument should be -Infinity, but was "+arg);\n' +
150
' return arg;\n' +
151
' }}',
152
false,
153
argumentValues,
154
)
155
156
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
157
assert.notEqual(result.realmId, null)
158
assert.equal(result.result.type, 'number')
159
assert.notEqual(result.result.value, null)
160
assert.equal(result.result.value, '-Infinity')
161
})
162
163
it('can call function with number argument', async function () {
164
const id = await driver.getWindowHandle()
165
const manager = await ScriptManager(id, driver)
166
let argumentValues = []
167
let value = LocalValue.createNumberValue(1.4)
168
argumentValues.push(value)
169
170
const result = await manager.callFunctionInBrowsingContext(
171
id,
172
'(arg) => {{\n' +
173
' if(arg!==1.4)\n' +
174
' throw Error("Argument should be 1.4, but was "+arg);\n' +
175
' return arg;\n' +
176
' }}',
177
false,
178
argumentValues,
179
)
180
181
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
182
assert.notEqual(result.realmId, null)
183
assert.equal(result.result.type, 'number')
184
assert.notEqual(result.result.value, null)
185
assert.equal(result.result.value, 1.4)
186
})
187
188
it('can call function with boolean argument', async function () {
189
const id = await driver.getWindowHandle()
190
const manager = await ScriptManager(id, driver)
191
let argumentValues = []
192
let value = LocalValue.createBooleanValue(true)
193
argumentValues.push(value)
194
195
const result = await manager.callFunctionInBrowsingContext(
196
id,
197
'(arg) => {{\n' +
198
' if(arg!==true)\n' +
199
' throw Error("Argument should be true, but was "+arg);\n' +
200
' return arg;\n' +
201
' }}',
202
false,
203
argumentValues,
204
)
205
206
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
207
assert.notEqual(result.realmId, null)
208
assert.equal(result.result.type, 'boolean')
209
assert.notEqual(result.result.value, null)
210
assert.equal(result.result.value, true)
211
})
212
213
it('can call function with big int argument', async function () {
214
const id = await driver.getWindowHandle()
215
const manager = await ScriptManager(id, driver)
216
let argumentValues = []
217
let value = LocalValue.createBigIntValue('42')
218
argumentValues.push(value)
219
220
const result = await manager.callFunctionInBrowsingContext(
221
id,
222
'(arg) => {{\n' +
223
' if(arg!==42n)\n' +
224
' throw Error("Argument should be 42n, but was "+arg);\n' +
225
' return arg;\n' +
226
' }}',
227
false,
228
argumentValues,
229
)
230
231
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
232
assert.notEqual(result.realmId, null)
233
assert.equal(result.result.type, 'bigint')
234
assert.notEqual(result.result.value, null)
235
assert.equal(result.result.value, '42')
236
})
237
238
it('can call function with array argument', async function () {
239
const id = await driver.getWindowHandle()
240
const manager = await ScriptManager(id, driver)
241
let argumentValues = []
242
let arrayValue = [LocalValue.createStringValue('foobar')]
243
let value = LocalValue.createArrayValue(arrayValue)
244
argumentValues.push(value)
245
246
const result = await manager.callFunctionInBrowsingContext(
247
id,
248
'(arg) => {{\n' +
249
' if(! (arg instanceof Array))\n' +
250
' throw Error("Argument type should be Array, but was "+\n' +
251
' Object.prototype.toString.call(arg));\n' +
252
' return arg;\n' +
253
' }}',
254
false,
255
argumentValues,
256
)
257
258
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
259
assert.notEqual(result.realmId, null)
260
assert.equal(result.result.type, 'array')
261
assert.notEqual(result.result.value, null)
262
263
let resultValue = result.result.value
264
assert.equal(resultValue.length, 1)
265
assert.equal(resultValue[0].type, 'string')
266
assert.equal(resultValue[0].value, 'foobar')
267
})
268
269
it('can call function with set argument', async function () {
270
const id = await driver.getWindowHandle()
271
const manager = await ScriptManager(id, driver)
272
let argumentValues = []
273
let setValue = [LocalValue.createStringValue('foobar')]
274
let value = LocalValue.createSetValue(setValue)
275
argumentValues.push(value)
276
277
const result = await manager.callFunctionInBrowsingContext(
278
id,
279
'(arg) => {{\n' +
280
' if(! (arg instanceof Set))\n' +
281
' throw Error("Argument type should be Set, but was "+\n' +
282
' Object.prototype.toString.call(arg));\n' +
283
' return arg;\n' +
284
' }}',
285
false,
286
argumentValues,
287
)
288
289
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
290
assert.notEqual(result.realmId, null)
291
assert.equal(result.result.type, 'set')
292
assert.notEqual(result.result.value, null)
293
294
let resultValue = result.result.value
295
assert.equal(resultValue.length, 1)
296
assert.equal(resultValue[0].type, 'string')
297
assert.equal(resultValue[0].value, 'foobar')
298
})
299
300
it('can call function with date argument', async function () {
301
const id = await driver.getWindowHandle()
302
const manager = await ScriptManager(id, driver)
303
let argumentValues = []
304
let value = LocalValue.createDateValue('2022-05-31T13:47:29.000Z')
305
argumentValues.push(value)
306
307
const result = await manager.callFunctionInBrowsingContext(
308
id,
309
'(arg) => {{\n' +
310
' if(! (arg instanceof Date))\n' +
311
' throw Error("Argument type should be Date, but was "+\n' +
312
' Object.prototype.toString.call(arg));\n' +
313
' return arg;\n' +
314
' }}',
315
false,
316
argumentValues,
317
)
318
319
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
320
assert.notEqual(result.realmId, null)
321
assert.equal(result.result.type, 'date')
322
assert.notEqual(result.result.value, null)
323
assert.equal(result.result.value, '2022-05-31T13:47:29.000Z')
324
})
325
326
it('can call function with map argument', async function () {
327
const id = await driver.getWindowHandle()
328
const manager = await ScriptManager(id, driver)
329
let argumentValues = []
330
let mapValue = { foobar: LocalValue.createStringValue('foobar') }
331
let value = LocalValue.createMapValue(mapValue)
332
argumentValues.push(value)
333
334
const result = await manager.callFunctionInBrowsingContext(
335
id,
336
'(arg) => {{\n' +
337
' if(! (arg instanceof Map))\n' +
338
' throw Error("Argument type should be Map, but was "+\n' +
339
' Object.prototype.toString.call(arg));\n' +
340
' return arg;\n' +
341
' }}',
342
false,
343
argumentValues,
344
)
345
346
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
347
assert.notEqual(result.realmId, null)
348
assert.equal(result.result.type, 'map')
349
assert.notEqual(result.result.value, null)
350
351
let resultValue = result.result.value
352
353
assert.equal(resultValue[0][0], 'foobar')
354
assert.equal(resultValue[0][1].type, 'string')
355
assert.equal(resultValue[0][1].value, 'foobar')
356
})
357
358
it('can call function with object argument', async function () {
359
const id = await driver.getWindowHandle()
360
const manager = await ScriptManager(id, driver)
361
let argumentValues = []
362
let mapValue = { foobar: LocalValue.createStringValue('foobar') }
363
let value = LocalValue.createObjectValue(mapValue)
364
argumentValues.push(value)
365
366
const result = await manager.callFunctionInBrowsingContext(
367
id,
368
'(arg) => {{\n' +
369
' if(! (arg instanceof Object))\n' +
370
' throw Error("Argument type should be Object, but was "+\n' +
371
' Object.prototype.toString.call(arg));\n' +
372
' return arg;\n' +
373
' }}',
374
false,
375
argumentValues,
376
)
377
378
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
379
assert.notEqual(result.realmId, null)
380
assert.equal(result.result.type, 'object')
381
assert.notEqual(result.result.value, null)
382
383
let resultValue = result.result.value
384
assert.equal(Object.keys(resultValue).length, 1)
385
assert.equal(resultValue['foobar'].type, 'string')
386
assert.equal(resultValue['foobar'].value, 'foobar')
387
})
388
389
it('can call function with regex argument', async function () {
390
const id = await driver.getWindowHandle()
391
const manager = await ScriptManager(id, driver)
392
let argumentValues = []
393
let value = LocalValue.createRegularExpressionValue(new RegExpValue('foo', 'g'))
394
argumentValues.push(value)
395
396
const result = await manager.callFunctionInBrowsingContext(
397
id,
398
'(arg) => {{\n' +
399
' if(! (arg instanceof RegExp))\n' +
400
' throw Error("Argument type should be RegExp, but was "+\n' +
401
' Object.prototype.toString.call(arg));\n' +
402
' return arg;\n' +
403
' }}',
404
false,
405
argumentValues,
406
)
407
408
assert.equal(result.resultType, EvaluateResultType.SUCCESS)
409
assert.notEqual(result.realmId, null)
410
assert.equal(result.result.type, 'regexp')
411
assert.notEqual(result.result.value, null)
412
413
let resultValue = result.result.value
414
assert.equal(resultValue.pattern, 'foo')
415
assert.equal(resultValue.flags, 'g')
416
})
417
})
418
},
419
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
420
)
421
422