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 AuthenticationYou can also use browserContext.setHTTPCredentials
to update HTTP credentials of an existing context.
#
API reference#
Handle file downloadsFor 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.
#
VariationsIf 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 eventsYou 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 requestsYou can mock API endpoints via handling the network quests in your Playwright script.
#
Variations#
API referencebrowserContext.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.