From a37a7279ea335f0c2acafd7f8283ca7a10abb613 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 17 Sep 2024 12:11:47 +0530 Subject: [PATCH 1/4] [js] Close BiDi websocket connection --- javascript/node/selenium-webdriver/lib/webdriver.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 6317a4fa05dba..8cc90468cdc6d 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -792,6 +792,10 @@ class WebDriver { if (this._wsConnection !== undefined) { this._wsConnection.close() } + + if (this._bidi !== undefined) { + this._bidi.close() + } }) } @@ -1288,9 +1292,12 @@ class WebDriver { * @returns {BIDI} */ async getBidi() { - const caps = await this.getCapabilities() + if (this._bidi === undefined) { + const caps = await this.getCapabilities() let WebSocketUrl = caps['map_'].get('webSocketUrl') - return new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1')) + this._bidi = new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1')) + } + return this._bidi; } /** From 61379c9c6aa43c41f5e4096e1cdfdfb68fc092f3 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 17 Sep 2024 13:14:22 +0530 Subject: [PATCH 2/4] [js] Fix formatting --- javascript/node/selenium-webdriver/lib/webdriver.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 8cc90468cdc6d..1df59dfc14f27 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -1294,10 +1294,10 @@ class WebDriver { async getBidi() { if (this._bidi === undefined) { const caps = await this.getCapabilities() - let WebSocketUrl = caps['map_'].get('webSocketUrl') - this._bidi = new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1')) - } - return this._bidi; + let WebSocketUrl = caps['map_'].get('webSocketUrl') + this._bidi = new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1')) + } + return this._bidi } /** From 13b86c26b6a1c0c810069a4f08693f1b443fb8aa Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 17 Sep 2024 13:18:25 +0530 Subject: [PATCH 3/4] [js] Rename websocket connection variables --- .../node/selenium-webdriver/lib/webdriver.js | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 1df59dfc14f27..560d60676403e 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -789,12 +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._cdpConnection !== undefined) { + this._cdpConnection.close() } - if (this._bidi !== undefined) { - this._bidi.close() + // Close the BiDi websocket connection + if (this._bidiConnection !== undefined) { + this._bidiConnection.close() } }) } @@ -1245,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._cdpConnection = new WebSocket(this._wsUrl.replace('localhost', '127.0.0.1')) + this._cdpConnection = new cdp.CdpConnection(this._cdpConnection) } catch (err) { reject(err) return } - this._wsConnection.on('open', async () => { + this._cdpConnection.on('open', async () => { await this.getCdpTargets() }) - this._wsConnection.on('message', async (message) => { + this._cdpConnection.on('message', async (message) => { const params = JSON.parse(message) if (params.result) { if (params.result.targetInfos) { @@ -1277,7 +1278,7 @@ class WebDriver { } }) - this._wsConnection.on('error', (error) => { + this._cdpConnection.on('error', (error) => { reject(error) }) }) @@ -1292,12 +1293,12 @@ class WebDriver { * @returns {BIDI} */ async getBidi() { - if (this._bidi === undefined) { + if (this._bidiConnection === undefined) { const caps = await this.getCapabilities() let WebSocketUrl = caps['map_'].get('webSocketUrl') - this._bidi = new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1')) + this._bidiConnection = new BIDI(WebSocketUrl.replace('localhost', '127.0.0.1')) } - return this._bidi + return this._bidiConnection } /** @@ -1345,7 +1346,7 @@ class WebDriver { * @param connection CDP Connection */ async register(username, password, connection) { - this._wsConnection.on('message', (message) => { + this._cdpConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Fetch.authRequired') { @@ -1390,7 +1391,7 @@ class WebDriver { * @param callback callback called when we intercept requests. */ async onIntercept(connection, httpResponse, callback) { - this._wsConnection.on('message', (message) => { + this._cdpConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Fetch.requestPaused') { const requestPausedParams = params['params'] @@ -1427,7 +1428,7 @@ class WebDriver { * @returns {Promise} */ async onLogEvent(connection, callback) { - this._wsConnection.on('message', (message) => { + this._cdpConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Runtime.consoleAPICalled') { const consoleEventParams = params['params'] @@ -1464,7 +1465,7 @@ class WebDriver { async onLogException(connection, callback) { await connection.execute('Runtime.enable', {}, null) - this._wsConnection.on('message', (message) => { + this._cdpConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Runtime.exceptionThrown') { @@ -1517,7 +1518,7 @@ class WebDriver { null, ) - this._wsConnection.on('message', async (message) => { + this._cdpConnection.on('message', async (message) => { const params = JSON.parse(message) if (params.method === 'Runtime.bindingCalled') { let payload = JSON.parse(params['params']['payload']) From dfa8f8f96ce4e8b52525e4b80630e3264a2f578f Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 17 Sep 2024 15:26:03 +0530 Subject: [PATCH 4/4] [js] Update variable name --- .../node/selenium-webdriver/lib/webdriver.js | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/webdriver.js b/javascript/node/selenium-webdriver/lib/webdriver.js index 560d60676403e..cb60563695a06 100644 --- a/javascript/node/selenium-webdriver/lib/webdriver.js +++ b/javascript/node/selenium-webdriver/lib/webdriver.js @@ -789,8 +789,8 @@ 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._cdpConnection !== undefined) { - this._cdpConnection.close() + if (this._cdpWsConnection !== undefined) { + this._cdpWsConnection.close() } // Close the BiDi websocket connection @@ -1246,18 +1246,18 @@ class WebDriver { this._wsUrl = await this.getWsUrl(debuggerUrl, target, caps) return new Promise((resolve, reject) => { try { - this._cdpConnection = new WebSocket(this._wsUrl.replace('localhost', '127.0.0.1')) - this._cdpConnection = new cdp.CdpConnection(this._cdpConnection) + 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._cdpConnection.on('open', async () => { + this._cdpWsConnection.on('open', async () => { await this.getCdpTargets() }) - this._cdpConnection.on('message', async (message) => { + this._cdpWsConnection.on('message', async (message) => { const params = JSON.parse(message) if (params.result) { if (params.result.targetInfos) { @@ -1278,7 +1278,7 @@ class WebDriver { } }) - this._cdpConnection.on('error', (error) => { + this._cdpWsConnection.on('error', (error) => { reject(error) }) }) @@ -1346,7 +1346,7 @@ class WebDriver { * @param connection CDP Connection */ async register(username, password, connection) { - this._cdpConnection.on('message', (message) => { + this._cdpWsConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Fetch.authRequired') { @@ -1391,7 +1391,7 @@ class WebDriver { * @param callback callback called when we intercept requests. */ async onIntercept(connection, httpResponse, callback) { - this._cdpConnection.on('message', (message) => { + this._cdpWsConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Fetch.requestPaused') { const requestPausedParams = params['params'] @@ -1428,7 +1428,7 @@ class WebDriver { * @returns {Promise} */ async onLogEvent(connection, callback) { - this._cdpConnection.on('message', (message) => { + this._cdpWsConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Runtime.consoleAPICalled') { const consoleEventParams = params['params'] @@ -1465,7 +1465,7 @@ class WebDriver { async onLogException(connection, callback) { await connection.execute('Runtime.enable', {}, null) - this._cdpConnection.on('message', (message) => { + this._cdpWsConnection.on('message', (message) => { const params = JSON.parse(message) if (params.method === 'Runtime.exceptionThrown') { @@ -1518,7 +1518,7 @@ class WebDriver { null, ) - this._cdpConnection.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'])