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 request | Result |
|---|---|
GET ?q=apple | 200 with the recorded body |
GET ?q=banana, not recorded | Failed to fetch |
POST ?q=pear, body x=1 | 200 with the recorded body |
POST ?q=pear, body x=2 | Failed 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#
| Situation | HAR replay helps? |
|---|---|
| The bug depends on a particular API response that has since changed | Yes: the page gets the old response |
| The maintainer has no access to the reporter's environment or data | Yes, once personal data is removed from the file |
| The bug is in how the server computed the response | No: replay skips the server |
| The bug depends on timing between responses | Not 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 body | Only 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.