2020-12-30 18:04:51 -08:00
|
|
|
<!-- THIS FILE IS NOW GENERATED -->
|
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
# Network
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Playwright provides APIs to **monitor** and **modify** network traffic, both
|
|
|
|
HTTP and HTTPS. Any requests that page does, including
|
|
|
|
[XHRs](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) and
|
|
|
|
[fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) requests,
|
|
|
|
can be tracked, modified and handled.
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-05-11 09:54:03 -07:00
|
|
|
<!-- GEN:toc-top-level -->
|
|
|
|
- [HTTP Authentication](#http-authentication)
|
|
|
|
- [Handle file downloads](#handle-file-downloads)
|
|
|
|
- [Network events](#network-events)
|
|
|
|
- [Handle requests](#handle-requests)
|
|
|
|
- [Modify requests](#modify-requests)
|
|
|
|
- [Abort requests](#abort-requests)
|
|
|
|
<!-- GEN:stop -->
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
<br/>
|
|
|
|
|
|
|
|
## HTTP Authentication
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
```js
|
|
|
|
const context = await browser.newContext({
|
|
|
|
httpCredentials: {
|
|
|
|
username: 'bill',
|
|
|
|
password: 'pa55w0rd',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const page = await context.newPage();
|
2020-04-20 14:04:49 -07:00
|
|
|
await page.goto('https://example.com');
|
2020-04-19 19:42:40 -07:00
|
|
|
```
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
#### API reference
|
2020-12-04 11:09:20 -08:00
|
|
|
- [browser.newContext([options])](./api.md#browsernewcontextoptions)
|
2020-04-19 19:42:40 -07:00
|
|
|
|
|
|
|
<br/>
|
|
|
|
|
|
|
|
## Handle file downloads
|
|
|
|
|
|
|
|
```js
|
|
|
|
const [ download ] = await Promise.all([
|
2020-05-07 22:33:35 +03:00
|
|
|
page.waitForEvent('download'), // <-- start waiting for the download
|
|
|
|
page.click('button#delayed-download') // <-- perform the action that directly or indirectly initiates it.
|
2020-04-19 19:42:40 -07:00
|
|
|
]);
|
|
|
|
const path = await download.path();
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
For every attachment downloaded by the page,
|
|
|
|
[page.on('download')](./api.md#pageondownload) 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.
|
2020-04-19 19:42:40 -07:00
|
|
|
|
|
|
|
#### Variations
|
|
|
|
|
|
|
|
If you have no idea what initiates the download, you can still handle the event:
|
|
|
|
|
|
|
|
```js
|
|
|
|
page.on('download', download => download.path().then(console.log));
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
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.
|
2020-04-19 19:42:40 -07:00
|
|
|
|
|
|
|
#### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [Download]
|
|
|
|
- [page.on('download')](./api.md#pageondownload)
|
|
|
|
- [page.waitForEvent(event[, optionsOrPredicate])](./api.md#pagewaitforeventevent-optionsorpredicate)
|
2020-04-19 19:42:40 -07:00
|
|
|
|
|
|
|
<br/>
|
|
|
|
|
|
|
|
## Network events
|
|
|
|
|
|
|
|
You can monitor all the requests and responses:
|
2020-04-16 09:39:33 -07:00
|
|
|
|
|
|
|
```js
|
2020-04-16 10:48:38 -07:00
|
|
|
const { chromium, webkit, firefox } = require('playwright');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const browser = await chromium.launch();
|
|
|
|
const page = await browser.newPage();
|
|
|
|
|
2020-04-20 14:04:49 -07:00
|
|
|
// Subscribe to 'request' and 'response' events.
|
2020-04-16 10:48:38 -07:00
|
|
|
page.on('request', request =>
|
|
|
|
console.log('>>', request.method(), request.url()));
|
|
|
|
page.on('response', response =>
|
|
|
|
console.log('<<', response.status(), response.url()));
|
|
|
|
await page.goto('https://example.com');
|
|
|
|
|
|
|
|
await browser.close();
|
|
|
|
})();
|
2020-04-16 09:39:33 -07:00
|
|
|
```
|
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
Or wait for a network response after the button click:
|
2020-04-16 09:39:33 -07:00
|
|
|
|
|
|
|
```js
|
2020-04-20 14:04:49 -07:00
|
|
|
// Use a glob URL pattern
|
2020-04-16 09:39:33 -07:00
|
|
|
const [response] = await Promise.all([
|
2020-04-20 14:04:49 -07:00
|
|
|
page.waitForResponse('**/api/fetch_data'),
|
2020-04-16 10:48:38 -07:00
|
|
|
page.click('button#update'),
|
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Variations
|
|
|
|
|
|
|
|
```js
|
2020-04-20 14:04:49 -07:00
|
|
|
// Use a RegExp
|
2020-04-16 10:48:38 -07:00
|
|
|
const [response] = await Promise.all([
|
2020-04-20 14:04:49 -07:00
|
|
|
page.waitForResponse(/\.jpeg$/),
|
2020-04-16 10:48:38 -07:00
|
|
|
page.click('button#update'),
|
|
|
|
]);
|
|
|
|
|
2020-04-20 14:04:49 -07:00
|
|
|
// Use a predicate taking a Response object
|
2020-04-16 10:48:38 -07:00
|
|
|
const [response] = await Promise.all([
|
2020-04-20 14:04:49 -07:00
|
|
|
page.waitForResponse(response => response.url().includes(token)),
|
2020-04-16 10:48:38 -07:00
|
|
|
page.click('button#update'),
|
2020-04-16 09:39:33 -07:00
|
|
|
]);
|
|
|
|
```
|
|
|
|
|
|
|
|
#### API reference
|
2020-12-30 18:04:51 -08:00
|
|
|
- [Request]
|
|
|
|
- [Response]
|
2020-12-04 11:09:20 -08:00
|
|
|
- [page.on('request')](./api.md#pageonrequest)
|
|
|
|
- [page.on('response')](./api.md#pageonresponse)
|
|
|
|
- [page.waitForRequest(urlOrPredicate[, options])](./api.md#pagewaitforrequesturlorpredicate-options)
|
|
|
|
- [page.waitForResponse(urlOrPredicate[, options])](./api.md#pagewaitforresponseurlorpredicate-options)
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-16 10:48:38 -07:00
|
|
|
<br/>
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
## Handle requests
|
|
|
|
|
2020-04-16 09:39:33 -07:00
|
|
|
```js
|
2020-04-20 14:04:49 -07:00
|
|
|
await page.route('**/api/fetch_data', route => route.fulfill({
|
2020-04-16 09:39:33 -07:00
|
|
|
status: 200,
|
|
|
|
body: testData,
|
|
|
|
}));
|
|
|
|
await page.goto('https://example.com');
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
You can mock API endpoints via handling the network quests in your Playwright
|
|
|
|
script.
|
2020-05-02 17:21:46 -07:00
|
|
|
|
2020-04-16 10:48:38 -07:00
|
|
|
#### Variations
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Set up route on the entire browser context.
|
|
|
|
// It will apply to popup windows and opened links.
|
|
|
|
|
2020-04-20 14:04:49 -07:00
|
|
|
await browserContext.route('**/api/login', route => route.fulfill({
|
2020-04-16 10:48:38 -07:00
|
|
|
status: 200,
|
|
|
|
body: 'accept',
|
|
|
|
}));
|
|
|
|
await page.goto('https://example.com');
|
|
|
|
```
|
|
|
|
|
2020-04-16 09:39:33 -07:00
|
|
|
#### API reference
|
2020-12-04 11:09:20 -08:00
|
|
|
- [browserContext.route(url, handler)](./api.md#browsercontextrouteurl-handler)
|
|
|
|
- [browserContext.unroute(url[, handler])](./api.md#browsercontextunrouteurl-handler)
|
|
|
|
- [page.route(url, handler)](./api.md#pagerouteurl-handler)
|
|
|
|
- [page.unroute(url[, handler])](./api.md#pageunrouteurl-handler)
|
2020-12-30 18:04:51 -08:00
|
|
|
- [Route]
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-16 10:48:38 -07:00
|
|
|
<br/>
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
## Modify requests
|
2020-04-16 10:48:38 -07:00
|
|
|
|
|
|
|
```js
|
2020-05-02 17:21:46 -07:00
|
|
|
// Delete header
|
2020-04-16 10:48:38 -07:00
|
|
|
await page.route('**/*', route => {
|
|
|
|
const headers = route.request().headers();
|
|
|
|
delete headers['X-Secret'];
|
|
|
|
route.continue({headers});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Continue requests as POST.
|
2020-04-20 14:04:49 -07:00
|
|
|
await page.route('**/*', route => route.continue({method: 'POST'}));
|
2020-04-16 10:48:38 -07:00
|
|
|
```
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
You can continue requests with modifications. Example above removes an HTTP
|
|
|
|
header from the outgoing requests.
|
2020-04-16 09:39:33 -07:00
|
|
|
|
2020-04-19 19:42:40 -07:00
|
|
|
## Abort requests
|
2020-04-16 09:39:33 -07:00
|
|
|
|
|
|
|
```js
|
2020-04-19 19:42:40 -07:00
|
|
|
await page.route('**/*.{png,jpg,jpeg}', route => route.abort());
|
|
|
|
|
2020-05-02 17:21:46 -07:00
|
|
|
// Abort based on the request type
|
2020-04-19 19:42:40 -07:00
|
|
|
await page.route('**/*', route => {
|
|
|
|
return route.request().resourceType() === 'image' ?
|
|
|
|
route.abort() : route.continue();
|
|
|
|
});
|
|
|
|
```
|
2020-04-16 09:39:33 -07:00
|
|
|
|
|
|
|
#### API reference
|
2020-12-04 11:09:20 -08:00
|
|
|
- [page.route(url, handler)](./api.md#pagerouteurl-handler)
|
|
|
|
- [browserContext.route(url, handler)](./api.md#browsercontextrouteurl-handler)
|
|
|
|
- [route.abort([errorCode])](./api.md#routeaborterrorcode)
|
2020-04-19 19:42:40 -07:00
|
|
|
|
|
|
|
<br/>
|
2020-12-30 18:04:51 -08:00
|
|
|
[Playwright]: api.md#class-playwright "Playwright"
|
|
|
|
[Browser]: api.md#class-browser "Browser"
|
|
|
|
[BrowserContext]: api.md#class-browsercontext "BrowserContext"
|
|
|
|
[Page]: api.md#class-page "Page"
|
|
|
|
[Frame]: api.md#class-frame "Frame"
|
|
|
|
[ElementHandle]: api.md#class-elementhandle "ElementHandle"
|
|
|
|
[JSHandle]: api.md#class-jshandle "JSHandle"
|
|
|
|
[ConsoleMessage]: api.md#class-consolemessage "ConsoleMessage"
|
|
|
|
[Dialog]: api.md#class-dialog "Dialog"
|
|
|
|
[Download]: api.md#class-download "Download"
|
|
|
|
[Video]: api.md#class-video "Video"
|
|
|
|
[FileChooser]: api.md#class-filechooser "FileChooser"
|
|
|
|
[Keyboard]: api.md#class-keyboard "Keyboard"
|
|
|
|
[Mouse]: api.md#class-mouse "Mouse"
|
|
|
|
[Touchscreen]: api.md#class-touchscreen "Touchscreen"
|
|
|
|
[Request]: api.md#class-request "Request"
|
|
|
|
[Response]: api.md#class-response "Response"
|
|
|
|
[Selectors]: api.md#class-selectors "Selectors"
|
|
|
|
[Route]: api.md#class-route "Route"
|
|
|
|
[WebSocket]: api.md#class-websocket "WebSocket"
|
|
|
|
[TimeoutError]: api.md#class-timeouterror "TimeoutError"
|
|
|
|
[Accessibility]: api.md#class-accessibility "Accessibility"
|
|
|
|
[Worker]: api.md#class-worker "Worker"
|
|
|
|
[BrowserServer]: api.md#class-browserserver "BrowserServer"
|
|
|
|
[BrowserType]: api.md#class-browsertype "BrowserType"
|
|
|
|
[Logger]: api.md#class-logger "Logger"
|
|
|
|
[ChromiumBrowser]: api.md#class-chromiumbrowser "ChromiumBrowser"
|
|
|
|
[ChromiumBrowserContext]: api.md#class-chromiumbrowsercontext "ChromiumBrowserContext"
|
|
|
|
[ChromiumCoverage]: api.md#class-chromiumcoverage "ChromiumCoverage"
|
|
|
|
[CDPSession]: api.md#class-cdpsession "CDPSession"
|
|
|
|
[FirefoxBrowser]: api.md#class-firefoxbrowser "FirefoxBrowser"
|
|
|
|
[WebKitBrowser]: api.md#class-webkitbrowser "WebKitBrowser"
|
|
|
|
[Array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array "Array"
|
|
|
|
[Buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer "Buffer"
|
|
|
|
[ChildProcess]: https://nodejs.org/api/child_process.html "ChildProcess"
|
|
|
|
[Element]: https://developer.mozilla.org/en-US/docs/Web/API/element "Element"
|
|
|
|
[Error]: https://nodejs.org/api/errors.html#errors_class_error "Error"
|
|
|
|
[EvaluationArgument]: #evaluationargument "Evaluation Argument"
|
|
|
|
[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map "Map"
|
|
|
|
[Object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object "Object"
|
|
|
|
[Promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise "Promise"
|
|
|
|
[RegExp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp "RegExp"
|
|
|
|
[Serializable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Description "Serializable"
|
|
|
|
[UIEvent.detail]: https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/detail "UIEvent.detail"
|
|
|
|
[URL]: https://nodejs.org/api/url.html "URL"
|
|
|
|
[USKeyboardLayout]: ../src/usKeyboardLayout.ts "USKeyboardLayout"
|
|
|
|
[UnixTime]: https://en.wikipedia.org/wiki/Unix_time "Unix Time"
|
|
|
|
[boolean]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type "Boolean"
|
|
|
|
[function]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function "Function"
|
|
|
|
[iterator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols "Iterator"
|
|
|
|
[null]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null "null"
|
|
|
|
[number]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type "Number"
|
|
|
|
[origin]: https://developer.mozilla.org/en-US/docs/Glossary/Origin "Origin"
|
|
|
|
[selector]: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors "selector"
|
|
|
|
[Readable]: https://nodejs.org/api/stream.html#stream_class_stream_readable "Readable"
|
|
|
|
[string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type "string"
|
|
|
|
[xpath]: https://developer.mozilla.org/en-US/docs/Web/XPath "xpath"
|