Path: blob/trunk/javascript/selenium-webdriver/bidi/networkInspector.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 { BeforeRequestSent, ResponseStarted } = require('./networkTypes')1819/**20* @deprecated21* in favor of using the `Network` class from `bidi/network.js`22* Inspector is specific to listening to events.23* Goal is to club commands and events under one class called Network.24*/25class NetworkInspector {26constructor(driver, browsingContextIds) {27this._driver = driver28this._browsingContextIds = browsingContextIds29}3031async init() {32this.bidi = await this._driver.getBidi()33}3435async beforeRequestSent(callback) {36await this.subscribeAndHandleEvent('network.beforeRequestSent', callback)37}3839async responseStarted(callback) {40await this.subscribeAndHandleEvent('network.responseStarted', callback)41}4243async responseCompleted(callback) {44await this.subscribeAndHandleEvent('network.responseCompleted', callback)45}4647async authRequired(callback) {48await this.subscribeAndHandleEvent('network.authRequired', callback)49}5051async subscribeAndHandleEvent(eventType, callback) {52if (this._browsingContextIds != null) {53await this.bidi.subscribe(eventType, this._browsingContextIds)54} else {55await this.bidi.subscribe(eventType)56}57await this._on(callback)58}5960async _on(callback) {61this.ws = await this.bidi.socket62this.ws.on('message', (event) => {63const { params } = JSON.parse(Buffer.from(event.toString()))64if (params) {65let response = null66if ('initiator' in params) {67response = new BeforeRequestSent(68params.context,69params.navigation,70params.redirectCount,71params.request,72params.timestamp,73params.initiator,74)75} else if ('response' in params) {76response = new ResponseStarted(77params.context,78params.navigation,79params.redirectCount,80params.request,81params.timestamp,82params.response,83)84}85callback(response)86}87})88}89}9091async function getNetworkInspectorInstance(driver, browsingContextIds = null) {92let instance = new NetworkInspector(driver, browsingContextIds)93await instance.init()94return instance95}9697module.exports = getNetworkInspectorInstance9899100