From 894b844f973e03c85dab124c2c9c92a6dc5bb5fe Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 23 Jan 2025 12:59:53 +0530 Subject: [PATCH 1/7] implement js bidi command `setCacheBehavior` --- .../node/selenium-webdriver/bidi/network.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/javascript/node/selenium-webdriver/bidi/network.js b/javascript/node/selenium-webdriver/bidi/network.js index af80fcf145580..9284fa2b1bcc7 100644 --- a/javascript/node/selenium-webdriver/bidi/network.js +++ b/javascript/node/selenium-webdriver/bidi/network.js @@ -355,6 +355,33 @@ class Network { await this.bidi.send(command) } + /** + * Sets the cache behavior for network requests. + * + * @param {string} behavior - The cache behavior ("default" or "bypass") + * @param {Array} [contexts] - Optional array of browsing context IDs + * @returns {Promise} A promise that resolves when the cache behavior is set + * @throws {Error} If behavior is invalid or context IDs are invalid + */ + async setCacheBehavior(behavior, contexts = null) { + if (behavior !== 'default' && behavior !== 'bypass') { + throw new Error('Cache behavior must be either "default" or "bypass"') + } + + const command = { + method: 'network.setCacheBehavior', + params: { + cacheBehavior: behavior, + }, + } + + if (contexts) { + command.params.contexts = contexts + } + + await this.bidi.send(command) + } + /** * Unsubscribes from network events for all browsing contexts. * @returns {Promise} A promise that resolves when the network connection is closed. From 3b6563688fe7a3473a2b899735fc295ec105a945 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 23 Jan 2025 13:01:34 +0530 Subject: [PATCH 2/7] add bidi tests for `setCacheBehavior` --- .../test/bidi/network_test.js | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/javascript/node/selenium-webdriver/test/bidi/network_test.js b/javascript/node/selenium-webdriver/test/bidi/network_test.js index 060eec72d3ce5..b95297cfbb882 100644 --- a/javascript/node/selenium-webdriver/test/bidi/network_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/network_test.js @@ -21,6 +21,7 @@ const assert = require('node:assert') const { Browser } = require('selenium-webdriver') const { Pages, suite, ignore } = require('../../lib/test') const Network = require('selenium-webdriver/bidi/network') +const BrowsingContext = require('selenium-webdriver/bidi/browsingContext') const until = require('selenium-webdriver/lib/until') suite( @@ -212,6 +213,38 @@ suite( assert(onResponseCompleted[0].response.mimeType.includes('text/plain')) }) }) + + describe('setCacheBehavior', function () { + it('can set cache behavior to bypass for a context', async function () { + await driver.get(Pages.emptyPage) + const browsingContext = await BrowsingContext(driver, { + type: 'tab', + }) + await network.setCacheBehavior('bypass', [browsingContext]) + }) + + it('can set cache behavior to default for a context', async function () { + await driver.get(Pages.emptyPage) + const browsingContext = await BrowsingContext(driver, { + type: 'tab', + }) + await network.setCacheBehavior('default', [browsingContext]) + }) + + it('can set cache behavior to default/bypass with no context id', async function () { + await driver.get(Pages.emptyPage) + await network.setCacheBehavior('default') + await network.setCacheBehavior('bypass') + }) + + it('throws error for invalid cache behavior', async function () { + await driver.get(Pages.emptyPage) + await assert.rejects( + async () => await network.setCacheBehavior('invalid'), + /Cache behavior must be either "default" or "bypass"/, + ) + }) + }) }, { browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] }, ) From 88eae93d68b82a7b58afc25f0e69a129628447c9 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 23 Jan 2025 14:12:56 +0530 Subject: [PATCH 3/7] updated tests --- javascript/node/selenium-webdriver/bidi/network.js | 2 +- .../node/selenium-webdriver/test/bidi/network_test.js | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/javascript/node/selenium-webdriver/bidi/network.js b/javascript/node/selenium-webdriver/bidi/network.js index 9284fa2b1bcc7..cbd5096039615 100644 --- a/javascript/node/selenium-webdriver/bidi/network.js +++ b/javascript/node/selenium-webdriver/bidi/network.js @@ -364,7 +364,7 @@ class Network { * @throws {Error} If behavior is invalid or context IDs are invalid */ async setCacheBehavior(behavior, contexts = null) { - if (behavior !== 'default' && behavior !== 'bypass') { + if (!['default', 'bypass'].includes(behavior)) { throw new Error('Cache behavior must be either "default" or "bypass"') } diff --git a/javascript/node/selenium-webdriver/test/bidi/network_test.js b/javascript/node/selenium-webdriver/test/bidi/network_test.js index b95297cfbb882..3039b5db193f8 100644 --- a/javascript/node/selenium-webdriver/test/bidi/network_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/network_test.js @@ -220,7 +220,8 @@ suite( const browsingContext = await BrowsingContext(driver, { type: 'tab', }) - await network.setCacheBehavior('bypass', [browsingContext]) + const contextId = browsingContext.id + await network.setCacheBehavior('bypass', [contextId]) }) it('can set cache behavior to default for a context', async function () { @@ -228,7 +229,8 @@ suite( const browsingContext = await BrowsingContext(driver, { type: 'tab', }) - await network.setCacheBehavior('default', [browsingContext]) + const contextId = browsingContext.id + await network.setCacheBehavior('default', [contextId]) }) it('can set cache behavior to default/bypass with no context id', async function () { From 1f06cb3fc1d0d02a4f420ddebfa11b7ee0262ebb Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Thu, 23 Jan 2025 18:38:48 +0530 Subject: [PATCH 4/7] add input validation for context IDs --- javascript/node/selenium-webdriver/bidi/network.js | 9 ++++++++- .../selenium-webdriver/test/bidi/network_test.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/javascript/node/selenium-webdriver/bidi/network.js b/javascript/node/selenium-webdriver/bidi/network.js index cbd5096039615..1cfc11e135cb1 100644 --- a/javascript/node/selenium-webdriver/bidi/network.js +++ b/javascript/node/selenium-webdriver/bidi/network.js @@ -375,7 +375,14 @@ class Network { }, } - if (contexts) { + if (contexts !== null) { + if ( + !Array.isArray(contexts) || + contexts.length === 0 || + contexts.some((c) => typeof c !== 'string' || c.trim() === '') + ) { + throw new Error('Contexts must be an array of non-empty strings') + } command.params.contexts = contexts } diff --git a/javascript/node/selenium-webdriver/test/bidi/network_test.js b/javascript/node/selenium-webdriver/test/bidi/network_test.js index 3039b5db193f8..7b96b9e7ec1f6 100644 --- a/javascript/node/selenium-webdriver/test/bidi/network_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/network_test.js @@ -246,6 +246,18 @@ suite( /Cache behavior must be either "default" or "bypass"/, ) }) + + it('throws error for invalid context id types', async function () { + await driver.get(Pages.emptyPage) + await assert.rejects( + async () => await network.setCacheBehavior('default', ''), + /Contexts must be an array of non-empty strings/, + ) + await assert.rejects( + async () => await network.setCacheBehavior('default', ['', ' ']), + /Contexts must be an array of non-empty strings/, + ) + }) }) }, { browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] }, From c9bd43b13f99d3ebcd9210c33cef53c5b6950d9b Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Tue, 4 Feb 2025 18:26:06 +0530 Subject: [PATCH 5/7] use enum like structure (CacheBehavior) --- javascript/node/selenium-webdriver/bidi/network.js | 11 ++++++++--- .../selenium-webdriver/test/bidi/network_test.js | 14 +++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/javascript/node/selenium-webdriver/bidi/network.js b/javascript/node/selenium-webdriver/bidi/network.js index 1cfc11e135cb1..8e860b33f2bdd 100644 --- a/javascript/node/selenium-webdriver/bidi/network.js +++ b/javascript/node/selenium-webdriver/bidi/network.js @@ -29,6 +29,11 @@ const NetworkEvent = { FETCH_ERROR: 'network.fetchError', } +const CacheBehavior = Object.freeze({ + DEFAULT: 'default', + BYPASS: 'bypass', +}) + /** * Represents all commands and events of Network module. * Described in https://w3c.github.io/webdriver-bidi/#module-network. @@ -364,8 +369,8 @@ class Network { * @throws {Error} If behavior is invalid or context IDs are invalid */ async setCacheBehavior(behavior, contexts = null) { - if (!['default', 'bypass'].includes(behavior)) { - throw new Error('Cache behavior must be either "default" or "bypass"') + if (!Object.values(CacheBehavior).includes(behavior)) { + throw new Error(`Cache behavior must be either "${CacheBehavior.DEFAULT}" or "${CacheBehavior.BYPASS}"`) } const command = { @@ -423,4 +428,4 @@ async function getNetworkInstance(driver, browsingContextIds = null) { return instance } -module.exports = getNetworkInstance +module.exports = { Network: getNetworkInstance, CacheBehavior } diff --git a/javascript/node/selenium-webdriver/test/bidi/network_test.js b/javascript/node/selenium-webdriver/test/bidi/network_test.js index 7b96b9e7ec1f6..5560e29ccd20d 100644 --- a/javascript/node/selenium-webdriver/test/bidi/network_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/network_test.js @@ -20,7 +20,7 @@ const assert = require('node:assert') const { Browser } = require('selenium-webdriver') const { Pages, suite, ignore } = require('../../lib/test') -const Network = require('selenium-webdriver/bidi/network') +const { Network, CacheBehavior } = require('selenium-webdriver/bidi/network') const BrowsingContext = require('selenium-webdriver/bidi/browsingContext') const until = require('selenium-webdriver/lib/until') @@ -221,7 +221,7 @@ suite( type: 'tab', }) const contextId = browsingContext.id - await network.setCacheBehavior('bypass', [contextId]) + await network.setCacheBehavior(CacheBehavior.BYPASS, [contextId]) }) it('can set cache behavior to default for a context', async function () { @@ -230,13 +230,13 @@ suite( type: 'tab', }) const contextId = browsingContext.id - await network.setCacheBehavior('default', [contextId]) + await network.setCacheBehavior(CacheBehavior.DEFAULT, [contextId]) }) it('can set cache behavior to default/bypass with no context id', async function () { await driver.get(Pages.emptyPage) - await network.setCacheBehavior('default') - await network.setCacheBehavior('bypass') + await network.setCacheBehavior(CacheBehavior.DEFAULT) + await network.setCacheBehavior(CacheBehavior.BYPASS) }) it('throws error for invalid cache behavior', async function () { @@ -250,11 +250,11 @@ suite( it('throws error for invalid context id types', async function () { await driver.get(Pages.emptyPage) await assert.rejects( - async () => await network.setCacheBehavior('default', ''), + async () => await network.setCacheBehavior(CacheBehavior.BYPASS, ''), /Contexts must be an array of non-empty strings/, ) await assert.rejects( - async () => await network.setCacheBehavior('default', ['', ' ']), + async () => await network.setCacheBehavior(CacheBehavior.BYPASS, ['', ' ']), /Contexts must be an array of non-empty strings/, ) }) From 50d4e0f88b9d4320604250cbd70760b722145e16 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Tue, 4 Feb 2025 19:55:24 +0530 Subject: [PATCH 6/7] use named import in other network test files --- .../test/bidi/add_intercept_parameters_test.js | 2 +- .../node/selenium-webdriver/test/bidi/network_commands_test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/javascript/node/selenium-webdriver/test/bidi/add_intercept_parameters_test.js b/javascript/node/selenium-webdriver/test/bidi/add_intercept_parameters_test.js index 5e81d6397e718..6426c193d267a 100644 --- a/javascript/node/selenium-webdriver/test/bidi/add_intercept_parameters_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/add_intercept_parameters_test.js @@ -20,7 +20,7 @@ const assert = require('node:assert') const { Browser } = require('selenium-webdriver') const { suite } = require('../../lib/test') -const Network = require('selenium-webdriver/bidi/network') +const { Network } = require('selenium-webdriver/bidi/network') const { AddInterceptParameters } = require('selenium-webdriver/bidi/addInterceptParameters') const { InterceptPhase } = require('selenium-webdriver/bidi/interceptPhase') const { UrlPattern } = require('selenium-webdriver/bidi/urlPattern') diff --git a/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js b/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js index 7c282aa913e37..f3969c7917888 100644 --- a/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js +++ b/javascript/node/selenium-webdriver/test/bidi/network_commands_test.js @@ -20,7 +20,7 @@ const assert = require('node:assert') const { Browser, By } = require('selenium-webdriver') const { Pages, suite } = require('../../lib/test') -const Network = require('selenium-webdriver/bidi/network') +const { Network } = require('selenium-webdriver/bidi/network') const { AddInterceptParameters } = require('selenium-webdriver/bidi/addInterceptParameters') const { InterceptPhase } = require('selenium-webdriver/bidi/interceptPhase') const { until } = require('selenium-webdriver/index') From 711269482cba9a58a8f54f4c9f1cc898ff222f09 Mon Sep 17 00:00:00 2001 From: Navin Chandra Date: Wed, 5 Feb 2025 17:16:29 +0530 Subject: [PATCH 7/7] fix lib/network.js --- javascript/node/selenium-webdriver/lib/network.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/javascript/node/selenium-webdriver/lib/network.js b/javascript/node/selenium-webdriver/lib/network.js index 1c55264653484..cfc5873804d53 100644 --- a/javascript/node/selenium-webdriver/lib/network.js +++ b/javascript/node/selenium-webdriver/lib/network.js @@ -15,7 +15,7 @@ // specific language governing permissions and limitations // under the License. -const network = require('../bidi/network') +const { Network: getNetwork } = require('../bidi/network') const { InterceptPhase } = require('../bidi/interceptPhase') const { AddInterceptParameters } = require('../bidi/addInterceptParameters') @@ -39,7 +39,7 @@ class Network { if (this.#network !== undefined) { return } - this.#network = await network(this.#driver) + this.#network = await getNetwork(this.#driver) await this.#network.addIntercept(new AddInterceptParameters(InterceptPhase.AUTH_REQUIRED))