Playwright Route Intercept Not Working Fix
In this tutorial, you'll learn about Playwright Route Intercept Not Working Fix. We cover key concepts, practical examples, and best practices.
Your page.route() doesn't intercept the request — the real API is called instead of your mock handler.
The Problem
// WRONG — route registered after the request was made
test('intercept API request', async ({ page }) => {
await page.goto('https://example.com');
// Page already loaded — API call happened
await page.route('**/api/data', (route) => {
route.fulfill({ body: 'mocked' });
});
// Too late — the request already fired
});
The route handler is registered after the page already sent the API request. By the time page.route() is called, the response was already received.
Step-by-Step Fix
1. Register route before navigation
// RIGHT — register route before page loads
import { test, expect } from '@playwright/test';
test('intercept API data', async ({ page }) => {
// Register route handler first
await page.route('**/api/user', (route) => {
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ name: 'Alice' }),
});
});
await page.goto('https://example.com/profile');
await expect(page.locator('.user-name')).toHaveText('Alice');
});
2. Modify responses
// RIGHT — modify real API responses
test('modify API response', async ({ page }) => {
await page.route('**/api/users*', async (route) => {
const response = await route.fetch();
const body = await response.json();
body.push({ id: 999, name: 'Mock User' });
route.fulfill({ response, body: JSON.stringify(body) });
});
await page.goto('https://example.com/users');
await expect(page.locator('text=Mock User')).toBeVisible();
});
3. Abort requests
// RIGHT — block specific requests
test('block analytics', async ({ page }) => {
await page.route('**/analytics*', (route) => route.abort());
await page.goto('https://example.com');
// Analytics requests are blocked
});
4. Conditional routing
// RIGHT — conditional intercept
test('conditional route handling', async ({ page }) => {
await page.route('**/api/**', (route) => {
if (route.request().method() === 'POST') {
route.fulfill({
status: 201,
body: JSON.stringify({ success: true }),
});
} else {
route.continue();
}
});
await page.goto('https://example.com');
});
Expected output:
✓ intercept API data (1.0s)
✓ modify API response (1.2s)
✓ block analytics (0.8s)
Prevention Tips
- Register routes before page navigation
- Use
route.fulfill()to return mock responses - Use
route.fetch()to get real response then modify - Use
route.abort()to block requests - Use
route.continue()to let requests pass through
Common Mistakes with intercept route
- Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro