2020-04-01 21:06:22 +02:00
# 🎭 Playwright
2020-04-22 07:08:56 +02:00
[](https://www.npmjs.com/package/playwright) <!-- GEN:chromium-version-badge --> [](https://www.chromium.org/Home)<!-- GEN:stop --> <!-- GEN:firefox-version-badge --> [](https://www.mozilla.org/en-US/firefox/new/)<!-- GEN:stop --> [](https://webkit.org/) [](https://join.slack.com/t/playwright/shared_invite/enQtOTEyMTUxMzgxMjIwLThjMDUxZmIyNTRiMTJjNjIyMzdmZDA3MTQxZWUwZTFjZjQwNGYxZGM5MzRmNzZlMWI5ZWUyOTkzMjE5Njg1NDg)
2020-01-19 02:41:52 +01:00
2020-04-23 11:08:30 +02:00
###### [API](https://github.com/microsoft/playwright/blob/master/docs/api.md) | [Changelog](https://github.com/microsoft/playwright/releases) | [Contributing](#contributing)
2020-01-09 01:53:57 +01:00
2020-01-22 19:04:36 +01:00
2020-01-31 15:42:29 +01:00
Playwright is a Node library to automate the [Chromium ](https://www.chromium.org/Home ), [WebKit ](https://webkit.org/ ) and [Firefox ](https://www.mozilla.org/en-US/firefox/new/ ) browsers with a single API. It enables **cross-browser** web automation that is **ever-green** , **capable** , **reliable** and **fast** .
2020-01-22 19:04:36 +01:00
| | ver | Linux | macOS | Win |
| ---: | :---: | :---: | :---: | :---: |
2020-04-23 03:00:49 +02:00
| Chromium| <!-- GEN:chromium - version --> 84.0.4117.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
2020-01-22 19:04:36 +01:00
| WebKit | 13.0.4 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
2020-04-22 07:08:56 +02:00
| Firefox | <!-- GEN:firefox - version --> 76.0b5<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
2020-01-31 15:53:33 +01:00
- Headless is supported for all the browsers on all platforms.
2020-01-24 21:19:17 +01:00
2020-01-31 15:53:33 +01:00
Our primary goal with Playwright is to improve automated UI testing by eliminating flakiness, improving the speed of execution and offering insights into the browser operation.
2020-01-07 20:51:22 +01:00
2020-03-23 18:39:43 +01:00
### Usage
2020-01-07 20:51:22 +01:00
```
npm i playwright
```
2020-01-12 08:39:49 +01:00
This installs Playwright along with its dependencies and the browser binaries. Browser binaries are about 50-100MB each, so expect the installation network traffic to be substantial.
2020-03-23 18:39:43 +01:00
Once installed, Playwright can be used to create a browser instance, open pages and then automate interactions.
2020-01-07 20:51:22 +01:00
2020-04-22 00:42:52 +02:00
* [Documentation ](docs/README.md )
2020-03-23 18:39:43 +01:00
* [API reference ](docs/api.md )
2020-01-07 20:51:22 +01:00
2020-01-09 01:53:57 +01:00
### Examples
2020-01-11 01:03:10 +01:00
#### Page screenshot
2020-01-07 20:51:22 +01:00
2020-01-24 21:19:17 +01:00
This code snippet navigates to whatsmyuseragent.org in Chromium, Firefox and WebKit, and saves 3 screenshots.
2020-01-07 20:51:22 +01:00
```js
2020-01-24 23:49:47 +01:00
const playwright = require('playwright');
2020-01-07 20:51:22 +01:00
(async () => {
2020-01-24 23:49:47 +01:00
for (const browserType of ['chromium', 'firefox', 'webkit']) {
const browser = await playwright[browserType].launch();
2020-02-10 06:44:18 +01:00
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('http://whatsmyuseragent.org/');
2020-01-24 23:49:47 +01:00
await page.screenshot({ path: `example-${browserType}.png` });
2020-01-24 21:19:17 +01:00
await browser.close();
}
2020-01-18 05:09:44 +01:00
})();
```
2020-01-24 21:19:17 +01:00
#### Mobile and geolocation
2020-01-18 05:09:44 +01:00
This snippet emulates Mobile Safari on a device at a given geolocation, navigates to maps.google.com, performs action and takes a screenshot.
```js
2020-01-24 23:49:47 +01:00
const { webkit, devices } = require('playwright');
const iPhone11 = devices['iPhone 11 Pro'];
2020-01-18 05:09:44 +01:00
(async () => {
2020-01-24 23:49:47 +01:00
const browser = await webkit.launch();
2020-02-10 06:44:18 +01:00
const context = await browser.newContext({
2020-04-07 05:44:54 +02:00
...iPhone11,
2020-01-18 05:09:44 +01:00
geolocation: { longitude: 12.492507, latitude: 41.889938 },
2020-04-04 23:20:24 +02:00
permissions: ['geolocation']
2020-01-18 05:09:44 +01:00
});
2020-02-10 06:44:18 +01:00
const page = await context.newPage();
await page.goto('https://maps.google.com');
2020-01-18 05:09:44 +01:00
await page.click('text="Your location"');
await page.waitForRequest(/.*preview\/pwa/);
2020-01-24 23:49:47 +01:00
await page.screenshot({ path: 'colosseum-iphone.png' });
2020-01-18 05:09:44 +01:00
await browser.close();
})();
```
2020-03-04 20:03:49 +01:00
#### Evaluate in browser context
2020-01-09 01:53:57 +01:00
This code snippet navigates to example.com in Firefox, and executes a script in the page context.
2020-01-07 20:51:22 +01:00
2020-01-09 01:53:57 +01:00
```js
2020-01-24 23:49:47 +01:00
const { firefox } = require('playwright');
2020-01-07 20:51:22 +01:00
2020-01-09 01:53:57 +01:00
(async () => {
2020-01-24 23:49:47 +01:00
const browser = await firefox.launch();
2020-02-10 06:44:18 +01:00
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('https://www.example.com/');
2020-01-09 01:53:57 +01:00
const dimensions = await page.evaluate(() => {
return {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight,
deviceScaleFactor: window.devicePixelRatio
}
})
console.log(dimensions);
await browser.close();
2020-01-07 20:51:22 +01:00
})();
```
2020-03-04 20:03:49 +01:00
#### Intercept network requests
2020-03-13 22:30:40 +01:00
This code snippet sets up request routing for a WebKit page to log all network requests.
2020-03-04 20:03:49 +01:00
```js
const { webkit } = require('playwright');
(async () => {
const browser = await webkit.launch();
const context = await browser.newContext();
const page = await context.newPage();
// Log and continue all network requests
2020-03-13 22:30:40 +01:00
page.route('**', route => {
console.log(route.request().url());
route.continue();
2020-03-04 20:03:49 +01:00
});
await page.goto('http://todomvc.com');
await browser.close();
})();
```
2020-04-22 00:42:52 +02:00
## Contributing
Check out our [contributing guide ](https://github.com/microsoft/playwright/blob/master/CONTRIBUTING.md ).
2020-01-09 01:53:57 +01:00
## Resources
2019-11-15 19:32:50 +01:00
2020-04-22 00:42:52 +02:00
* [Documentation ](docs/README.md )
* [Example recipes ](docs/examples/README.md )
* [API reference ](docs/api.md )
2020-02-15 01:54:14 +01:00
* [Community showcase ](docs/showcase.md )