ScuttleBot

1
# 🎭 Playwright
2
3
[![npm version](https://img.shields.io/npm/v/playwright.svg)](https://www.npmjs.com/package/playwright) <!-- GEN:chromium-version-badge -->[![Chromium version](https://img.shields.io/badge/chromium-145.0.7632.6-blue.svg?logo=google-chrome)](https://www.chromium.org/Home)<!-- GEN:stop --> <!-- GEN:firefox-version-badge -->[![Firefox version](https://img.shields.io/badge/firefox-146.0.1-blue.svg?logo=firefoxbrowser)](https://www.mozilla.org/en-US/firefox/new/)<!-- GEN:stop --> <!-- GEN:webkit-version-badge -->[![WebKit version](https://img.shields.io/badge/webkit-26.0-blue.svg?logo=safari)](https://webkit.org/)<!-- GEN:stop --> [![Join Discord](https://img.shields.io/badge/join-discord-informational)](https://aka.ms/playwright/discord)
4
5
## [Documentation](https://playwright.dev) | [API reference](https://playwright.dev/docs/api/class-playwright)
6
7
Playwright is a framework for Web Testing and Automation. It allows testing [Chromium](https://www.chromium.org/Home), [Firefox](https://www.mozilla.org/en-US/firefox/new/) and [WebKit](https://webkit.org/) with a single API. Playwright is built to enable cross-browser web automation that is **ever-green**, **capable**, **reliable**, and **fast**.
8
9
| | Linux | macOS | Windows |
10
| :--- | :---: | :---: | :---: |
11
| Chromium <!-- GEN:chromium-version -->145.0.7632.6<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
12
| WebKit <!-- GEN:webkit-version -->26.0<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
13
| Firefox <!-- GEN:firefox-version -->146.0.1<!-- GEN:stop --> | :white_check_mark: | :white_check_mark: | :white_check_mark: |
14
15
Headless execution is supported for all browsers on all platforms. Check out [system requirements](https://playwright.dev/docs/intro#system-requirements) for details.
16
17
Looking for Playwright for [Python](https://playwright.dev/python/docs/intro), [.NET](https://playwright.dev/dotnet/docs/intro), or [Java](https://playwright.dev/java/docs/intro)?
18
19
## Installation
20
21
Playwright has its own test runner for end-to-end tests, we call it Playwright Test.
22
23
### Using init command
24
25
The easiest way to get started with Playwright Test is to run the init command.
26
27
```Shell
28
# Run from your project's root directory
29
npm init playwright@latest
30
# Or create a new project
31
npm init playwright@latest new-project
32
```
33
34
This will create a configuration file, optionally add examples, a GitHub Action workflow and a first test example.spec.ts. You can now jump directly to writing assertions section.
35
36
### Manually
37
38
Add dependency and install browsers.
39
40
```Shell
41
npm i -D @playwright/test
42
# install supported browsers
43
npx playwright install
44
```
45
46
You can optionally install only selected browsers, see [install browsers](https://playwright.dev/docs/cli#install-browsers) for more details. Or you can install no browsers at all and use existing [browser channels](https://playwright.dev/docs/browsers).
47
48
* [Getting started](https://playwright.dev/docs/intro)
49
* [API reference](https://playwright.dev/docs/api/class-playwright)
50
51
## Capabilities
52
53
### Resilient • No flaky tests
54
55
**Auto-wait**. Playwright waits for elements to be actionable prior to performing actions. It also has a rich set of introspection events. The combination of the two eliminates the need for artificial timeouts - a primary cause of flaky tests.
56
57
**Web-first assertions**. Playwright assertions are created specifically for the dynamic web. Checks are automatically retried until the necessary conditions are met.
58
59
**Tracing**. Configure test retry strategy, capture execution trace, videos and screenshots to eliminate flakes.
60
61
### No trade-offs • No limits
62
63
Browsers run web content belonging to different origins in different processes. Playwright is aligned with the architecture of the modern browsers and runs tests out-of-process. This makes Playwright free of the typical in-process test runner limitations.
64
65
**Multiple everything**. Test scenarios that span multiple tabs, multiple origins and multiple users. Create scenarios with different contexts for different users and run them against your server, all in one test.
66
67
**Trusted events**. Hover elements, interact with dynamic controls and produce trusted events. Playwright uses real browser input pipeline indistinguishable from the real user.
68
69
Test frames, pierce Shadow DOM. Playwright selectors pierce shadow DOM and allow entering frames seamlessly.
70
71
### Full isolation • Fast execution
72
73
**Browser contexts**. Playwright creates a browser context for each test. Browser context is equivalent to a brand new browser profile. This delivers full test isolation with zero overhead. Creating a new browser context only takes a handful of milliseconds.
74
75
**Log in once**. Save the authentication state of the context and reuse it in all the tests. This bypasses repetitive log-in operations in each test, yet delivers full isolation of independent tests.
76
77
### Powerful Tooling
78
79
**[Codegen](https://playwright.dev/docs/codegen)**. Generate tests by recording your actions. Save them into any language.
80
81
**[Playwright inspector](https://playwright.dev/docs/inspector)**. Inspect page, generate selectors, step through the test execution, see click points and explore execution logs.
82
83
**[Trace Viewer](https://playwright.dev/docs/trace-viewer)**. Capture all the information to investigate the test failure. Playwright trace contains test execution screencast, live DOM snapshots, action explorer, test source and many more.
84
85
Looking for Playwright for [TypeScript](https://playwright.dev/docs/intro), [JavaScript](https://playwright.dev/docs/intro), [Python](https://playwright.dev/python/docs/intro), [.NET](https://playwright.dev/dotnet/docs/intro), or [Java](https://playwright.dev/java/docs/intro)?
86
87
## Examples
88
89
To learn how to run these Playwright Test examples, check out our [getting started docs](https://playwright.dev/docs/intro).
90
91
#### Page screenshot
92
93
This code snippet navigates to Playwright homepage and saves a screenshot.
94
95
```TypeScript
96
import { test } from '@playwright/test';
97
98
test('Page Screenshot', async ({ page }) => {
99
await page.goto('https://playwright.dev/');
100
await page.screenshot({ path: `example.png` });
101
});
102
```
103
104
#### Mobile and geolocation
105
106
This snippet emulates Mobile Safari on a device at given geolocation, navigates to maps.google.com, performs the action and takes a screenshot.
107
108
```TypeScript
109
import { test, devices } from '@playwright/test';
110
111
test.use({
112
...devices['iPhone 13 Pro'],
113
locale: 'en-US',
114
geolocation: { longitude: 12.492507, latitude: 41.889938 },
115
permissions: ['geolocation'],
116
})
117
118
test('Mobile and geolocation', async ({ page }) => {
119
await page.goto('https://maps.google.com');
120
await page.getByText('Your location').click();
121
await page.waitForRequest(/.*preview\/pwa/);
122
await page.screenshot({ path: 'colosseum-iphone.png' });
123
});
124
```
125
126
#### Evaluate in browser context
127
128
This code snippet navigates to example.com, and executes a script in the page context.
129
130
```TypeScript
131
import { test } from '@playwright/test';
132
133
test('Evaluate in browser context', async ({ page }) => {
134
await page.goto('https://www.example.com/');
135
const dimensions = await page.evaluate(() => {
136
return {
137
width: document.documentElement.clientWidth,
138
height: document.documentElement.clientHeight,
139
deviceScaleFactor: window.devicePixelRatio
140
}
141
});
142
console.log(dimensions);
143
});
144
```
145
146
#### Intercept network requests
147
148
This code snippet sets up request routing for a page to log all network requests.
149
150
```TypeScript
151
import { test } from '@playwright/test';
152
153
test('Intercept network requests', async ({ page }) => {
154
// Log and continue all network requests
155
await page.route('**', route => {
156
console.log(route.request().url());
157
route.continue();
158
});
159
await page.goto('http://todomvc.com');
160
});
161
```
162
163
## Resources
164
165
* [Documentation](https://playwright.dev)
166
* [API reference](https://playwright.dev/docs/api/class-playwright/)
167
* [Contribution guide](CONTRIBUTING.md)
168
* [Changelog](https://github.com/microsoft/playwright/releases)
169

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button