Request interception
Request interception enables us to observe which requests and responses are being exchanged as part of our script’s execution. For example, this is how we could print them out when we load our test website:request-interception-read.spec.ts
request-interception-block.spec.ts

resourceType
to stylesheet
would result in the target website loading without any CSS styling.

Response interception
Isolating one or more software components from their dependencies makes them easier to test. We can do so by substituting interactions with such dependencies with simulated, simplified ones. This is also known as stubbing. Playwright makes it easy for us, as for every request we can intercept we also can stub a response. Every time we load it, our test website is sending a request to its backend to fetch a list of best selling books. For our example, we are going to intercept this response and modify it to return a single book we define on the fly.response-interception.spec.ts

Takeaways
- Playwright gives us control over outgoing HTTP requests.
- Playwright can easily stub HTTP responses.
Further reading
- Official documentation on this topic from Playwright.
- Mocks Aren’t Stubs by Martin Fowler.