Blame

369df4 Thai Pangsakulyanont 2026-01-29 07:13:57 1
---
2
name: playwright
3
description: Playwright testing. Use this skill to write and run automated tests for web applications using Playwright.
4
---
5
6
# Test authoring guidelines
7
8
For more details on Playwright best practices see <https://playwright.dev/docs/best-practices>
9
10
## Test user-visible behavior
11
12
Automated tests should verify that the application code works for the end users, and avoid relying on implementation details such as things which users will not typically use, see, or even know about such as the name of a function, whether something is an array, or the CSS class of some element. The end user will see or interact with what is rendered on the page, so your test should typically only see/interact with the same rendered output.
13
14
## Make tests as isolated as possible
15
16
Each test should be completely isolated from another test and should run independently with its own local storage, session storage, data, cookies etc.
17
18
## Avoid testing third-party dependencies
19
20
Only test what you control. Don't try to test links to external sites or third party servers that you do not control, unless it is specifically for testing purposes.
21
22
## Use locators
23
24
Locators come with auto waiting and retry-ability. To make tests resilient, we recommend prioritizing user-facing attributes and explicit contracts.
25
26
```javascript
27
// 👍 Use role selectors
28
page.getByRole("button", { name: "submit" });
29
30
// Using filters to locate elements with text
31
page.getByRole("listitem").filter({ hasText: "Product 2" });
32
33
// Use chaining and filtering
34
page
35
.getByRole("listitem")
36
.filter({ hasText: "Product 2" })
37
.getByRole("button", { name: "Add to cart" });
38
39
// 👎 Avoid CSS selectors
40
page.locator("button.buttonIcon.episode-actions-later");
41
```
42
43
### Locator preference
44
45
1. getByRole to locate by explicit and implicit accessibility attributes ⭐⭐⭐⭐⭐
46
2. getByText to locate by text content
47
3. getByLabel to locate a form control by associated label's text
48
4. getByPlaceholder to locate an input by placeholder
49
5. getByAltText to locate an element, usually image, by its text alternative
50
6. getByTitle to locate an element by its title attribute
51
7. getByTestId to locate an element based on its `data-testid`
52
53
### Filtering
54
55
```javascript
56
// Filter elements having text
57
page.getByRole("listitem").filter({ hasText: "Product 2" });
58
59
// Filter elements not having text
60
page.getByRole("listitem").filter({ hasNotText: "Out of stock" });
61
62
// Filter elements having another locator inside
63
page
64
.getByRole("listitem")
65
.filter({ has: page.getByRole("heading", { name: "Product 2" }) });
66
67
// Filter only visible elements
68
// Note: Hidden elements do not have a role, so this filter is not
69
// needed when using getByRole
70
// 👎 CSS selector is not recommended, use a better locator if possible
71
page.locator(".something").filter({ visible: true });
72
```
73
74
The filtering locator **must be relative** to the original locator and is queried starting with the original locator match, not the document root. Therefore, the following will not work, because the filtering locator starts matching from the `<ul>` list element that is outside of the `<li>` list item matched by the original locator:
75
76
```javascript
77
// ✖ WRONG
78
page
79
.getByRole('listitem')
80
.filter({ has: page.getByRole('list').getByText('Product 2') }))
81
```
82
83
There is `hasNot` which does the opposite of `has`.
84
85
### Combining locators
86
87
```javascript
88
// Chaining: Find buttons inside listitems
89
page.getByRole("listitem").getByRole("button");
90
91
// Use `and` for intersection (buttons whose title is Subscribe)
92
page.getByRole("button").and(page.getByTitle("Subscribe"));
93
94
// Use `or` to match multiple locators
95
page
96
.getByRole("button", { name: "New" })
97
.or(page.getByText("Confirm security settings"));
98
99
// Example: Dismiss a known dialog before clicking New
100
const newEmail = page.getByRole("button", { name: "New" });
101
const dialog = page.getByText("Confirm security settings");
102
await expect(newEmail.or(dialog).first()).toBeVisible();
103
if (await dialog.isVisible())
104
await page.getByRole("button", { name: "Dismiss" }).click();
105
await newEmail.click();
106
```
107
108
Locators are strict. This means that all operations on locators that imply some target DOM element will throw an exception if more than one element matches. For example, the following call throws if there are several buttons in the DOM:
109
110
```javascript
111
// Throws an error if more than one
112
await page.getByRole("button").click();
113
114
// Click the first button
115
await page.getByRole("button").first().click();
116
```
117
118
For more info on selectors refer to <https://playwright.dev/docs/locators>
119
120
## Use web first assertions
121
122
```javascript
123
// 👎 Don't use manual assertions
124
expect(await page.getByText("welcome").isVisible()).toBe(true);
125
126
// 👍 Use web first assertions
127
await expect(page.getByText("welcome")).toBeVisible();
128
```
129
130
## When encountering challenges
131
132
If you encounter challenges when writing tests, it may be tempting to work around them (e.g. by using timeouts, sleeps, or brittle selectors). DO NOT DO THAT! Instead, try to make the app more testable first. Maybe adding semantic attributes (best), data attributes, or test IDs. For example, if a test script clicks the button too fast (before it is ready to be clicked), consider adjusting the app to initially disable the button until it is really ready to be clicked.
133
134
## Locator handlers
135
136
When testing a web page, sometimes unexpected overlays like a "Sign up" dialog appear and block actions you want to automate, e.g. clicking a button. These overlays don't always show up in the same way or at the same time, making them tricky to handle in automated tests.
137
138
The `addLocatorHandler` lets you set up a special function, called a handler, that activates when it detects that overlay is visible. The handler's job is to remove the overlay, allowing your test to continue as if the overlay wasn't there.
139
140
Running the handler will alter your page state mid-test. For example it will change the currently focused element and move the mouse. Make sure that actions that run after the handler are self-contained and do not rely on the focus and mouse state being unchanged.
141
142
```javascript
143
// Setup the handler.
144
await page.addLocatorHandler(
145
page.getByText("Sign up to the newsletter"),
146
async () => {
147
await page.getByRole("button", { name: "No thanks" }).click();
148
},
149
);
150
151
// Write the test as usual.
152
await page.goto("https://example.com");
153
await page.getByRole("button", { name: "Start here" }).click();
154
```
155
156
# Page Objects
157
158
To set up structure for page objects, extend the base `test` and `expect` functions:
159
160
```javascript
161
// support/index.ts
162
import { test as base, expect as baseExpect } from "@playwright/test";
163
import { AppTester } from "./AppTester";
164
165
export const test =
166
base.extend <
167
{ app: AppTester } >
168
{
169
app: async ({ page }, use) => {
170
const app = new AppTester(page);
171
await use(app);
172
},
173
};
174
175
export const expect = baseExpect;
176
```
177
178
Create a context interface for page objects:
179
180
```javascript
181
// support/PageObjectContext.ts
182
export interface PageObjectContext {
183
page: Page
184
}
185
```
186
187
Create an AppTester, the root page object:
188
189
```javascript
190
import type { PageObjectContext } from './PageObjectContext'
191
import { LoginPageTester } from './LoginPageTester'
192
import { RepoPageTester } from './RepoPageTester'
193
194
export class AppTester {
195
constructor(public context: PageObjectContext) {}
196
get loginPage() {
197
return new LoginPageTester(this.context)
198
}
199
get repoPage() {
200
return new RepoPageTester(this.context)
201
}
202
}
203
```
204
205
Create a page object for each page in your app. For example, a LoginPageTester:
206
207
```javascript
208
export class LoginPageTester {
209
constructor(public context: PageObjectContext) {}
210
async goto() {
211
const { page } = this.context
212
await page.goto('/login')
213
}
214
async login(username: string, password: string) {
215
const { page } = this.context
216
await page.getByRole('textbox', { name: 'Username' }).fill(username)
217
await page.getByRole('textbox', { name: 'Password' }).fill(password)
218
await page.getByRole('button', { name: 'Sign in' }).click()
219
}
220
}
221
```
222
223
A page object:
224
225
- Is a class.
226
- Has a constructor that takes a `PageObjectContext`.
227
- Exposes methods and properties that represent user actions and elements on the page.
228
229
Now it can be used in tests:
230
231
```javascript
232
import { test, expect } from "./support";
233
234
test("Create a new issue", async ({ app }) => {
235
await app.loginPage.goto();
236
await app.loginPage.login("username", "password");
237
await app.repoPage.goto("myorg/myrepo");
238
});
239
```
240
241
Note: Work iteratively and don't create a premature abstraction! Use existing page object if possible. If not, don't create a new page object just yet! Implement it directly inside the test, and get it working first. Once working, analyze your test script to see if the hardcoded behavior should be added to a an existing page object or a new page object should be created.