Skip to content

Commit

Permalink
[js][bidi]: implement bidi setCacheBehavior command (#15136)
Browse files Browse the repository at this point in the history
* implement js bidi command `setCacheBehavior`

* add bidi tests for `setCacheBehavior`

* updated tests

* add input validation for context IDs

* use enum like structure (CacheBehavior)

* use named import in other network test files

* fix lib/network.js

---------

Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com>
Co-authored-by: Puja Jagani <puja.jagani93@gmail.com>
  • Loading branch information
3 people authored Feb 5, 2025
1 parent 73607ee commit 552c80c
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
41 changes: 40 additions & 1 deletion javascript/node/selenium-webdriver/bidi/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -355,6 +360,40 @@ 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<string>} [contexts] - Optional array of browsing context IDs
* @returns {Promise<void>} 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 (!Object.values(CacheBehavior).includes(behavior)) {
throw new Error(`Cache behavior must be either "${CacheBehavior.DEFAULT}" or "${CacheBehavior.BYPASS}"`)
}

const command = {
method: 'network.setCacheBehavior',
params: {
cacheBehavior: behavior,
},
}

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
}

await this.bidi.send(command)
}

/**
* Unsubscribes from network events for all browsing contexts.
* @returns {Promise<void>} A promise that resolves when the network connection is closed.
Expand Down Expand Up @@ -389,4 +428,4 @@ async function getNetworkInstance(driver, browsingContextIds = null) {
return instance
}

module.exports = getNetworkInstance
module.exports = { Network: getNetworkInstance, CacheBehavior }
4 changes: 2 additions & 2 deletions javascript/node/selenium-webdriver/lib/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -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))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
49 changes: 48 additions & 1 deletion javascript/node/selenium-webdriver/test/bidi/network_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
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')

suite(
Expand Down Expand Up @@ -212,6 +213,52 @@ 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',
})
const contextId = browsingContext.id
await network.setCacheBehavior(CacheBehavior.BYPASS, [contextId])
})

it('can set cache behavior to default for a context', async function () {
await driver.get(Pages.emptyPage)
const browsingContext = await BrowsingContext(driver, {
type: 'tab',
})
const contextId = browsingContext.id
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(CacheBehavior.DEFAULT)
await network.setCacheBehavior(CacheBehavior.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"/,
)
})

it('throws error for invalid context id types', async function () {
await driver.get(Pages.emptyPage)
await assert.rejects(
async () => await network.setCacheBehavior(CacheBehavior.BYPASS, ''),
/Contexts must be an array of non-empty strings/,
)
await assert.rejects(
async () => await network.setCacheBehavior(CacheBehavior.BYPASS, ['', ' ']),
/Contexts must be an array of non-empty strings/,
)
})
})
},
{ browsers: [Browser.FIREFOX, Browser.CHROME, Browser.EDGE] },
)

0 comments on commit 552c80c

Please sign in to comment.