API testing and mocking the network

Knowledge Videos

Short animated explainers, organised by subject.

API testing and mocking the network

Use the request fixture to prepare data through the API, and page.route to fake server responses so you can test empty lists, errors and slow responses.

Transcript

Not everything has to go through the screen. Playwright can also talk to the server directly, with the request fixture. This is A P I testing.

Use it to prepare data fast. Create an order with one post request, then open the page and check it. It is quicker and more reliable than clicking through the whole shop.

Playwright can also fake the network. With page dot route, you catch a request from the page and answer it yourself, so the real server is never called.

Now you can test what is hard to trigger by hand. Return an empty list, a server error, or a very slow response, and check that the page shows the right message.

Remember: prepare data with the request fixture, fake responses with page dot route, and test the error cases you cannot trigger by hand.

More in this series