Skip to content

[bidi][js] Add high-level script command #14293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 84 additions & 2 deletions javascript/node/selenium-webdriver/bidi/protocolValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// specific language governing permissions and limitations
// under the License.

const { PrimitiveType, NonPrimitiveType, RemoteType } = require('./protocolType')
const { PrimitiveType, NonPrimitiveType, RemoteType, SpecialNumberType } = require('./protocolType')

const TYPE_CONSTANT = 'type'
const VALUE_CONSTANT = 'value'
Expand Down Expand Up @@ -189,6 +189,88 @@ class LocalValue {
return new ReferenceValue(handle, sharedId)
}

static getArgument(argument) {
let localValue = null

if (
argument === SpecialNumberType.NAN ||
argument === SpecialNumberType.MINUS_ZERO ||
argument === SpecialNumberType.INFINITY ||
argument === SpecialNumberType.MINUS_INFINITY
) {
localValue = LocalValue.createSpecialNumberValue(argument)
return localValue
}

const type = typeof argument

switch (type) {
case PrimitiveType.STRING:
localValue = LocalValue.createStringValue(argument)
break
case PrimitiveType.NUMBER:
localValue = LocalValue.createNumberValue(argument)
break
case PrimitiveType.BOOLEAN:
localValue = LocalValue.createBooleanValue(argument)
break
case PrimitiveType.BIGINT:
localValue = LocalValue.createBigIntValue(argument.toString())
break
case PrimitiveType.UNDEFINED:
localValue = LocalValue.createUndefinedValue()
break
case NonPrimitiveType.OBJECT:
if (argument === null) {
localValue = LocalValue.createNullValue()
break
}
if (argument instanceof Date) {
localValue = LocalValue.createDateValue(argument)
} else if (argument instanceof Map) {
const map = []

argument.forEach((value, key) => {
let objectKey
if (typeof key === 'string') {
objectKey = key
} else {
objectKey = LocalValue.getArgument(key)
}
const objectValue = LocalValue.getArgument(value)
map.push([objectKey, objectValue])
})
localValue = new LocalValue(NonPrimitiveType.MAP, map)
} else if (argument instanceof Set) {
const set = []
argument.forEach((value) => {
set.push(LocalValue.getArgument(value))
})
localValue = LocalValue.createSetValue(set)
} else if (argument instanceof Array) {
const arr = []
argument.forEach((value) => {
arr.push(LocalValue.getArgument(value))
})
localValue = LocalValue.createArrayValue(arr)
} else if (argument instanceof RegExp) {
localValue = LocalValue.createRegularExpressionValue({
pattern: argument.source,
flags: argument.flags,
})
} else {
let value = []
Object.entries(argument).forEach((entry) => {
value.push([LocalValue.getArgument(entry[0]), LocalValue.getArgument(entry[1])])
})
localValue = new LocalValue(NonPrimitiveType.OBJECT, value)
}
break
}

return localValue
}

asMap() {
let toReturn = {}
toReturn[TYPE_CONSTANT] = this.type
Expand Down Expand Up @@ -246,7 +328,7 @@ class RemoteValue {
}

deserializeValue(value, type) {
if ([NonPrimitiveType.MAP, NonPrimitiveType.OBJECT].includes(type)) {
if (type === NonPrimitiveType.OBJECT) {
return Object.fromEntries(value)
} else if (type === NonPrimitiveType.REGULAR_EXPRESSION) {
return new RegExpValue(value.pattern, value.flags)
Expand Down
16 changes: 16 additions & 0 deletions javascript/node/selenium-webdriver/lib/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,22 @@ class Script {
await this.#initScript()
await this.#script.removePreloadScript(id)
}

async execute(script, ...args) {
await this.#initScript()

const browsingContextId = await this.#driver.getWindowHandle()

const argumentList = []

args.forEach((arg) => {
argumentList.push(LocalValue.getArgument(arg))
})

const response = await this.#script.callFunctionInBrowsingContext(browsingContextId, script, true, argumentList)

return response.result
}
}

module.exports = Script
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ suite(

let resultValue = result.result.value

assert.equal(Object.keys(resultValue).length, 1)
assert.equal(resultValue['foobar'].type, 'string')
assert.equal(resultValue['foobar'].value, 'foobar')
assert.equal(resultValue[0][0], 'foobar')
assert.equal(resultValue[0][1].type, 'string')
assert.equal(resultValue[0][1].value, 'foobar')
})

it('can call function with object argument', async function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ suite(
assert.equal(response.resultType, EvaluateResultType.SUCCESS)
assert.equal(response.result.type, 'map')

const sharedId = response.result.value.sharedId
const value = response.result.value[0]

assert.strictEqual(sharedId.value, nodeId)
assert.strictEqual(value[1].type, 'string')
assert.strictEqual(value[1].value, nodeId)
})

it('can find element', async function () {
Expand Down
Loading
Loading