← Back to Discord Forum

Is there a way to cache Playwright route .fetch responses and reuse across contexts?

I'm trying to speed up e2e tests and not overwhelm our BE test server by caching responses.

Currently I use an LRU cache to store Playwright APIResponse instances in memory from route.fech().

This approach works until the original context that generated the response gets disposed eventually after the test completes, then I get "fetch response is disposed" error when I do route.fulfill({ response: cachedResponse }) in a different context that runs a different test

Ideally I hope I could clone the APIResponse instance, but APIResponse class doesn't seem to be exported from @playwright/test nor playwright-core/lib/client/fetch

Or if there's another recommended way to cache responses, I'm all ears! Thank you!

This thread is trying to answer question "Is there a way to cache Playwright route .fetch responses and reuse them across different contexts?"

2 replies

If you need all of the responses, you can just do routeFromHar() https://playwright.dev/docs/api/class-page#page-route-from-har with this case, you can just commit the har files and run from them (it supports .zip)

Ooh nice! I skipped that one after reading the title, since I didn't know what Har stood for lol Thanks so much for helping me out!

I think I'll dig into how routeFromHar is implemented under the hood and see if I could use it as a runtime cache, since we can't pre-record the responses due to each test environment having its own data and subdomain!

Related Discord Threads

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.