Steps
- Capture the flowRecord the problem with the steps in how to capture a HAR file.
- Open it in HAR PlaygroundDrop the file on HAR Playground. Secrets are redacted before anything is exported.
- Open Export and pick a Playwright formatChoose Playwright routes for
page.routestubs, Playwright routeFromHAR for an API-only HAR plus a snippet, or Regression tests for one test per finding. - Copy or download the codePaste the snippet into your spec, or save the
.api.harnext to your tests. - Run the testRun
npx playwright testand replace any[REDACTED:...]placeholders with test values.
page.route: mock one API call
Use page.route when you want to force one response, such as the 500 that broke checkout, and check how the page copes.
import { test, expect } from '@playwright/test';
test('checkout shows an error when the orders API returns 500', async ({ page }) => {
await page.route('**/api/orders', (route) =>
route.fulfill({
status: 500,
contentType: 'application/json',
body: JSON.stringify({ error: 'internal' }),
}),
);
await page.goto('https://shop.example/checkout');
await expect(page.getByRole('alert')).toBeVisible();
});routeFromHAR: replay a whole capture
Use page.routeFromHAR to serve many responses from a saved HAR, so a test does not need a live backend. Set notFound: 'fallback' so requests that are not in the file go to the network as normal.
import { test, expect } from '@playwright/test';
test('dashboard renders from a recorded API', async ({ page }) => {
await page.routeFromHAR('https://shop.example/fixtures/dashboard-api.har', {
url: '**/api/**',
notFound: 'fallback',
});
await page.goto('https://shop.example/dashboard');
await expect(page.getByRole('heading', { name: 'Dashboard' })).toBeVisible();
});Tips for stable tests
- Keep only the API calls the test needs. A smaller HAR is easier to review and breaks less often.
- Replace redacted placeholders with fixed test data, and never commit real tokens.
- Re-record the HAR when the API changes. Playwright can also update it for you with
update: true. - Prefer a regression test with a clear assertion over a bare replay, so the test fails for the right reason.