← Back to library
Playwrightnetworkintermediatev1.20+

Intercept & mock a GET request

Mock a REST API response so your test never hits the real server.

1import { test, expect } from '@playwright/test';
2
3test('shows mocked user data', async ({ page }) => {
4  // Intercept before navigation
5  await page.route('**/api/user', async (route) => {
6    await route.fulfill({
7      status: 200,
8      contentType: 'application/json',
9      body: JSON.stringify({ id: 1, name: 'QA Engineer' }),
10    });
11  });
12
13  await page.goto('/profile');
14  await expect(page.getByText('QA Engineer')).toBeVisible();
15});
#intercept#mock#api#route