selenium-webdriver
Version:
The official WebDriver JavaScript bindings from the Selenium project
231 lines (230 loc) • 8.54 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.Network = void 0;
exports.getNetworkInstance = getNetworkInstance;
// --- Additional Types ---
/** @deprecated Use {@link Network.create} instead — will be removed in a future major version. */
async function getNetworkInstance(driver) {
return Network.create(driver);
}
// --- Implementation ---
class Network {
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 Network(bidi);
}
async addDataCollector(params) {
const response = await this.bidi.send({
method: 'network.addDataCollector',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async addIntercept(params) {
const response = await this.bidi.send({
method: 'network.addIntercept',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async continueRequest(params) {
const response = await this.bidi.send({
method: 'network.continueRequest',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async continueResponse(params) {
const response = await this.bidi.send({
method: 'network.continueResponse',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async continueWithAuth(paramsOrRequestId, username, password) {
let params;
if (typeof paramsOrRequestId === 'string') {
if (username === undefined || password === undefined) {
throw new Error('username and password are required when calling continueWithAuth with a request id');
}
params = {
request: paramsOrRequestId,
action: 'provideCredentials',
credentials: { type: 'password', username, password },
};
}
else {
params = paramsOrRequestId;
}
const response = await this.bidi.send({
method: 'network.continueWithAuth',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async disownData(params) {
const response = await this.bidi.send({
method: 'network.disownData',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async failRequest(params) {
const response = await this.bidi.send({
method: 'network.failRequest',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async getData(params) {
const response = await this.bidi.send({
method: 'network.getData',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
return response.result;
}
async provideResponse(params) {
const response = await this.bidi.send({
method: 'network.provideResponse',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async removeDataCollector(params) {
const response = await this.bidi.send({
method: 'network.removeDataCollector',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async removeIntercept(params) {
const response = await this.bidi.send({
method: 'network.removeIntercept',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async setCacheBehavior(params) {
const response = await this.bidi.send({
method: 'network.setCacheBehavior',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async setExtraHeaders(params) {
const response = await this.bidi.send({
method: 'network.setExtraHeaders',
params: params,
});
if (response['type'] === 'error') {
throw new Error(`${response['error']}: ${response['message']}`);
}
}
async onAuthRequired(callback) {
await this.bidi.subscribe('network.authRequired');
this.bidi.on('network.authRequired', (params) => {
callback(params);
});
}
async onBeforeRequestSent(callback) {
await this.bidi.subscribe('network.beforeRequestSent');
this.bidi.on('network.beforeRequestSent', (params) => {
callback(params);
});
}
async onFetchError(callback) {
await this.bidi.subscribe('network.fetchError');
this.bidi.on('network.fetchError', (params) => {
callback(params);
});
}
async onResponseCompleted(callback) {
await this.bidi.subscribe('network.responseCompleted');
this.bidi.on('network.responseCompleted', (params) => {
callback(params);
});
}
async onResponseStarted(callback) {
await this.bidi.subscribe('network.responseStarted');
this.bidi.on('network.responseStarted', (params) => {
callback(params);
});
}
async cancelAuth(requestId) {
await this.continueWithAuth({ request: requestId, action: 'cancel' });
}
async continueWithAuthNoCredentials(requestId) {
await this.continueWithAuth({ request: requestId, action: 'default' });
}
/** @deprecated Use {@link onBeforeRequestSent} instead — will be removed in a future major version. */
async beforeRequestSent(callback) {
await this.onBeforeRequestSent(callback);
}
/** @deprecated Use {@link onAuthRequired} instead — will be removed in a future major version. */
async authRequired(callback) {
await this.onAuthRequired(callback);
}
/** @deprecated Use {@link onFetchError} instead — will be removed in a future major version. */
async fetchError(callback) {
await this.onFetchError(callback);
}
/** @deprecated Use {@link onResponseCompleted} instead — will be removed in a future major version. */
async responseCompleted(callback) {
await this.onResponseCompleted(callback);
}
/** @deprecated Use {@link onResponseStarted} instead — will be removed in a future major version. */
async responseStarted(callback) {
await this.onResponseStarted(callback);
}
}
exports.Network = Network;