page.route mocks or patches network requests in Playwright
Answer: Playwright can mock an API by fulfilling a page.route handler with a custom response. No requests to that API will be made. You can also perform the request, then fulfill it with a modified response, rather than mocking it entirely. page.routeFromHAR() serves matching responses from a recorded HAR file.
Client API
The page client implements route (URL, handler, optional times) and routeFromHAR.
See also
Claims
- id: C1 claim: A mocked route can return a custom response instead. source: S1 evidence: "and will return a custom response instead."
- id: C2 claim: A fully mocked API is not called. source: S1 evidence: "No requests to the API will be made."
- id: C3 claim: routeFromHAR serves responses from a HAR file. source: S1 evidence: "page.routeFromHAR()"
- id: C4 claim: The page client implements a route method. source: S2 evidence: "async route(url: URLMatch, handler: RouteHandlerCallback"
Sources
- [S1] Mock APIs. Playwright docs. https://playwright.dev/docs/mock . License: Apache-2.0. Accessed 2026-11-16.
- [S2] Page.route (playwright-core client). microsoft/playwright (GitHub). https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/client/page.ts . License: Apache-2.0. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Playwright docs, Mock APIs, Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation; microsoft/playwright (GitHub), Page.route (playwright-core client), Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Playwright docs, Mock APIs, Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation; microsoft/playwright (GitHub), Page.route (playwright-core client), Apache-2.0 (https://raw.githubusercontent.com/microsoft/playwright/main/LICENSE). Copyright (c) Microsoft Corporation.
Cite this
iinventedlight. 2026. "page.route mocks or patches network requests in Playwright." iinventedlight Library, iil:note:cxt9rmtn, version of 2026-11-16. https://iinventedlight.com/library/notes/playwright-route-mocking/. License: CC-BY-4.0.
@misc{iil_note_cxt9rmtn,
author = {{iinventedlight}},
title = {{page.route mocks or patches network requests in Playwright}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/playwright-route-mocking/},
note = {Permanent ID iil:note:cxt9rmtn. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/playwright-route-mocking.md.