2020-12-30 18:04:51 -08:00
|
|
|
<!-- THIS FILE IS NOW GENERATED -->
|
|
|
|
|
2020-04-24 12:19:31 -07:00
|
|
|
# Getting Started
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-09-09 17:32:49 -07:00
|
|
|
<!-- GEN:toc-top-level -->
|
2020-04-24 12:19:31 -07:00
|
|
|
- [Installation](#installation)
|
|
|
|
- [Usage](#usage)
|
2020-05-03 14:22:07 -07:00
|
|
|
- [First script](#first-script)
|
2020-09-09 17:32:49 -07:00
|
|
|
- [Record scripts](#record-scripts)
|
|
|
|
- [TypeScript support](#typescript-support)
|
2020-05-03 14:22:07 -07:00
|
|
|
- [System requirements](#system-requirements)
|
2020-04-24 12:19:31 -07:00
|
|
|
<!-- GEN:stop -->
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-04-24 12:19:31 -07:00
|
|
|
## Installation
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Use npm or Yarn to install Playwright in your Node.js project. See
|
|
|
|
[system requirements](#system-requirements).
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-05-07 22:33:35 +03:00
|
|
|
```sh
|
2020-05-26 10:24:48 -07:00
|
|
|
npm i -D playwright
|
2020-04-21 15:42:52 -07:00
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
This single command downloads the Playwright NPM package and browser binaries
|
|
|
|
for Chromium, Firefox and WebKit. To modify this behavior see
|
|
|
|
[installation parameters](./installation.md).
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-04-24 12:19:31 -07:00
|
|
|
## Usage
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Once installed, you can `require` Playwright in a Node.js script, and launch any
|
|
|
|
of the 3 browsers (`chromium`, `firefox` and `webkit`).
|
2020-04-21 15:42:52 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { chromium } = require('playwright');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const browser = await chromium.launch();
|
|
|
|
// Create pages, interact with UI elements, assert values
|
|
|
|
await browser.close();
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Playwright APIs are asynchronous and return Promise objects. Our code examples
|
|
|
|
use
|
|
|
|
[the async/await pattern](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await)
|
|
|
|
to ease readability. The code is wrapped in an unnamed async arrow function
|
|
|
|
which is invoking itself.
|
2020-04-21 15:42:52 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
(async () => { // Start of async arrow function
|
|
|
|
// Function code
|
|
|
|
// ...
|
|
|
|
})(); // End of the function and () to invoke itself
|
|
|
|
```
|
|
|
|
|
2020-05-03 14:22:07 -07:00
|
|
|
## First script
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
In our first script, we will navigate to `whatsmyuseragent.org` and take a
|
|
|
|
screenshot in WebKit.
|
2020-04-21 15:42:52 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
const { webkit } = require('playwright');
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
const browser = await webkit.launch();
|
|
|
|
const page = await browser.newPage();
|
|
|
|
await page.goto('http://whatsmyuseragent.org/');
|
|
|
|
await page.screenshot({ path: `example.png` });
|
|
|
|
await browser.close();
|
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
By default, Playwright runs the browsers in headless mode. To see the browser
|
|
|
|
UI, pass the `headless: false` flag while launching the browser. You can also
|
|
|
|
use `slowMo` to slow down execution.
|
2020-04-21 15:42:52 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
firefox.launch({ headless: false, slowMo: 50 });
|
|
|
|
```
|
|
|
|
|
2020-09-09 17:32:49 -07:00
|
|
|
## Record scripts
|
2020-05-03 14:22:07 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
[Playwright CLI](./cli.md) can be used to record user interactions and generate
|
|
|
|
JavaScript code.
|
2020-04-21 15:42:52 -07:00
|
|
|
|
2020-12-28 17:38:44 -08:00
|
|
|
```sh
|
|
|
|
npx playwright codegen wikipedia.org
|
2020-09-09 17:32:49 -07:00
|
|
|
```
|
2020-06-15 18:34:58 -07:00
|
|
|
|
2020-09-09 17:32:49 -07:00
|
|
|
## TypeScript support
|
2020-06-15 18:34:58 -07:00
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Playwright includes built-in support for TypeScript. Type definitions will be
|
|
|
|
imported automatically. It is recommended to use type-checking to improve the
|
|
|
|
IDE experience.
|
2020-06-15 18:34:58 -07:00
|
|
|
|
2020-09-09 17:32:49 -07:00
|
|
|
### In JavaScript
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
Add the following to the top of your JavaScript file to get type-checking in VS
|
|
|
|
Code or WebStorm.
|
2020-06-15 18:34:58 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
//@ts-check
|
|
|
|
// ...
|
|
|
|
```
|
|
|
|
|
2020-09-09 17:32:49 -07:00
|
|
|
Alternatively, you can use JSDoc to set types for variables.
|
2020-06-15 18:34:58 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
/** @type {import('playwright').Page} */
|
|
|
|
let page;
|
|
|
|
```
|
2020-09-09 17:32:49 -07:00
|
|
|
|
|
|
|
### In TypeScript
|
2020-12-30 18:04:51 -08:00
|
|
|
|
|
|
|
TypeScript support will work out-of-the-box. Types can also be imported
|
|
|
|
explicitly.
|
2020-09-09 17:32:49 -07:00
|
|
|
|
|
|
|
```ts
|
|
|
|
let page: import('playwright').Page;
|
|
|
|
```
|
|
|
|
|
|
|
|
## System requirements
|
|
|
|
|
2020-12-30 18:04:51 -08:00
|
|
|
Playwright requires Node.js version 10.17 or above. The browser binaries for
|
|
|
|
Chromium, Firefox and WebKit work across the 3 platforms (Windows, macOS,
|
|
|
|
Linux):
|
2020-09-09 17:32:49 -07:00
|
|
|
* **Windows**: Works with Windows and Windows Subsystem for Linux (WSL).
|
|
|
|
* **macOS**: Requires 10.14 or above.
|
2020-12-30 18:04:51 -08:00
|
|
|
* **Linux**: Depending on your Linux distribution, you might need to install
|
|
|
|
additional dependencies to run the browsers.
|
2020-09-09 17:32:49 -07:00
|
|
|
* Firefox requires Ubuntu 18.04+
|
2020-12-30 18:04:51 -08:00
|
|
|
* For Ubuntu 18.04, the additional dependencies are defined in
|
|
|
|
[our Docker image](docker/Dockerfile.bionic), which is based on Ubuntu.
|
|
|
|
[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"
|