Learning center

Freeze the backend's answers with a recorded HAR

A bug that depends on what the server returned is hard to replay a day later, because the server returns something else. Playwright can serve a recorded HAR file back to the page, so the page receives exactly the responses the reporter's session got. In our fixture, replay worked with the server stopped, and it also showed two ways a recording silently misses the response you need.

Record the responses#

Record a HAR while reproducing, limited to the requests that matter:

const context = await browser.newContext({
  recordHar: { path: 'repro.har', mode: 'minimal', urlFilter: '**/api/**' },
});
// reproduce the bug, then:
await context.close(); // the HAR is written on close

Or with codegen: npx playwright codegen --save-har=repro.har --save-har-glob="**/api/**" https://staging.example.test.

Replay them#

const context = await browser.newContext({ serviceWorkers: 'block' });
const page = await context.newPage();
await page.routeFromHAR('repro.har', { url: '**/api/**', notFound: 'abort' });
await page.goto('https://staging.example.test/orders');

Requests matching url are answered from the file. With notFound: 'abort', the default, a matching request that is not in the file fails. With 'fallback' it goes to the network. Playwright does not serve requests that a service worker intercepts from the HAR, and recommends blocking workers when replaying, as above. Playwright: Replaying from HAR ยท page.routeFromHAR()

What we saw in a fixture#

We recorded two API calls with Playwright 1.63, GET /api/slow?q=apple and POST /api/slow?q=pear with body x=1, then replayed with notFound: 'abort':

Replayed requestResult
GET ?q=apple200 with the recorded body
GET ?q=banana, not recordedFailed to fetch
POST ?q=pear, body x=1200 with the recorded body
POST ?q=pear, body x=2Failed to fetch

The server received none of the four requests. Matching uses the URL and method, and for the POST also the body, so a replay with different input does not silently get the wrong recorded answer.

The first recording attempt went wrong. The page's code had not read the response bodies, and the HAR recorded the responses with no content. Replaying them returned 200 with an empty body. Record with a page that consumes the responses, as the real app does, and open the HAR to check that each entry you need has content.text.

When a HAR replay is the right tool#

SituationHAR replay helps?
The bug depends on a particular API response that has since changedYes: the page gets the old response
The maintainer has no access to the reporter's environment or dataYes, once personal data is removed from the file
The bug is in how the server computed the responseNo: replay skips the server
The bug depends on timing between responsesNot by itself: replay answers as fast as it can. Hold one response to control order.
Requests include a timestamp, nonce or random ID in the URL or bodyOnly if you make them match, since a different value is a different request

Clean the file#

A HAR contains headers, cookies and bodies. Record from a test account, keep the urlFilter narrow, and edit out personal data before sharing. A replayed response must still parse, so replace values with synthetic ones of the same type and shape, rather than deleting fields. Share a HAR file covers what to remove.

Worked example: a price list that broke for one day#

This example is fictional. On Tuesday, a tester sees the pricing table render "NaN" for one plan. By Wednesday the pricing API has been fixed and nobody can see it, but the frontend's handling still needs a fix.

The tester had recorded a HAR of /api/** while reproducing. The developer replays it with routeFromHAR and sees "NaN" again: the recorded response had "price": null for the plan. The frontend formatted null as a number. With the replay, the developer wrote a test that loads the page against the recorded response and fixed the formatter to show "Contact us" for a missing price.

Questions#

Can I replay a HAR exported from Chrome DevTools?

Playwright reads HAR files, and a DevTools export is one. Chrome's default export removes some headers, and its bodies are only those DevTools kept, so check the entries you need before relying on it.

Can I edit a response in the HAR?

Yes. It is JSON, so you can change a value to test a hypothesis, such as setting price back to a number and confirming the table renders. Say in the report which entries you edited.

What does update: true do?

It records into the file instead of replaying, which refreshes a recording against the current server. Use it to create a new recording, not to reproduce an old one.

Return to reproducing what someone else saw for the other layers a replay needs.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading