Playwright · Reliable end-to-end tests
How does network interception work with page.route?
Short answer
page.route registers a handler that can continue, fulfill or abort matching requests. The most recently registered matching route runs first, and fallback passes control to earlier handlers.
Route handlers let a test sit between the page and the network. A handler matches by URL pattern, then decides whether to let the request go to the server, return a synthetic response, or fail it. This is how tests remove dependence on slow or unstable third parties.
await page.route("**/api/recommendations", async (route) => {
await route.fulfill({
status: 200,
contentType: "application/json",
body: JSON.stringify({
items: [{ id: 1, name: "Lamp" }],
}),
});
});
await page.goto("/cart");
await expect(page.getByText("Lamp")).toBeVisible();The handler decisions
route.continue()sends the request to the network, optionally with overridesroute.fulfill()returns a synthetic response without hitting the serverroute.abort()fails the request, useful for offline and error statesroute.fallback()defers to the next matching handlerroute.request()exposes the URL, method, headers and post body
Order matters. The most recently registered matching route is tried first. That makes it easy to install a broad default and then override one endpoint in a specific test. Use fallback when the newer handler only wants to modify some requests and let the rest continue to the earlier behaviour.
await page.route("**/api/**", async (route) => {
if (route.request().url().includes("/health")) {
await route.fallback();
return;
}
await route.continue();
});page.route by default. If the app caches through a service worker, launch the context with serviceWorkers: "block" or move the interception to context.route after understanding the app's caching.Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between continue, fulfill and abort?
- 2Which matching route runs first?
- 3When would you use fallback?
- 4Why might a service worker bypass page.route?
Say it in the interview
The route API installs a handler that can continue a request, fulfill it with a fake response, or abort it. The most recently registered matching handler runs first, and fallback lets it defer to an earlier one. I register mocks before navigation so the first request is covered. If the app uses a service worker, I remember that the route handler may not see its requests and adjust the context configuration.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Playwright mock interview and get scored feedback on depth, clarity and confidence.