UNPKG

selenium-webdriver

Version:

The official WebDriver JavaScript bindings from the Selenium project

281 lines (280 loc) 10.1 kB
"use strict"; // 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.BrowsingContext = void 0; exports.getBrowsingContextInstance = getBrowsingContextInstance; // --- Additional Types --- /** @deprecated Use {@link BrowsingContext.create} instead — will be removed in a future major version. */ async function getBrowsingContextInstance(driver) { return BrowsingContext.create(driver); } // --- Implementation --- class BrowsingContext { 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 BrowsingContext(bidi); } async activate(params) { const response = await this.bidi.send({ method: 'browsingContext.activate', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } } async captureScreenshot(params) { const response = await this.bidi.send({ method: 'browsingContext.captureScreenshot', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async close(params) { const response = await this.bidi.send({ method: 'browsingContext.close', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } } async create(params) { const response = await this.bidi.send({ method: 'browsingContext.create', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async getTree(params) { const response = await this.bidi.send({ method: 'browsingContext.getTree', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async handleUserPrompt(params) { const response = await this.bidi.send({ method: 'browsingContext.handleUserPrompt', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } } async locateNodes(params) { const response = await this.bidi.send({ method: 'browsingContext.locateNodes', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async navigate(params) { const response = await this.bidi.send({ method: 'browsingContext.navigate', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async print(params) { const response = await this.bidi.send({ method: 'browsingContext.print', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async reload(params) { const response = await this.bidi.send({ method: 'browsingContext.reload', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async setBypassCSP(params) { const response = await this.bidi.send({ method: 'browsingContext.setBypassCSP', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } } async setViewport(params) { const response = await this.bidi.send({ method: 'browsingContext.setViewport', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } } async startScreencast(params) { const response = await this.bidi.send({ method: 'browsingContext.startScreencast', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async stopScreencast(params) { const response = await this.bidi.send({ method: 'browsingContext.stopScreencast', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } return response.result; } async traverseHistory(params) { const response = await this.bidi.send({ method: 'browsingContext.traverseHistory', params: params, }); if (response['type'] === 'error') { throw new Error(`${response['error']}: ${response['message']}`); } } async onContextCreated(callback) { await this.bidi.subscribe('browsingContext.contextCreated'); this.bidi.on('browsingContext.contextCreated', (params) => { callback(params); }); } async onContextDestroyed(callback) { await this.bidi.subscribe('browsingContext.contextDestroyed'); this.bidi.on('browsingContext.contextDestroyed', (params) => { callback(params); }); } async onDomContentLoaded(callback) { await this.bidi.subscribe('browsingContext.domContentLoaded'); this.bidi.on('browsingContext.domContentLoaded', (params) => { callback(params); }); } async onDownloadEnd(callback) { await this.bidi.subscribe('browsingContext.downloadEnd'); this.bidi.on('browsingContext.downloadEnd', (params) => { callback(params); }); } async onDownloadWillBegin(callback) { await this.bidi.subscribe('browsingContext.downloadWillBegin'); this.bidi.on('browsingContext.downloadWillBegin', (params) => { callback(params); }); } async onFragmentNavigated(callback) { await this.bidi.subscribe('browsingContext.fragmentNavigated'); this.bidi.on('browsingContext.fragmentNavigated', (params) => { callback(params); }); } async onHistoryUpdated(callback) { await this.bidi.subscribe('browsingContext.historyUpdated'); this.bidi.on('browsingContext.historyUpdated', (params) => { callback(params); }); } async onLoad(callback) { await this.bidi.subscribe('browsingContext.load'); this.bidi.on('browsingContext.load', (params) => { callback(params); }); } async onNavigationAborted(callback) { await this.bidi.subscribe('browsingContext.navigationAborted'); this.bidi.on('browsingContext.navigationAborted', (params) => { callback(params); }); } async onNavigationCommitted(callback) { await this.bidi.subscribe('browsingContext.navigationCommitted'); this.bidi.on('browsingContext.navigationCommitted', (params) => { callback(params); }); } async onNavigationFailed(callback) { await this.bidi.subscribe('browsingContext.navigationFailed'); this.bidi.on('browsingContext.navigationFailed', (params) => { callback(params); }); } async onNavigationStarted(callback) { await this.bidi.subscribe('browsingContext.navigationStarted'); this.bidi.on('browsingContext.navigationStarted', (params) => { callback(params); }); } async onUserPromptClosed(callback) { await this.bidi.subscribe('browsingContext.userPromptClosed'); this.bidi.on('browsingContext.userPromptClosed', (params) => { callback(params); }); } async onUserPromptOpened(callback) { await this.bidi.subscribe('browsingContext.userPromptOpened'); this.bidi.on('browsingContext.userPromptOpened', (params) => { callback(params); }); } async back(context) { await this.traverseHistory({ context, delta: -1 }); } async forward(context) { await this.traverseHistory({ context, delta: 1 }); } async getTopLevelContexts() { return this.getTree({}); } async printPage(params) { return this.print(params); } } exports.BrowsingContext = BrowsingContext;