Skip to main content

Record and replay with HAR

Why This Matters

Writing stubs by hand works for one response. For a whole page full of API calls, it is tedious and easy to get wrong. A HAR file (HTTP Archive) records real traffic once, with every request and response, and Playwright can play it back. The test then runs against realistic data, without the real backend.

Record

The test in tests/module-5/har.spec.ts records when you ask it to, and replays otherwise:

const har = "tests/module-5/hars/products.har";
const recording = !!process.env.UPDATE_HAR;

test("the product list from a recorded HAR", async ({ page }) => {
  test.skip(!recording && !existsSync(har), `No ${har} yet: record it once with UPDATE_HAR=1`);

  await page.routeFromHAR(har, {
    url: "**/api/shop/products**",
    update: recording,
    updateContent: "embed",
    // A request that is not in the HAR fails instead of quietly reaching the real server.
    notFound: "abort",
  });
  await page.goto("/shopping");

  await expect(page.getByTestId(/^product-\d+$/).filter({ hasText: "Bluetooth Headphones" })).toBeVisible();
  await expect(page.getByTestId("results-count")).toHaveText("10");
});
  • url limits the HAR to the products API. The page itself, its scripts and images still come from the real site.
  • update: true records: the requests go to the real server and are written to the file when the test ends.
  • updateContent: "embed" keeps the response bodies inside the one .har file.
  • notFound: "abort" makes a request that is not in the recording fail loudly.

Record it once, from the project folder:

UPDATE_HAR=1 npx playwright test har.spec.ts --project=chromium

Replay

npx playwright test har.spec.ts --project=chromium

The real runs, recording first and then replaying:

Running 1 test using 1 worker
  ✓  1 …/module-5/har.spec.ts:9:1 › the product list from a recorded HAR (809ms)
  1 passed (1.1s)

Running 1 test using 1 worker
  ✓  1 …/module-5/har.spec.ts:9:1 › the product list from a recorded HAR (817ms)
  1 passed (1.1s)

Before the file existed, the test reported itself as skipped with the message from test.skip, rather than failing for a missing file. A skip with a reason is a clear instruction for the next person.

Commit the .har file: it is test data, just like a fixture.

How replay matches

Playwright matches each request against the recording by URL, method and, for POST requests, the body. The shop asks for /api/shop/products?page=1&limit=6&sort=name&order=asc; that exact request is in the HAR, so it is served from the file. Search for “lamp” during a replay and the page asks for a URL that was never recorded. With notFound: "abort" that request fails, and the shop shows its products error. That is the point: a replayed test only covers what you recorded.

The risk: frozen data

A HAR is a snapshot. If the real API changes (a field renamed, a product added), the replayed test keeps passing on the old data. Keep it honest:

  • Re-record regularly, for example in a scheduled CI job, and review the diff.
  • Keep API tests (module 4) against the real backend, so a changed contract fails somewhere.
  • Use HAR for what it is good at: realistic, fast, repeatable UI tests that do not depend on a backend being up.

Exercise

Open tests/module-5/hars/products.har in your editor. Find the request the shop made, and the stock of the HD Monitor in the recorded response. Then change one product name in the HAR by hand and run the replay. What does that tell you about HAR files and review?

Show suggested answer

The request is a GET to /api/shop/products?page=1&limit=6&sort=name&order=asc; the HD Monitor has "stock": 0 and "inStock": false. After your edit, the replay shows your new name: Playwright serves whatever the file says.

So a HAR file is test data that anyone can edit, and it deserves the same review as test code. A changed HAR in a pull request should be a re-recording with a reason, not a quiet edit that makes a test pass.