Intercept, inspect, and mock network requests in Playwright tests

domain: playwright.dev · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Register a route handler before navigation: `await page.route('**/api/users', route => route.fulfill({ status: 200, body: JSON.stringify([{ id: 1, name: 'Alice' }]) }))` — the glob pattern matches the URL and `fulfill` returns the mock response.
  2. To modify a real response rather than replace it, use `route.fetch()` to make the actual request, mutate the response body, and call `route.fulfill({ response, body: modifiedBody })`.
  3. Abort specific requests (e.g., analytics) with `route.abort()` to speed up tests and remove noise.
  4. Inspect outgoing requests without blocking them using `page.on('request', req => console.log(req.url()))` and `page.on('response', res => ...)` event listeners.
  5. Remove a route when no longer needed with `page.unroute('**/api/users')` to avoid interfering with subsequent navigation in the same context.

Known gotchas

Related routes

Capture and analyze network requests during a Playwright test using request and response events
playwright.dev · 5 steps · unrated
Intercept and mock WebSocket messages in Playwright using page.routeWebSocket()
playwright.dev · 5 steps · unrated
decide between raw cdp fetch-domain request interception and playwright's page.route() for mocking network requests
playwright.dev · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans