-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[js][bidi] add Network module events (#12197)
* [js][bidi] add Network module events * [js][bidi] insert await keyword
- Loading branch information
1 parent
ebb232f
commit 25dbacb
Showing
9 changed files
with
761 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!DOCTYPE html> | ||
<html></html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!DOCTYPE html> | ||
<html>redirected</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!DOCTYPE html> | ||
<head> | ||
<meta http-equiv="refresh" content="0;redirected.html" /> | ||
</head> |
88 changes: 88 additions & 0 deletions
88
javascript/node/selenium-webdriver/bidi/networkInspector.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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. | ||
|
||
const { BeforeRequestSent, ResponseStarted } = require('./networkTypes') | ||
|
||
class NetworkInspector { | ||
constructor(driver, browsingContextIds) { | ||
this._driver = driver | ||
this._browsingContextIds = browsingContextIds | ||
} | ||
|
||
async init() { | ||
this.bidi = await this._driver.getBidi() | ||
} | ||
|
||
async beforeRequestSent(callback) { | ||
await this.subscribeAndHandleEvent('network.beforeRequestSent', callback) | ||
} | ||
|
||
async responseStarted(callback) { | ||
await this.subscribeAndHandleEvent('network.responseStarted', callback) | ||
} | ||
|
||
async responseCompleted(callback) { | ||
await this.subscribeAndHandleEvent('network.responseCompleted', callback) | ||
} | ||
|
||
async subscribeAndHandleEvent(eventType, callback) { | ||
if (this._browsingContextIds != null) { | ||
await this.bidi.subscribe(eventType, this._browsingContextIds) | ||
} else { | ||
await this.bidi.subscribe(eventType) | ||
} | ||
await this._on(callback) | ||
} | ||
|
||
async _on(callback) { | ||
this.ws = await this.bidi.socket | ||
this.ws.on('message', (event) => { | ||
const { params } = JSON.parse(Buffer.from(event.toString())) | ||
if (params) { | ||
let response = null | ||
if ('initiator' in params) { | ||
response = new BeforeRequestSent( | ||
params.context, | ||
params.navigation, | ||
params.redirectCount, | ||
params.request, | ||
params.timestamp, | ||
params.initiator | ||
) | ||
} else if ('response' in params) { | ||
response = new ResponseStarted( | ||
params.context, | ||
params.navigation, | ||
params.redirectCount, | ||
params.request, | ||
params.timestamp, | ||
params.response | ||
) | ||
} | ||
callback(response) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
async function getNetworkInspectorInstance(driver, browsingContextIds = null) { | ||
let instance = new NetworkInspector(driver, browsingContextIds) | ||
await instance.init() | ||
return instance | ||
} | ||
|
||
module.exports = getNetworkInspectorInstance |
Oops, something went wrong.