selenium-webdriver
Version:
The official WebDriver JavaScript bindings from the Selenium project
143 lines (113 loc) • 4.23 kB
JavaScript
// 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.
const logInspector = require('../bidi/logInspector')
const scriptManager = require('../bidi//scriptManager')
const { LocalValue, ChannelValue } = require('../bidi/protocolValue')
const fs = require('node:fs')
const path = require('node:path')
const by = require('./by')
class Script {
constructor(driver) {
this.
}
// This should be done in the constructor.
// But since it needs to call async methods we cannot do that in the constructor.
// We can have a separate async method that initialises the Script instance.
// However, that pattern does not allow chaining the methods as we would like the user to use it.
// Since it involves awaiting to get the instance and then another await to call the method.
// Using this allows the user to do this "await driver.script().addJavaScriptErrorHandler(callback)"
async
if (this.
return
}
this.
}
async
if (this.
return
}
this.
}
async addJavaScriptErrorHandler(callback) {
await this.
return await this.
}
async removeJavaScriptErrorHandler(id) {
await this.
await this.
}
async addConsoleMessageHandler(callback) {
await this.
return this.
}
async removeConsoleMessageHandler(id) {
await this.
await this.
}
async addDomMutationHandler(callback) {
await this.
let argumentValues = []
let value = LocalValue.createChannelValue(new ChannelValue('channel_name'))
argumentValues.push(value)
const filePath = path.join(__dirname, 'atoms', 'bidi-mutation-listener.js')
let mutationListener = fs.readFileSync(filePath, 'utf-8').toString()
await this.
let id = await this.
let payload = JSON.parse(message['data']['value'])
let elements = await this.
css: '*[data-__webdriver_id=' + by.escapeCss(payload['target']) + ']',
})
if (elements.length === 0) {
return
}
let event = {
element: elements[0],
attribute_name: payload['name'],
current_value: payload['value'],
old_value: payload['oldValue'],
}
callback(event)
})
return id
}
async removeDomMutationHandler(id) {
await this.
await this.
}
async pin(script) {
await this.
return await this.
}
async unpin(id) {
await this.
await this.
}
async execute(script, ...args) {
await this.
const browsingContextId = await this.
const argumentList = []
args.forEach((arg) => {
argumentList.push(LocalValue.getArgument(arg))
})
const response = await this.
return response.result
}
}
module.exports = Script