Network
Playwright provides APIs to monitor and modify network traffic, both HTTP and HTTPS. Any requests that page does, including XHRs and fetch requests, can be tracked, modified and handled.
#
HTTP Authentication#
API reference#
HTTP ProxyYou can configure pages to load over the HTTP(S) proxy or SOCKSv5. Proxy can be either set globally for the entire browser, or for each browser context individually.
You can optionally specify username and password for HTTP(S) proxy, you can also specify hosts to bypass proxy for.
Here is an example of a global proxy:
When specifying proxy for each context individually, you need to give Playwright a hint that proxy will be set. This is done via passing a non-empty proxy server to the browser itself. Here is an example of a context-specific proxy:
#
Network eventsYou can monitor all the requests and responses:
Or wait for a network response after the button click:
#
Variations#
API reference- Request
- Response
- page.on('request')
- page.on('response')
- page.waitForRequest(urlOrPredicate[, options])
- page.waitForResponse(urlOrPredicate[, options])
#
Handle requestsYou can mock API endpoints via handling the network quests in your Playwright script.
#
Variations#
API reference- browserContext.route(url, handler)
- browserContext.unroute(url[, handler])
- page.route(url, handler)
- page.unroute(url[, handler])
- Route
#
Modify requestsYou can continue requests with modifications. Example above removes an HTTP header from the outgoing requests.