2015-04-04 09:53:59 -07:00
|
|
|
// 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
|
2014-11-05 14:40:28 -08:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-05 14:40:28 -08:00
|
|
|
//
|
2015-04-04 09:53:59 -07:00
|
|
|
// 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.
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
'use strict'
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2024-04-16 01:50:58 +05:30
|
|
|
const assert = require('node:assert')
|
2024-06-27 18:33:36 +01:00
|
|
|
const { Browser, By, WebElement, error } = require('selenium-webdriver')
|
2020-08-03 17:56:31 +03:00
|
|
|
const { Pages, ignore, suite } = require('../lib/test')
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
suite(function (env) {
|
|
|
|
|
var driver
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
before(async function () {
|
|
|
|
|
driver = await env.builder().build()
|
|
|
|
|
})
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
after(function () {
|
|
|
|
|
return driver.quit()
|
|
|
|
|
})
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
beforeEach(function () {
|
|
|
|
|
return driver.get(Pages.echoPage)
|
|
|
|
|
})
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
describe('executeScript;', function () {
|
|
|
|
|
var shouldHaveFailed = new Error('Should have failed')
|
2014-11-05 14:40:28 -08:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
it('fails if script throws', function () {
|
2016-10-30 11:30:58 -07:00
|
|
|
return execute('throw new Error("boom")')
|
2020-08-03 17:56:31 +03:00
|
|
|
.then(function () {
|
|
|
|
|
throw shouldHaveFailed
|
|
|
|
|
})
|
|
|
|
|
.catch(function (e) {
|
|
|
|
|
// The java WebDriver server adds a bunch of crap to error messages.
|
|
|
|
|
// Error message will just be "JavaScript error" for IE.
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.ok(/.*(JavaScript error|boom).*/.test(e.message), `Unexpected error: ${e.message}`)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fails if script does not parse', function () {
|
2016-10-30 11:30:58 -07:00
|
|
|
return execute('throw function\\*')
|
2020-08-03 17:56:31 +03:00
|
|
|
.then(function () {
|
|
|
|
|
throw shouldHaveFailed
|
|
|
|
|
})
|
|
|
|
|
.catch(function (e) {
|
|
|
|
|
assert.notEqual(e, shouldHaveFailed)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('scripts;', function () {
|
|
|
|
|
it('do not pollute the global scope', async function () {
|
|
|
|
|
await execute('var x = 1;')
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return typeof x;'), 'undefined')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can set global variables', async function () {
|
|
|
|
|
await execute('window.x = 1234;')
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return x;'), 1234)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('may be defined as a function expression', async function () {
|
|
|
|
|
let result = await execute(function () {
|
|
|
|
|
return 1234 + 'abc'
|
|
|
|
|
})
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result, '1234abc')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('return values;', function () {
|
|
|
|
|
it('returns undefined as null', async function () {
|
|
|
|
|
assert.strictEqual(await execute('var x; return x;'), null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return null', async function () {
|
|
|
|
|
assert.strictEqual(await execute('return null;'), null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return numbers', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return 1234'), 1234)
|
|
|
|
|
assert.strictEqual(await execute('return 3.1456'), 3.1456)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return strings', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return "hello"'), 'hello')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return booleans', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return true'), true)
|
|
|
|
|
assert.strictEqual(await execute('return false'), false)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return an array of primitives', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return execute('var x; return [1, false, null, 3.14, x]').then(verifyJson([1, false, null, 3.14, null]))
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return nested arrays', function () {
|
|
|
|
|
return execute('return [[1, 2, [3]]]').then(verifyJson([[1, 2, [3]]]))
|
|
|
|
|
})
|
|
|
|
|
|
2024-02-07 16:07:24 +00:00
|
|
|
ignore(env.browsers(Browser.INTERNET_EXPLORER)).it('can return empty object literal', function () {
|
|
|
|
|
return execute('return {}').then(verifyJson({}))
|
|
|
|
|
})
|
2020-08-03 17:56:31 +03:00
|
|
|
|
|
|
|
|
it('can return object literals', function () {
|
|
|
|
|
return execute('return {a: 1, b: false, c: null}').then((result) => {
|
|
|
|
|
verifyJson(['a', 'b', 'c'])(Object.keys(result).sort())
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result.a, 1)
|
|
|
|
|
assert.strictEqual(result.b, false)
|
2020-08-03 17:56:31 +03:00
|
|
|
assert.strictEqual(result.c, null)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return complex object literals', function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
return execute('return {a:{b: "hello"}}').then(verifyJson({ a: { b: 'hello' } }))
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return dom elements as web elements', async function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
let result = await execute('return document.querySelector(".header.host")')
|
2020-08-03 17:56:31 +03:00
|
|
|
assert.ok(result instanceof WebElement)
|
|
|
|
|
|
|
|
|
|
let text = await result.getText()
|
|
|
|
|
assert.ok(text.startsWith('host: '), `got: ${text}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return array of dom elements', async function () {
|
|
|
|
|
let result = await execute(
|
2024-02-07 16:07:24 +00:00
|
|
|
'var nodes = document.querySelectorAll(".request,.host");' + 'return [nodes[0], nodes[1]];',
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result.length, 2)
|
2020-08-03 17:56:31 +03:00
|
|
|
|
|
|
|
|
assert.ok(result[0] instanceof WebElement)
|
|
|
|
|
assert.ok((await result[0].getText()).startsWith('GET '))
|
|
|
|
|
|
|
|
|
|
assert.ok(result[1] instanceof WebElement)
|
|
|
|
|
assert.ok((await result[1].getText()).startsWith('host: '))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return a NodeList as an array of web elements', async function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
let result = await execute('return document.querySelectorAll(".request,.host");')
|
2020-08-03 17:56:31 +03:00
|
|
|
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result.length, 2)
|
2020-08-03 17:56:31 +03:00
|
|
|
|
|
|
|
|
assert.ok(result[0] instanceof WebElement)
|
|
|
|
|
assert.ok((await result[0].getText()).startsWith('GET '))
|
|
|
|
|
|
|
|
|
|
assert.ok(result[1] instanceof WebElement)
|
|
|
|
|
assert.ok((await result[1].getText()).startsWith('host: '))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can return object literal with element property', async function () {
|
|
|
|
|
let result = await execute('return {a: document.body}')
|
|
|
|
|
|
|
|
|
|
assert.ok(result.a instanceof WebElement)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual((await result.a.getTagName()).toLowerCase(), 'body')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('parameters;', function () {
|
|
|
|
|
it('can pass numeric arguments', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return arguments[0]', 12), 12)
|
|
|
|
|
assert.strictEqual(await execute('return arguments[0]', 3.14), 3.14)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can pass boolean arguments', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return arguments[0]', true), true)
|
|
|
|
|
assert.strictEqual(await execute('return arguments[0]', false), false)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can pass string arguments', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return arguments[0]', 'hi'), 'hi')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can pass null arguments', async function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await execute('return arguments[0] === null', null), true)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return arguments[0]', null), null)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('passes undefined as a null argument', async function () {
|
|
|
|
|
var x
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await execute('return arguments[0] === null', x), true)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return arguments[0]', x), null)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can pass multiple arguments', async function () {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(await execute('return arguments.length'), 0)
|
2024-02-07 16:07:24 +00:00
|
|
|
assert.strictEqual(await execute('return arguments.length', 1, 'a', false), 3)
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
ignore(env.browsers(Browser.FIREFOX, Browser.SAFARI)).it(
|
|
|
|
|
'can return arguments object as array',
|
|
|
|
|
async function () {
|
|
|
|
|
let val = await execute('return arguments', 1, 'a', false)
|
|
|
|
|
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(val.length, 3)
|
|
|
|
|
assert.strictEqual(val[0], 1)
|
|
|
|
|
assert.strictEqual(val[1], 'a')
|
|
|
|
|
assert.strictEqual(val[2], false)
|
2024-02-07 16:07:24 +00:00
|
|
|
},
|
2020-08-03 17:56:31 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
it('can pass object literal', async function () {
|
2024-02-07 16:07:24 +00:00
|
|
|
let result = await execute('return [typeof arguments[0], arguments[0].a]', { a: 'hello' })
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result[0], 'object')
|
|
|
|
|
assert.strictEqual(result[1], 'hello')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('WebElement arguments are passed as DOM elements', async function () {
|
|
|
|
|
let el = await driver.findElement(By.tagName('div'))
|
2024-02-07 16:07:24 +00:00
|
|
|
let result = await execute('return arguments[0].tagName.toLowerCase();', el)
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result, 'div')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can pass array containing object literals', async function () {
|
|
|
|
|
let result = await execute('return arguments[0]', [{ color: 'red' }])
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(result.length, 1)
|
|
|
|
|
assert.strictEqual(result[0].color, 'red')
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('does not modify object literal parameters', function () {
|
|
|
|
|
var input = { color: 'red' }
|
|
|
|
|
return execute('return arguments[0];', input).then(verifyJson(input))
|
|
|
|
|
})
|
|
|
|
|
})
|
2014-12-02 16:07:18 +11:00
|
|
|
|
|
|
|
|
// See https://code.google.com/p/selenium/issues/detail?id=8223.
|
2020-08-03 17:56:31 +03:00
|
|
|
describe('issue 8223;', function () {
|
|
|
|
|
describe('using for..in loops;', function () {
|
|
|
|
|
it('can return array built from for-loop index', function () {
|
|
|
|
|
return execute(function () {
|
|
|
|
|
var ret = []
|
2014-12-02 16:07:18 +11:00
|
|
|
for (var i = 0; i < 3; i++) {
|
2020-08-03 17:56:31 +03:00
|
|
|
ret.push(i)
|
2014-12-02 16:07:18 +11:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
return ret
|
|
|
|
|
}).then(verifyJson[(0, 1, 2)])
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can copy input array contents', function () {
|
|
|
|
|
return execute(
|
|
|
|
|
function (input) {
|
|
|
|
|
var ret = []
|
|
|
|
|
for (var i in input) {
|
|
|
|
|
ret.push(input[i])
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
},
|
2024-02-07 16:07:24 +00:00
|
|
|
['fa', 'fe', 'fi'],
|
2020-08-03 17:56:31 +03:00
|
|
|
).then(verifyJson(['fa', 'fe', 'fi']))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can iterate over input object keys', function () {
|
|
|
|
|
return execute(
|
|
|
|
|
function (thing) {
|
|
|
|
|
var ret = []
|
|
|
|
|
for (var w in thing.words) {
|
|
|
|
|
ret.push(thing.words[w].word)
|
|
|
|
|
}
|
|
|
|
|
return ret
|
|
|
|
|
},
|
2024-02-07 16:07:24 +00:00
|
|
|
{ words: [{ word: 'fa' }, { word: 'fe' }, { word: 'fi' }] },
|
2020-08-03 17:56:31 +03:00
|
|
|
).then(verifyJson(['fa', 'fe', 'fi']))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('recursive functions;', function () {
|
|
|
|
|
it('can build array from input', function () {
|
|
|
|
|
var input = ['fa', 'fe', 'fi']
|
|
|
|
|
return execute(function (thearray) {
|
|
|
|
|
var ret = []
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2014-12-02 16:07:18 +11:00
|
|
|
function build_response(thearray, ret) {
|
2020-08-03 17:56:31 +03:00
|
|
|
ret.push(thearray.shift())
|
2024-02-07 16:07:24 +00:00
|
|
|
return (!thearray.length && ret) || build_response(thearray, ret)
|
2014-12-02 16:07:18 +11:00
|
|
|
}
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
return build_response(thearray, ret)
|
|
|
|
|
}, input).then(verifyJson(input))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('can build array from elements in object', function () {
|
|
|
|
|
var input = {
|
|
|
|
|
words: [{ word: 'fa' }, { word: 'fe' }, { word: 'fi' }],
|
|
|
|
|
}
|
|
|
|
|
return execute(function (thing) {
|
|
|
|
|
var ret = []
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2014-12-02 16:07:18 +11:00
|
|
|
function build_response(thing, ret) {
|
2020-08-03 17:56:31 +03:00
|
|
|
var item = thing.words.shift()
|
|
|
|
|
ret.push(item.word)
|
2024-02-07 16:07:24 +00:00
|
|
|
return (!thing.words.length && ret) || build_response(thing, ret)
|
2014-12-02 16:07:18 +11:00
|
|
|
}
|
2024-03-08 12:13:00 +00:00
|
|
|
|
2020-08-03 17:56:31 +03:00
|
|
|
return build_response(thing, ret)
|
|
|
|
|
}, input).then(verifyJson(['fa', 'fe', 'fi']))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('async timeouts', function () {
|
|
|
|
|
var TIMEOUT_IN_MS = 200
|
|
|
|
|
var ACCEPTABLE_WAIT = TIMEOUT_IN_MS / 10
|
|
|
|
|
var TOO_LONG_WAIT = TIMEOUT_IN_MS * 10
|
|
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
|
return driver.manage().setTimeouts({ script: TIMEOUT_IN_MS })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('does not fail if script execute in time', function () {
|
|
|
|
|
return executeTimeOutScript(ACCEPTABLE_WAIT)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('fails if script took too long', function () {
|
2016-06-07 17:51:29 +02:00
|
|
|
return executeTimeOutScript(TOO_LONG_WAIT)
|
2020-08-03 17:56:31 +03:00
|
|
|
.then(function () {
|
|
|
|
|
assert.fail('it should have timed out')
|
|
|
|
|
})
|
|
|
|
|
.catch(function (e) {
|
2019-05-05 14:49:34 -07:00
|
|
|
if (env.browser.name === Browser.SAFARI) {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(e.name, error.TimeoutError.name)
|
2019-05-05 14:49:34 -07:00
|
|
|
} else {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(e.name, error.ScriptTimeoutError.name)
|
2019-05-05 14:49:34 -07:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
|
|
|
|
})
|
2016-06-07 17:51:29 +02:00
|
|
|
|
|
|
|
|
function executeTimeOutScript(sleepTime) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return driver.executeAsyncScript(function (sleepTime) {
|
|
|
|
|
var callback = arguments[arguments.length - 1]
|
2016-06-07 17:51:29 +02:00
|
|
|
setTimeout(callback, sleepTime)
|
2020-08-03 17:56:31 +03:00
|
|
|
}, sleepTime)
|
2016-06-07 17:51:29 +02:00
|
|
|
}
|
|
|
|
|
})
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|
2016-03-15 10:14:09 -07:00
|
|
|
|
2014-11-05 14:40:28 -08:00
|
|
|
function verifyJson(expected) {
|
2020-08-03 17:56:31 +03:00
|
|
|
return function (actual) {
|
2021-03-24 10:42:10 -07:00
|
|
|
assert.strictEqual(JSON.stringify(actual), JSON.stringify(expected))
|
2020-08-03 17:56:31 +03:00
|
|
|
}
|
2014-11-05 14:40:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function execute() {
|
2020-08-03 17:56:31 +03:00
|
|
|
return driver.executeScript.apply(driver, arguments)
|
2014-11-05 14:40:28 -08:00
|
|
|
}
|
2020-08-03 17:56:31 +03:00
|
|
|
})
|