Skip to main content

abort

Overview​

Aborts matching requests while the rule is active. BiDi and Classic/CDP signatures differ. Do not pass an error-code string in BiDi: it does not select a network error and may change whether the rule is one-shot.

Usage​

BiDi: mock.abort(once = false)

Classic/CDP: mock.abort(errorReason, sticky = true)

Parameters and result​

BiDi: once is a boolean, default false; use abortOnce for one request. Classic/CDP: errorReason selects the error; sticky defaults to true to retain the rule.

Classic/CDP error codes: Failed, Aborted, TimedOut, AccessDenied, ConnectionClosed, ConnectionReset, ConnectionRefused, ConnectionAborted, ConnectionFailed, NameNotResolved, InternetDisconnected, AddressUnreachable, BlockedByClient, BlockedByResponse.

BiDi synchronously returns the same mock; Classic/CDP has no useful return value. BiDi rules share a queue with request and redirect. A persistent rule replaces the queue; a one-shot rule retains other one-shot rules and removes the persistent rule. CDP appends rules: an earlier persistent rule can prevent a later rule from being used.

Example​

The example uses Chrome with WebDriver BiDi. Point baseUrl to a test HTTP server: / serves an HTML page, and /api/items returns JSON with source: "original" and status 200. The explicit event subscription supports new sessions in the same worker in WDIO 9.6.2.

const assert = require("node:assert/strict");

it("mock.abort: block matching BiDi requests", async ({ browser }) => {
await browser.url("/");
await browser.sessionSubscribe({
events: ["network.beforeRequestSent", "network.responseStarted"],
});
const mock = await browser.mock("/api/items");
try {
mock.abort();
for (let i = 0; i < 2; i++) {
const failed = await browser.execute(async () => {
try {
await fetch("/api/items", { cache: "no-store" });
return false;
} catch {
return true;
}
});
assert.equal(failed, true);
}
} finally {
await mock.restore();
}
});

References​

This page draws on the original WebdriverIO article.