SeleniumHQ/selenium · error · Error
Contexts must be an array of non-empty strings
Error message
Contexts must be an array of non-empty strings
What it means
Thrown by Network.setCacheBehavior() when the optional contexts argument is non-null but is not a non-empty array of non-empty trimmed strings. The client verifies Array.isArray, length > 0, and that every element is a string whose trim() is not empty, before attaching the contexts to the network.setCacheBehavior command. This guards against empty context IDs that the remote end would reject.
Source
Thrown at javascript/selenium-webdriver/bidi/network.js:389
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.
*/
async close() {
if (
this._browsingContextIds !== null &&
this._browsingContextIds !== undefined &&
this._browsingContextIds.length > 0
) {
await this.bidi.unsubscribe(View on GitHub (pinned to aa36b38e69)
Solutions
- Pass an array of one or more non-empty string context ids, e.g. setCacheBehavior('bypass', ['ctx1','ctx2']).
- Filter out empty/blank ids before calling: contexts.filter(c => typeof c === 'string' && c.trim() !== '').
- Pass null (the default) to apply the cache behavior globally, avoiding the contexts check.
Example fix
// before
await network.setCacheBehavior('bypass', currentContextId) // string
// after
await network.setCacheBehavior('bypass', [currentContextId]) // array of non-empty strings Defensive patterns
Strategy: validation
Validate before calling
function assertContexts(contexts) {
if (contexts === null) return null
if (!Array.isArray(contexts) || contexts.length === 0 ||
contexts.some((c) => typeof c !== 'string' || c.trim() === '')) {
throw new Error('contexts must be a non-empty array of non-empty strings')
}
return contexts
} Type guard
function isNonEmptyStringArray(v) {
return Array.isArray(v) && v.length > 0 &&
v.every((c) => typeof c === 'string' && c.trim() !== '')
} Prevention
- Pass null to apply cache behavior globally when per-context is unneeded.
- Filter context arrays to remove empty/blank ids before calling.
- Always wrap a single context id in an array.
When it happens
Trigger: Calling setCacheBehavior('bypass', 'ctx1') (string not array), setCacheBehavior('bypass', []), setCacheBehavior('bypass', ['']), setCacheBehavior('bypass', [' ']), or setCacheBehavior('bypass', [123]).
Common situations: Passing a single context id string instead of an array; reusing a variable that was filtered to empty; including whitespace-only or null context ids copied from a stale context list.
Related errors
- Cache behavior must be either "${CacheBehavior.DEFAULT}" or
- Params must be an instance of AddInterceptParameters. Receiv
- Params must be an instance of ContinueRequestParameters. Rec
- Params must be an instance of ContinueResponseParameters. Re
- Params must be an instance of ProvideResponseParameters. Rec
AI-assisted analysis of SeleniumHQ/selenium@aa36b38e69 (2026-08-14).
Data as JSON: /api/errors/7ff3175c19f88a48.
Report an issue: GitHub.