selenium-webdriver
Version:
The official WebDriver JavaScript bindings from the Selenium project
147 lines (146 loc) • 6.07 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.
Object.defineProperty(exports, "__esModule", { value: true });
exports.Script = void 0;
exports.getScriptManagerInstance = getScriptManagerInstance;
// --- Additional Types ---
/** @deprecated Use {@link Script.create} instead — will be removed in a future major version. */
async function getScriptManagerInstance(driver) {
return Script.create(driver);
}
// --- Implementation ---
class Script {
constructor(bidi) {
this.bidi = bidi;
}
static async create(driver) {
const caps = await driver.getCapabilities();
if (!caps.get('webSocketUrl')) {
throw new Error('WebDriver instance must support BiDi protocol');
}
const bidi = await driver.getBidi();
return new Script(bidi);
}
async addPreloadScript(params) {
const response = await this.bidi.send({
method: 'script.addPreloadScript',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async callFunction(params) {
const response = await this.bidi.send({
method: 'script.callFunction',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async disown(params) {
const response = await this.bidi.send({
method: 'script.disown',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async evaluate(params) {
const response = await this.bidi.send({
method: 'script.evaluate',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async getRealms(params) {
const response = await this.bidi.send({
method: 'script.getRealms',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async removePreloadScript(params) {
const response = await this.bidi.send({
method: 'script.removePreloadScript',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async onMessage(callback) {
await this.bidi.subscribe('script.message');
this.bidi.on('script.message', (params) => {
callback(params);
});
}
async onRealmCreated(callback) {
await this.bidi.subscribe('script.realmCreated');
this.bidi.on('script.realmCreated', (params) => {
callback(params);
});
}
async onRealmDestroyed(callback) {
await this.bidi.subscribe('script.realmDestroyed');
this.bidi.on('script.realmDestroyed', (params) => {
callback(params);
});
}
async getAllRealms() {
return this.getRealms({});
}
async getRealmsByType(type) {
return this.getRealms({ type });
}
async getRealmsInBrowsingContext(context) {
return this.getRealms({ context });
}
async getRealmsInBrowsingContextByType(context, type) {
return this.getRealms({ context, type });
}
async callFunctionInRealm(realmId, functionDeclaration, awaitPromise, args, resultOwnership, serializationOptions, userActivation) {
return this.callFunction({ functionDeclaration, awaitPromise, target: { realm: realmId }, arguments: args, resultOwnership, serializationOptions, userActivation });
}
async callFunctionInBrowsingContext(browsingContextId, functionDeclaration, awaitPromise, args, sandbox, resultOwnership, serializationOptions, userActivation) {
return this.callFunction({ functionDeclaration, awaitPromise, target: { context: browsingContextId, sandbox }, arguments: args, resultOwnership, serializationOptions, userActivation });
}
async evaluateFunctionInRealm(realmId, expression, awaitPromise, resultOwnership, serializationOptions, userActivation) {
return this.evaluate({ expression, awaitPromise, target: { realm: realmId }, resultOwnership, serializationOptions, userActivation });
}
async evaluateFunctionInBrowsingContext(browsingContextId, expression, awaitPromise, sandbox, resultOwnership, serializationOptions, userActivation) {
return this.evaluate({ expression, awaitPromise, target: { context: browsingContextId, sandbox }, resultOwnership, serializationOptions, userActivation });
}
async disownRealmScript(realmId, handles) {
await this.disown({ handles, target: { realm: realmId } });
}
async disownBrowsingContextScript(browsingContextId, handles, sandbox) {
await this.disown({ handles, target: { context: browsingContextId, sandbox } });
}
}
exports.Script = Script;