How do I enable 'preserve log' in Playwright's routeFromHAR() method to record the HAR for the entire session?


Sure, you can enable "preserve log" in routeFromHAR(). This method in Playwright lets you serve responses from a HAR file during tests. By default, it only serves requests matching the specified URL pattern.

To record the HAR for the entire session, set the update option to true when calling page.routeFromHAR() or browserContext.routeFromHAR(). This updates the HAR file with actual network info, not just serving requests from the HAR file.

Here's an example:

// Enable "preserve log" and record HAR for whole session
await page.routeFromHAR('https://ray.run/hars/fruit.har', { url: '*/**/api/v1/fruits', update: true });

In this example, we're recording a HAR file named "fruit.har". The URL pattern matches all requests containing "/api/v1/fruits". By setting update to true, each request made during the test is recorded in the HAR file.

Remember, once you've recorded a HAR file, you can modify it by editing its JSON content. The modified mock data can then be used during test execution by turning off or removing the update option when using routeFromHAR(). This lets your tests run against the modified mock data stored in the HAR file instead of hitting an actual API.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.