Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js][bidi]: implement bidi setCacheBehavior command #15136

Merged
merged 12 commits into from
Feb 5, 2025
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] },
)
Loading