Skip to content

Commit

Permalink
[js] Close BiDi websocket connection (#14507)
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani authored Oct 4, 2024
1 parent 798cf1c commit 27ad7f4
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,13 @@ class WebDriver {
// If the websocket connection is not closed,
// and we are running CDP sessions against the Selenium Grid,
// the node process never exits since the websocket connection is open until the Grid is shutdown.
if (this._wsConnection !== undefined) {
this._wsConnection.close()
if (this._cdpWsConnection !== undefined) {
this._cdpWsConnection.close()
}

// Close the BiDi websocket connection
if (this._bidiConnection !== undefined) {
this._bidiConnection.close()
}
})
}
Expand Down Expand Up @@ -1241,18 +1246,18 @@ class WebDriver {
this._wsUrl = await this.getWsUrl(debuggerUrl, target, caps)
return new Promise((resolve, reject) => {
try {
this._wsConnection = new WebSocket(this._wsUrl.replace('localhost', '127.0.0.1'))
this._cdpConnection = new cdp.CdpConnection(this._wsConnection)
this._cdpWsConnection = new WebSocket(this._wsUrl.replace('localhost', '127.0.0.1'))
this._cdpConnection = new cdp.CdpConnection(this._cdpWsConnection)
} catch (err) {
reject(err)
return
}

this._wsConnection.on('open', async () => {
this._cdpWsConnection.on('open', async () => {
await this.getCdpTargets()
})

this._wsConnection.on('message', async (message) => {
this._cdpWsConnection.on('message', async (message) => {
const params = JSON.parse(message)
if (params.result) {
if (params.result.targetInfos) {
Expand All @@ -1273,7 +1278,7 @@ class WebDriver {
}
})

this._wsConnection.on('error', (error) => {
this._cdpWsConnection.on('error', (error) => {
reject(error)
})
})
Expand All @@ -1288,9 +1293,12 @@ class WebDriver {
* @returns {BIDI}
*/
async getBidi() {
const caps = await this.getCapabilities()
let WebSocketUrl = caps['map_'].get('webSocketUrl')
return new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1'))
if (this._bidiConnection === undefined) {
const caps = await this.getCapabilities()
let WebSocketUrl = caps['map_'].get('webSocketUrl')
this._bidiConnection = new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1'))
}
return this._bidiConnection
}

/**
Expand Down Expand Up @@ -1338,7 +1346,7 @@ class WebDriver {
* @param connection CDP Connection
*/
async register(username, password, connection) {
this._wsConnection.on('message', (message) => {
this._cdpWsConnection.on('message', (message) => {
const params = JSON.parse(message)

if (params.method === 'Fetch.authRequired') {
Expand Down Expand Up @@ -1383,7 +1391,7 @@ class WebDriver {
* @param callback callback called when we intercept requests.
*/
async onIntercept(connection, httpResponse, callback) {
this._wsConnection.on('message', (message) => {
this._cdpWsConnection.on('message', (message) => {
const params = JSON.parse(message)
if (params.method === 'Fetch.requestPaused') {
const requestPausedParams = params['params']
Expand Down Expand Up @@ -1420,7 +1428,7 @@ class WebDriver {
* @returns {Promise<void>}
*/
async onLogEvent(connection, callback) {
this._wsConnection.on('message', (message) => {
this._cdpWsConnection.on('message', (message) => {
const params = JSON.parse(message)
if (params.method === 'Runtime.consoleAPICalled') {
const consoleEventParams = params['params']
Expand Down Expand Up @@ -1457,7 +1465,7 @@ class WebDriver {
async onLogException(connection, callback) {
await connection.execute('Runtime.enable', {}, null)

this._wsConnection.on('message', (message) => {
this._cdpWsConnection.on('message', (message) => {
const params = JSON.parse(message)

if (params.method === 'Runtime.exceptionThrown') {
Expand Down Expand Up @@ -1510,7 +1518,7 @@ class WebDriver {
null,
)

this._wsConnection.on('message', async (message) => {
this._cdpWsConnection.on('message', async (message) => {
const params = JSON.parse(message)
if (params.method === 'Runtime.bindingCalled') {
let payload = JSON.parse(params['params']['payload'])
Expand Down

0 comments on commit 27ad7f4

Please sign in to comment.