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#
You can also use browserContext.setHTTPCredentials to update HTTP credentials of an existing context.
API reference#
Handle file downloads#
For every attachment downloaded by the page, "download" event is emitted. If you create a browser context with the acceptDownloads: true, all these attachments are going to be downloaded into a temporary folder. You can obtain the download url, file system path and payload stream using the Download object from the event.
Variations#
If you have no idea what initiates the download, you can still handle the event:
Note that handling the event forks the control flow and makes script harder to follow. Your scenario might end while you are downloading a file since your main control flow is not awaiting for this operation to resolve.
API reference#
Network events#
You can monitor all the requests and responses:
Or wait for a network response after the button click:
Variations#
API reference#
- class
Request - class
Response - event
'request' - event
'response' page.waitForRequest(urlOrPredicate[, options])page.waitForResponse(urlOrPredicate[, options])
Handle requests#
You 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 requests#
You can continue requests with modifications. Example above removes an HTTP header from the outgoing requests.