Path: blob/trunk/javascript/selenium-webdriver/bidi/browsingContextInspector.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const { BrowsingContextInfo, NavigationInfo, UserPromptOpened, UserPromptClosed } = require('./browsingContextTypes')1819/**20* Represents a browsing context related events.21* Described in https://w3c.github.io/webdriver-bidi/#module-contexts-events.22* While BrowsingContext class represents a browsing context lifecycle and related commands.23* This class is specific to listening to events. Events can be subscribed to multiple browsing contexts or all of them.24*/25class BrowsingContextInspector {26constructor(driver, browsingContextIds) {27this._driver = driver28this._browsingContextIds = browsingContextIds29}3031async init() {32this.bidi = await this._driver.getBidi()33}3435/**36* Subscribes to the 'browsingContext.contextCreated' event.37* @param {Function} callback - The callback function to handle the event.38* @returns {Promise<void>} - A promise that resolves when the event is emitted.39*/40async onBrowsingContextCreated(callback) {41await this.subscribeAndHandleEvent('browsingContext.contextCreated', callback)42}4344/**45* Subscribes to the 'browsingContext.contextDestroyed' event.46* @param {Function} callback - The callback function to handle the event.47* @returns {Promise<void>} - A promise that resolves when the event is emitted.48*/49async onBrowsingContextDestroyed(callback) {50await this.subscribeAndHandleEvent('browsingContext.contextDestroyed', callback)51}5253/**54* Subscribe to the 'browsingContext.navigationStarted' event.55* @param {Function} callback - The callback function to handle the event.56* @returns {Promise<void>} - A promise that resolves when the event is emitted.57*/58async onNavigationStarted(callback) {59await this.subscribeAndHandleEvent('browsingContext.navigationStarted', callback)60}6162/**63* Subscribes to the 'browsingContext.fragmentNavigated' event.64*65* @param {Function} callback - The callback function to handle the event.66* @returns {Promise<void>} - A promise that resolves when the event is emitted.67*/68async onFragmentNavigated(callback) {69await this.subscribeAndHandleEvent('browsingContext.fragmentNavigated', callback)70}7172/**73* Subscribes to the 'browsingContext.userPromptClosed' event.74*75* @param {Function} callback - The callback function to handle the event.76* @returns {Promise<void>} - A promise that resolves when the event is emitted.77*/78async onUserPromptClosed(callback) {79await this.subscribeAndHandleEvent('browsingContext.userPromptClosed', callback)80}8182/**83* Subscribes to the 'browsingContext.userPromptOpened' event.84*85* @param {Function} callback - The callback function to handle the event.86* @returns {Promise<void>} - A promise that resolves when the event is emitted.87*/88async onUserPromptOpened(callback) {89await this.subscribeAndHandleEvent('browsingContext.userPromptOpened', callback)90}9192/**93* Subscribes to the 'browsingContext.domContentLoaded' event.94*95* @param {Function} callback - The callback function to handle the event.96* @returns {Promise<void>} - A promise that resolves when the event is emitted.97*/98async onDomContentLoaded(callback) {99await this.subscribeAndHandleEvent('browsingContext.domContentLoaded', callback)100}101102/**103* Subscribes to the 'browsingContext.load' event.104*105* @param {Function} callback - The callback function to handle the event.106* @returns {Promise<void>} - A promise that resolves when the event is emitted.107*/108async onBrowsingContextLoaded(callback) {109await this.subscribeAndHandleEvent('browsingContext.load', callback)110}111112async subscribeAndHandleEvent(eventType, callback) {113if (this._browsingContextIds != null) {114await this.bidi.subscribe(eventType, this._browsingContextIds)115} else {116await this.bidi.subscribe(eventType)117}118await this._on(callback)119}120121async _on(callback) {122this.ws = await this.bidi.socket123this.ws.on('message', (event) => {124const { params } = JSON.parse(Buffer.from(event.toString()))125if (params) {126let response = null127if ('navigation' in params) {128response = new NavigationInfo(params.context, params.navigation, params.timestamp, params.url)129} else if ('accepted' in params) {130response = new UserPromptClosed(params.context, params.accepted, params.userText)131} else if ('type' in params) {132response = new UserPromptOpened(params.context, params.type, params.message)133} else {134response = new BrowsingContextInfo(params.context, params.url, params.children, params.parent)135}136callback(response)137}138})139}140141async close() {142if (143this._browsingContextIds !== null &&144this._browsingContextIds !== undefined &&145this._browsingContextIds.length > 0146) {147await this.bidi.unsubscribe(148'browsingContext.contextCreated',149'browsingContext.contextDestroyed',150'browsingContext.fragmentNavigated',151'browsingContext.userPromptClosed',152this._browsingContextIds,153)154} else {155await this.bidi.unsubscribe(156'browsingContext.contextCreated',157'browsingContext.contextDestroyed',158'browsingContext.fragmentNavigated',159'browsingContext.userPromptClosed',160)161}162}163}164165async function getBrowsingContextInstance(driver, browsingContextIds = null) {166let instance = new BrowsingContextInspector(driver, browsingContextIds)167await instance.init()168return instance169}170171module.exports = getBrowsingContextInstance172173174