abortOnce
Overview
Aborts one matching request. 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.abortOnce()
Classic/CDP: mock.abortOnce(errorReason)
Parameters and result
BiDi takes no arguments. Classic/CDP requires errorReason.
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.abortOnce: block one BiDi request", async ({ browser }) => {
await browser.url("/");
await browser.sessionSubscribe({
events: ["network.beforeRequestSent", "network.responseStarted"],
});
const mock = await browser.mock("/api/items");
try {
mock.abortOnce();
const failed = await browser.execute(async () => {
try {
await fetch("/api/items", { cache: "no-store" });
return false;
} catch {
return true;
}
});
assert.equal(failed, true);
const status = await browser.execute(
async () => (await fetch("/api/items", { cache: "no-store" })).status,
);
assert.equal(status, 200);
} finally {
await mock.restore();
}
});
Related commands
References
This page draws on the original WebdriverIO article.