Skip to content
Merged
34 changes: 34 additions & 0 deletions javascript/node/selenium-webdriver/bidi/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,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 (!['default', 'bypass'].includes(behavior)) {
throw new Error('Cache behavior must be either "default" or "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
47 changes: 47 additions & 0 deletions javascript/node/selenium-webdriver/test/bidi/network_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
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('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('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')
})

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('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] },
)