question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Record network requests and save them as ready-to-use mocks

See original GitHub issue

I’m using playwright for integration tests. And I can’t have a full environment for testing. So I mock api requests and test against them.

When I saw https://github.com/microsoft/playwright-cli/pull/194 this flow for authentication (which is super helpful, thank you), I thought it would be a great idea to generate code for network requests in a file and use them in specific tests (test by test basis in it("...") or beforeEach depending on the scenario.

In this case, I see it like so:

  • Adding an option to the cli to save network requests to a given file (file name) like this npx playwright-cli --save-network-requests=flow_1_requests.json codegen
  • Adding an API method in playwright to use a mock file or multiple files. This would allow to overwrite previous mocks if they are defined in two files. playwright.useMockFiles(context, ['path/to/flow_1_requests.json', 'path/to/flow_2_requests.json']). It takes the context and paths to mock files.

Same as https://github.com/microsoft/playwright-cli/issues/196

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:48
  • Comments:28 (7 by maintainers)

github_iconTop GitHub Comments

10reactions
kousenlsncommented, Aug 3, 2021

Well, I ended up creating a lib if anyone ends up needing this while we don’t have an inside solution at Playwright itself.

https://github.com/kousenlsn/playwright-request-mocker

It records any network request (using recordHar), clears up the HAR file to only keep XHR requests (persisting into a new mock.json file), then automatically mocks everything following the mock file for any future runs.

Feel free to contribute or report any issues, it is quite green still.

8reactions
pavelfeldmancommented, Jun 21, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

Record API requests and HTTP traffic and auto-mock endpoints
Learn how to record entering API requests and HTTP traffic and auto-mock your endpoints with Mockoon.
Read more >
API Auto Mocking | SwaggerHub Documentation
Click Save and Execute, then Done. The mock is now ready to use. You can send requests to the mock as follows: curl...
Read more >
Mocking, Intercepting, Monitoring and Waiting for network ...
In this lesson we learn all about the # network # request handling features of #Playwright. We look at how we can monitor...
Read more >
Mocking in Python - Anna-Lena Popkes
Mocking can be used whenever we don't want to actually call an object. Let's say our Spell class has a function save which...
Read more >
Using Postman Interceptor
Interceptor enables you to capture network requests and cookies directly ... By default, requests will be saved in a debug session in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found