Skip to main content

restore

Overview​

Removes the network interceptor, clears history and disables overrides. Subsequent requests proceed without this mock. This is not browser.restore, which restores emulation.

Await restore() before sending the next request or ending the test. In BiDi, a restored mock cannot be reused: create another with browser.mock. To retain the interceptor, use reset.

Usage​

await mock.restore()

Parameters and result​

Takes no arguments. Returns a Promise resolving to the same mock in BiDi and no useful value in Classic/CDP. Use it for cleanup in finally, including when an assertion fails.

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.restore: remove interception before the next request", async ({ browser }) => {
await browser.url("/");
await browser.sessionSubscribe({
events: ["network.beforeRequestSent", "network.responseStarted"],
});
const mock = await browser.mock("/api/items");
try {
mock.respond({ source: "mocked" });
const body = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(body.source, "mocked");
} finally {
await mock.restore();
}
const body = await browser.execute(async () =>
(await fetch("/api/items", { cache: "no-store" })).json(),
);
assert.equal(body.source, "original");
assert.equal(mock.calls.length, 0);
assert.throws(() => mock.respond({ source: "unused" }), /restored mock/);
});

References​

This page draws on the original WebdriverIO article.