Skip to content

Commit 574fea7

Browse files
committed
[bidi][js] Deprecate NetworkInspector in favor of Network
1 parent 4d6bdd0 commit 574fea7

File tree

3 files changed

+116
-18
lines changed

3 files changed

+116
-18
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
class Network {
21+
constructor(driver, browsingContextIds) {
22+
this._driver = driver
23+
this._browsingContextIds = browsingContextIds
24+
}
25+
26+
async init() {
27+
this.bidi = await this._driver.getBidi()
28+
}
29+
30+
async beforeRequestSent(callback) {
31+
await this.subscribeAndHandleEvent('network.beforeRequestSent', callback)
32+
}
33+
34+
async responseStarted(callback) {
35+
await this.subscribeAndHandleEvent('network.responseStarted', callback)
36+
}
37+
38+
async responseCompleted(callback) {
39+
await this.subscribeAndHandleEvent('network.responseCompleted', callback)
40+
}
41+
42+
async authRequired(callback) {
43+
await this.subscribeAndHandleEvent('network.authRequired', callback)
44+
}
45+
46+
async subscribeAndHandleEvent(eventType, callback) {
47+
if (this._browsingContextIds != null) {
48+
await this.bidi.subscribe(eventType, this._browsingContextIds)
49+
} else {
50+
await this.bidi.subscribe(eventType)
51+
}
52+
await this._on(callback)
53+
}
54+
55+
async _on(callback) {
56+
this.ws = await this.bidi.socket
57+
this.ws.on('message', (event) => {
58+
const { params } = JSON.parse(Buffer.from(event.toString()))
59+
if (params) {
60+
let response = null
61+
if ('initiator' in params) {
62+
response = new BeforeRequestSent(
63+
params.context,
64+
params.navigation,
65+
params.redirectCount,
66+
params.request,
67+
params.timestamp,
68+
params.initiator
69+
)
70+
} else if ('response' in params) {
71+
response = new ResponseStarted(
72+
params.context,
73+
params.navigation,
74+
params.redirectCount,
75+
params.request,
76+
params.timestamp,
77+
params.response
78+
)
79+
}
80+
callback(response)
81+
}
82+
})
83+
}
84+
}
85+
86+
async function getNetworkInstance(driver, browsingContextIds = null) {
87+
let instance = new Network(driver, browsingContextIds)
88+
await instance.init()
89+
return instance
90+
}
91+
92+
module.exports = getNetworkInstance

javascript/node/selenium-webdriver/bidi/networkInspector.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
const { BeforeRequestSent, ResponseStarted } = require('./networkTypes')
1919

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+
*/
2026
class NetworkInspector {
2127
constructor(driver, browsingContextIds) {
2228
this._driver = driver

javascript/node/selenium-webdriver/test/bidi/network_test.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const assert = require('assert')
2121
const firefox = require('../../firefox')
2222
const { Browser, By, WebElement } = require('../../')
2323
const { Pages, suite } = require('../../lib/test')
24-
const NetworkInspector = require('../../bidi/networkInspector')
24+
const Network = require('../../bidi/network')
2525
const until = require('../../lib/until')
2626

2727
suite(
@@ -39,11 +39,11 @@ suite(
3939
await driver.quit()
4040
})
4141

42-
describe('Network Inspector', function () {
42+
describe('Network network', function () {
4343
it('can listen to event before request is sent', async function () {
4444
let beforeRequestEvent = null
45-
const inspector = await NetworkInspector(driver)
46-
await inspector.beforeRequestSent(function (event) {
45+
const network = await Network(driver)
46+
await network.beforeRequestSent(function (event) {
4747
beforeRequestEvent = event
4848
})
4949

@@ -55,9 +55,9 @@ suite(
5555
})
5656

5757
it('can request cookies', async function () {
58-
const inspector = await NetworkInspector(driver)
58+
const network = await Network(driver)
5959
let beforeRequestEvent = null
60-
await inspector.beforeRequestSent(function (event) {
60+
await network.beforeRequestSent(function (event) {
6161
beforeRequestEvent = event
6262
})
6363

@@ -89,8 +89,8 @@ suite(
8989

9090
it('can redirect http equiv', async function () {
9191
let beforeRequestEvent = []
92-
const inspector = await NetworkInspector(driver)
93-
await inspector.beforeRequestSent(function (event) {
92+
const network = await Network(driver)
93+
await network.beforeRequestSent(function (event) {
9494
beforeRequestEvent.push(event)
9595
})
9696

@@ -109,8 +109,8 @@ suite(
109109

110110
it('can subscribe to response started', async function () {
111111
let onResponseStarted = []
112-
const inspector = await NetworkInspector(driver)
113-
await inspector.responseStarted(function (event) {
112+
const network = await Network(driver)
113+
await network.responseStarted(function (event) {
114114
onResponseStarted.push(event)
115115
})
116116

@@ -133,8 +133,8 @@ suite(
133133

134134
it('test response started mime type', async function () {
135135
let onResponseStarted = []
136-
const inspector = await NetworkInspector(driver)
137-
await inspector.responseStarted(function (event) {
136+
const network = await Network(driver)
137+
await network.responseStarted(function (event) {
138138
onResponseStarted.push(event)
139139
})
140140

@@ -163,8 +163,8 @@ suite(
163163

164164
it('can subscribe to response completed', async function () {
165165
let onResponseCompleted = []
166-
const inspector = await NetworkInspector(driver)
167-
await inspector.responseCompleted(function (event) {
166+
const network = await Network(driver)
167+
await network.responseCompleted(function (event) {
168168
onResponseCompleted.push(event)
169169
})
170170

@@ -188,8 +188,8 @@ suite(
188188

189189
xit('can listen to auth required event', async function () {
190190
let authRequiredEvent = null
191-
const inspector = await NetworkInspector(driver)
192-
await inspector.authRequired(function (event) {
191+
const network = await Network(driver)
192+
await network.authRequired(function (event) {
193193
authRequiredEvent = event
194194
})
195195

@@ -207,8 +207,8 @@ suite(
207207

208208
it('test response completed mime type', async function () {
209209
let onResponseCompleted = []
210-
const inspector = await NetworkInspector(driver)
211-
await inspector.responseCompleted(function (event) {
210+
const network = await Network(driver)
211+
await network.responseCompleted(function (event) {
212212
onResponseCompleted.push(event)
213213
})
214214

0 commit comments

Comments
 (0)