Overview
Playwright’s network interception allows you to intercept, modify, mock, or block network requests. This is essential for testing edge cases, simulating API responses, and controlling network conditions.Basic Request Interception
Intercept and handle network requests using theroute API.
Mock API Responses
Return mock data instead of making real API requests.Modify Requests
Change request properties before they are sent.Modify Responses
Fetch the real response and modify it before returning.Request Information
Access detailed information about intercepted requests.Inspect Requests
Context-Level Interception
Intercept requests for all pages in a context.Context Routes
Conditional Interception
Apply different handling based on request properties.Conditional Routing
Abort Requests
Block specific requests to test offline scenarios or reduce load times.HAR Recording and Replay
Record network traffic to HAR files and replay them later.Unroute
Remove route handlers when no longer needed.Remove Routes
Best Practices
Pattern Specificity
Use specific patterns to avoid unintended interceptions. Match exact endpoints when possible.
Error Handling
Always test both success and failure scenarios. Mock error responses to test error handling.
Performance
Use network mocking to speed up tests by avoiding slow external APIs.
Realistic Mocks
Keep mock data realistic and up-to-date with actual API responses.
Related Resources
Network Events
Monitor and analyze network activity
API Testing
Test APIs directly with APIRequestContext
