TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
Playwright / Chapter 57 of 60~18 min • Beginner
Chapter 57Playwright

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

  1. Register a route
  2. Navigate to the mocked endpoint
  3. Assert its response text

Example

TypeScript
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');
});
Expected result: Mock ready appears without a live backend response
💡 Point to remember
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