diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 6317a4fa05dba..cb60563695a06 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -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() } }) } @@ -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) { @@ -1273,7 +1278,7 @@ class WebDriver { } }) - this._wsConnection.on('error', (error) => { + this._cdpWsConnection.on('error', (error) => { reject(error) }) }) @@ -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 } /** @@ -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') { @@ -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'] @@ -1420,7 +1428,7 @@ class WebDriver { * @returns {Promise} */ 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'] @@ -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') { @@ -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'])