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
1:217Structure your tests: describe, hooks, tags
Group tests with describe, share setup in beforeEach, keep every test independent in a fresh browser context, and pick what to run with tags like @smoke.
1:178Page Object Model
Stop repeating locators across tests: put each page's locators and actions in a class, so tests read like manual steps and a UI change is fixed in one place.
1:229Log in once, and use fixtures
Stop logging in through the form in every test: save the session as storage state in a setup project, reuse it in the config, and share helpers with fixtures.