From 689e9ec6cfcce08146e9285be9a36119bcb40ebe Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 13 Aug 2024 15:58:20 +0530 Subject: [PATCH 1/3] [bidi][js] Allow passing in uri for authentication handlers --- .../node/selenium-webdriver/lib/network.js | 50 +++++++++++-------- .../test/lib/webdriver_network_test.js | 43 +++++++++++++++- 2 files changed, 71 insertions(+), 22 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/network.js b/javascript/node/selenium-webdriver/lib/network.js index 5ab24c4b0da9f..96b1ba50f9d8f 100644 --- a/javascript/node/selenium-webdriver/lib/network.js +++ b/javascript/node/selenium-webdriver/lib/network.js @@ -20,9 +20,10 @@ const { InterceptPhase } = require('../bidi/interceptPhase') const { AddInterceptParameters } = require('../bidi/addInterceptParameters') class Network { + #callbackId = 0 #driver #network - #callBackInterceptIdMap = new Map() + #authHandlers = new Map() constructor(driver) { this.#driver = driver @@ -39,39 +40,46 @@ class Network { return } this.#network = await network(this.#driver) - } - async addAuthenticationHandler(username, password) { - await this.#init() + await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED)) - const interceptId = await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED)) + await this.#network.authRequired(async (event) => { + const requestId = event.request.request + const uri = event.request.url + const credentials = this.getAuthCredentials(uri) + if (credentials !== null) { + await this.#network.continueWithAuth(requestId, credentials.username, credentials.password) + return + } - const id = await this.#network.authRequired(async (event) => { - await this.#network.continueWithAuth(event.request.request, username, password) + await this.#network.continueWithAuthNoCredentials(requestId) }) - - this.#callBackInterceptIdMap.set(id, interceptId) - return id } - async removeAuthenticationHandler(id) { + getAuthCredentials(uri) { + for (let [, value] of this.#authHandlers) { + if (uri.match(value.uri)) { + return value + } + } + return null + } + async addAuthenticationHandler(username, password, uri = '//') { await this.#init() - const interceptId = this.#callBackInterceptIdMap.get(id) + const id = this.#callbackId++ - await this.#network.removeIntercept(interceptId) - await this.#network.removeCallback(id) + this.#authHandlers.set(id, { username, password, uri }) + return id + } - this.#callBackInterceptIdMap.delete(id) + async removeAuthenticationHandler(id) { + await this.#init() + this.#authHandlers.delete(id) } async clearAuthenticationHandlers() { - for (const [key, value] of this.#callBackInterceptIdMap.entries()) { - await this.#network.removeIntercept(value) - await this.#network.removeCallback(key) - } - - this.#callBackInterceptIdMap.clear() + this.#authHandlers.clear() } } diff --git a/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js b/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js index 538fefa87916d..37d6c523e483c 100644 --- a/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js +++ b/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js @@ -45,6 +45,35 @@ suite( assert.equal(source.includes('Access granted'), true) }) + it('can add authentication handler with filter', async function () { + await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth') + await driver.get(Pages.basicAuth) + + await driver.wait(until.elementLocated(By.css('pre'))) + let source = await driver.getPageSource() + assert.equal(source.includes('Access granted'), true) + }) + + it('can add multiple authentication handlers with filter', async function () { + await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth') + await driver.network().addAuthenticationHandler('test', 'test', 'test') + await driver.get(Pages.basicAuth) + + await driver.wait(until.elementLocated(By.css('pre'))) + let source = await driver.getPageSource() + assert.equal(source.includes('Access granted'), true) + }) + + it('can add multiple authentication handlers with the same filter', async function () { + await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth') + await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth') + await driver.get(Pages.basicAuth) + + await driver.wait(until.elementLocated(By.css('pre'))) + let source = await driver.getPageSource() + assert.equal(source.includes('Access granted'), true) + }) + it('can remove authentication handler', async function () { const id = await driver.network().addAuthenticationHandler('genie', 'bottle') @@ -59,8 +88,20 @@ suite( } }) + it('can remove authentication handler', async function () { + await driver.network().removeAuthenticationHandler(10) + + try { + await driver.get(Pages.basicAuth) + await driver.wait(until.elementLocated(By.css('pre'))) + assert.fail('Page should not be loaded') + } catch (e) { + assert.strictEqual(e.name, 'UnexpectedAlertOpenError') + } + }) + it('can clear authentication handlers', async function () { - await driver.network().addAuthenticationHandler('genie', 'bottle') + await driver.network().addAuthenticationHandler('genie', 'bottle', 'basicAuth') await driver.network().addAuthenticationHandler('bottle', 'genie') From 843e3a9cd3e9c9d4531e67bfc81bc7f46590d710 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 20 Aug 2024 13:31:28 +0530 Subject: [PATCH 2/3] Fixing the test name --- .../node/selenium-webdriver/test/lib/webdriver_network_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js b/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js index 37d6c523e483c..ab6df8f90494c 100644 --- a/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js +++ b/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js @@ -88,7 +88,7 @@ suite( } }) - it('can remove authentication handler', async function () { + it('can remove authentication handler that does not exist', async function () { await driver.network().removeAuthenticationHandler(10) try { From e7513c9468f96d276ca689890f295b0c16c9bfb8 Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Tue, 20 Aug 2024 13:45:16 +0530 Subject: [PATCH 3/3] Throw error for a missing callback --- javascript/node/selenium-webdriver/lib/network.js | 7 ++++++- .../test/lib/webdriver_network_test.js | 11 ++++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/network.js b/javascript/node/selenium-webdriver/lib/network.js index 96b1ba50f9d8f..1c55264653484 100644 --- a/javascript/node/selenium-webdriver/lib/network.js +++ b/javascript/node/selenium-webdriver/lib/network.js @@ -75,7 +75,12 @@ class Network { async removeAuthenticationHandler(id) { await this.#init() - this.#authHandlers.delete(id) + + if (this.#authHandlers.has(id)) { + this.#authHandlers.delete(id) + } else { + throw Error(`Callback with id ${id} not found`) + } } async clearAuthenticationHandlers() { diff --git a/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js b/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js index ab6df8f90494c..ff1ce496bf038 100644 --- a/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js +++ b/javascript/node/selenium-webdriver/test/lib/webdriver_network_test.js @@ -88,15 +88,12 @@ suite( } }) - it('can remove authentication handler that does not exist', async function () { - await driver.network().removeAuthenticationHandler(10) - + it('throws an error when remove authentication handler that does not exist', async function () { try { - await driver.get(Pages.basicAuth) - await driver.wait(until.elementLocated(By.css('pre'))) - assert.fail('Page should not be loaded') + await driver.network().removeAuthenticationHandler(10) + assert.fail('Expected error not thrown. Non-existent handler cannot be removed') } catch (e) { - assert.strictEqual(e.name, 'UnexpectedAlertOpenError') + assert.strictEqual(e.message, 'Callback with id 10 not found') } })