Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/networkInspector.js
2884 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
const { BeforeRequestSent, ResponseStarted } = require('./networkTypes')
19
20
/**
21
* @deprecated
22
* in favor of using the `Network` class from `bidi/network.js`
23
* Inspector is specific to listening to events.
24
* Goal is to club commands and events under one class called Network.
25
*/
26
class NetworkInspector {
27
constructor(driver, browsingContextIds) {
28
this._driver = driver
29
this._browsingContextIds = browsingContextIds
30
}
31
32
async init() {
33
this.bidi = await this._driver.getBidi()
34
}
35
36
async beforeRequestSent(callback) {
37
await this.subscribeAndHandleEvent('network.beforeRequestSent', callback)
38
}
39
40
async responseStarted(callback) {
41
await this.subscribeAndHandleEvent('network.responseStarted', callback)
42
}
43
44
async responseCompleted(callback) {
45
await this.subscribeAndHandleEvent('network.responseCompleted', callback)
46
}
47
48
async authRequired(callback) {
49
await this.subscribeAndHandleEvent('network.authRequired', callback)
50
}
51
52
async subscribeAndHandleEvent(eventType, callback) {
53
if (this._browsingContextIds != null) {
54
await this.bidi.subscribe(eventType, this._browsingContextIds)
55
} else {
56
await this.bidi.subscribe(eventType)
57
}
58
await this._on(callback)
59
}
60
61
async _on(callback) {
62
this.ws = await this.bidi.socket
63
this.ws.on('message', (event) => {
64
const { params } = JSON.parse(Buffer.from(event.toString()))
65
if (params) {
66
let response = null
67
if ('initiator' in params) {
68
response = new BeforeRequestSent(
69
params.context,
70
params.navigation,
71
params.redirectCount,
72
params.request,
73
params.timestamp,
74
params.initiator,
75
)
76
} else if ('response' in params) {
77
response = new ResponseStarted(
78
params.context,
79
params.navigation,
80
params.redirectCount,
81
params.request,
82
params.timestamp,
83
params.response,
84
)
85
}
86
callback(response)
87
}
88
})
89
}
90
}
91
92
async function getNetworkInspectorInstance(driver, browsingContextIds = null) {
93
let instance = new NetworkInspector(driver, browsingContextIds)
94
await instance.init()
95
return instance
96
}
97
98
module.exports = getNetworkInspectorInstance
99
100