Chapter 57: Network interception and mocking
Automation learning lab
Playwright Automation
Self Study Guide
Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.
Network interception and mocking
page.route registers a handler for matching requests. route.fulfill supplies a controlled response, continue sends the real request and abort cancels it. Mocking makes selected UI scenarios deterministic but does not replace end-to-end backend coverage.
How it works
Step-by-step walkthrough
- Register a route
- Navigate to the mocked endpoint
- Assert its response text
Example
import { test, expect } from '@playwright/test';
test('chapter 57 practice', async ({ page }) => {
await page.route('https://example.com/api/status', route => route.fulfill({
status: 200, contentType: 'text/plain', body: 'Mock ready'
}));
await page.goto('https://example.com/api/status');
await expect(page.locator('body')).toContainText('Mock ready');
});Register routes before the request and keep mocks realistic.
✎ Your practice task
Change the mocked response status to 503 and inspect the response.
Knowledge check
Which method supplies a mocked response?
🌐 Continue learning
60-chapter learning path
Start at chapter 1, or choose any topic. Your progress is tracked separately for each chapter.
JavaScript fundamentals
TypeScript fundamentals
Playwright • setup & locators
Playwright • interactions & assertions
Playwright • framework & debugging
Playwright • real-world automation
Interactive practice lab
Try the controls below and observe the corresponding Playwright locator instructions. This browser demo does not execute Node.js or Playwright Test; copy the code to your local Playwright project to automate it.
Task manager demo
Suggested automation
Actions on the demo update these examples. In your local test use a page that contains the same controls.
Download a standalone lab page, then serve it locally (for example with Python's http.server). Navigate to it in a Playwright test.