You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/aria-snapshots.md
+88-91Lines changed: 88 additions & 91 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,19 +1,19 @@
1
1
---
2
-
id: aria-snapshot
3
-
title: "Accessibility Snapshots"
2
+
id: aria-snapshots
3
+
title: "Aria snapshots"
4
4
---
5
5
6
6
## Overview
7
7
8
-
In Playwright, accessibility snapshots provide a YAML representation of the accessible elements on a page.
8
+
In Playwright, aria snapshots provide a YAML representation of the accessibility tree of a page.
9
9
These snapshots can be stored and compared later to verify if the page structure remains consistent or meets defined
10
10
expectations.
11
11
12
-
The YAML format describes the hierarchical structure of accessible elements on the page, detailing **roles**,
13
-
**attributes**, **values**, and **text content**. The structure follows a tree-like syntax, where each node represents
14
-
an accessible element, and indentation indicates nested elements.
12
+
The YAML format describes the hierarchical structure of accessible elements on the page, detailing **roles**,**attributes**, **values**, and **text content**.
13
+
The structure follows a tree-like syntax, where each node represents an accessible element, and indentation indicates
14
+
nested elements.
15
15
16
-
Following is a simple example of an accessibility snapshot for the playwright.dev homepage:
16
+
Following is a simple example of an aria snapshot for the playwright.dev homepage:
17
17
18
18
```yaml
19
19
- banner:
@@ -32,19 +32,19 @@ Each accessible element in the tree is represented as a YAML node:
32
32
```
33
33
34
34
- **role**: Specifies the ARIA or HTML role of the element (e.g., `heading`, `list`, `listitem`, `button`).
35
-
- **"name"**: Accessible name of the element. Quoted strings indicate exact values, or regular expression.
35
+
- **"name"**: Accessible name of the element. Quoted strings indicate exact values, `/patterns/` are used for regular expression.
36
36
- **[attribute=value]**: Attributes and values, in square brackets, represent specific ARIA attributes, such
37
37
as `checked`, `disabled`, `expanded`, `level`, `pressed`, or `selected`.
38
38
39
39
These values are derived from ARIA attributes or calculated based on HTML semantics. To inspect the accessibility tree
40
40
structure of a page, use the [Chrome DevTools Accessibility Pane](https://developer.chrome.com/docs/devtools/accessibility/reference#pane).
41
41
42
42
43
-
## Snapshot Matching
43
+
## Snapshot matching
44
44
45
45
The [`method: LocatorAssertions.toMatchAriaSnapshot`] assertion method in Playwright compares the accessible
46
-
structure of a page with a predefined accessibility snapshot template, helping validate the page's accessibility
47
-
state against testing requirements.
46
+
structure of the locator scope with a predefined aria snapshot template, helping validate the page's state against
47
+
testing requirements.
48
48
49
49
For the following DOM:
50
50
@@ -93,7 +93,7 @@ When matching, the snapshot template is compared to the current accessibility tr
93
93
page's accessibility tree.
94
94
95
95
96
-
### Partial Matching
96
+
### Partial matching
97
97
98
98
You can perform partial matches on nodes by omitting attributes or accessible names, enabling verification of specific
99
99
parts of the accessibility tree without requiring exact matches. This flexibility is helpful for dynamic or irrelevant
@@ -103,9 +103,9 @@ attributes.
103
103
<button>Submit</button>
104
104
```
105
105
106
+
*aria snapshot*
106
107
107
108
```yaml
108
-
# accessibility snapshot
109
109
- button
110
110
```
111
111
@@ -119,10 +119,9 @@ focusing solely on role and hierarchy.
119
119
120
120
```html
121
121
<input type="checkbox" checked>
122
-
<input type="checkbox">
123
122
```
124
123
125
-
*accessibility tree for partial match*
124
+
*aria snapshot for partial match*
126
125
127
126
```yaml
128
127
- checkbox
@@ -142,17 +141,17 @@ Similarly, you can partially match children in lists or groups by omitting speci
142
141
</ul>
143
142
```
144
143
145
-
*accessibility tree for partial match*
144
+
*aria snapshot for partial match*
146
145
147
146
```yaml
148
147
- list
149
148
- listitem: Feature B
150
149
```
151
150
152
-
Partial matches let you create flexible accessibility tests that verify essential page structure without enforcing
151
+
Partial matches let you create flexible snapshot tests that verify essential page structure without enforcing
153
152
specific content or attributes.
154
153
155
-
### Matching with Regular Expressions
154
+
### Matching with regular expressions
156
155
157
156
Regular expressions allow flexible matching for elements with dynamic or variable text. Accessible names and text can
158
157
support regex patterns.
@@ -161,19 +160,64 @@ support regex patterns.
161
160
<h1>Issues 12</h1>
162
161
```
163
162
164
-
*accessibility tree with regular expression*
163
+
*aria snapshot with regular expression*
165
164
166
165
```yaml
167
166
- heading /Issues \d+/
168
167
```
169
168
169
+
## Generating snapshots
170
170
171
-
## Generating Snapshots
172
-
173
-
Creating accessibility snapshots in Playwright helps ensure and maintain your application’s structure.
171
+
Creating aria snapshots in Playwright helps ensure and maintain your application’s structure.
174
172
You can generate snapshots in various ways depending on your testing setup and workflow.
175
173
176
-
### 1. Using the `Locator.ariaSnapshot` Method
174
+
### 1. Generating snapshots with the Playwright code generator
175
+
176
+
If you’re using Playwright’s [Code Generator](./codegen.md), generating aria snapshots is streamlined with its
177
+
interactive interface:
178
+
179
+
- **"Assert snapshot" Action**: In the code generator, you can use the "Assert snapshot" action to automatically create
180
+
a snapshot assertion for the selected elements. This is a quick way to capture the aria snapshot as part of your
181
+
recorded test flow.
182
+
183
+
- **"Aria snapshot" Tab**: The "Aria snapshot" tab within the code generator interface visually represents the
184
+
aria snapshot for a selected locator, letting you explore, inspect, and verify element roles, attributes, and
185
+
accessible names to aid snapshot creation and review.
186
+
187
+
### 2. Updating snapshots with `@playwright/test` and the `--update-snapshots` flag
188
+
189
+
When using the Playwright test runner (`@playwright/test`), you can automatically update snapshots by running tests with
190
+
the `--update-snapshots` flag:
191
+
192
+
```bash
193
+
npx playwright test --update-snapshots
194
+
```
195
+
196
+
This command regenerates snapshots for assertions, including aria snapshots, replacing outdated ones. It’s
197
+
useful when application structure changes require new snapshots as a baseline. Note that Playwright will wait for the
198
+
maximum expect timeout specified in the test runner configuration to ensure the
199
+
page is settled before taking the snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
200
+
while generating snapshots.
201
+
202
+
#### Empty template for snapshot generation
203
+
204
+
Passing an empty string as the template in an assertion generates a snapshot on-the-fly:
205
+
206
+
```js
207
+
await expect(locator).toMatchAriaSnapshot('');
208
+
```
209
+
210
+
Note that Playwright will wait for the maximum expect timeout specified in the test runner configuration to ensure the
211
+
page is settled before taking the snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
212
+
while generating snapshots.
213
+
214
+
#### Snapshot patch files
215
+
216
+
When updating snapshots, Playwright creates patch files that capture differences. These patch files can be reviewed,
217
+
applied, and committed to source control, allowing teams to track structural changes over time and ensure updates are
218
+
consistent with application requirements.
219
+
220
+
### 3. Using the `Locator.ariaSnapshot` method
177
221
178
222
The [`method: Locator.ariaSnapshot`] method allows you to programmatically create a YAML representation of accessible
179
223
elements within a locator’s scope, especially helpful for generating snapshots dynamically during test execution.
@@ -205,59 +249,12 @@ var snapshot = await page.Locator("body").AriaSnapshotAsync();
205
249
Console.WriteLine(snapshot);
206
250
```
207
251
208
-
This command outputs the accessibility tree within the specified locator’s scope in YAML format, which you can validate
252
+
This command outputs the aria snapshot within the specified locator’s scope in YAML format, which you can validate
209
253
or store as needed.
210
254
211
-
### 2. Generating Snapshots with the Playwright Code Generator
212
-
213
-
If you’re using Playwright’s [Code Generator](./codegen.md), generating accessibility snapshots is streamlined with its
214
-
interactive interface:
215
-
216
-
- **"Assert Snapshot" Action**: In the code generator, you can select elements and use the "Assert snapshot" action to
217
-
automatically create a snapshot assertion for those elements. This is a quick way to capture the accessibility structure
218
-
as part of your recorded test flow.
219
-
220
-
- **"Accessibility" Tab**: The "Accessibility" tab within the code generator interface visually represents the
221
-
accessibility tree for a selected locator, letting you explore, inspect, and verify element roles, attributes, and
222
-
accessible names to aid snapshot creation and review.
223
-
224
-
### 3. Updating Snapshots with `@playwright/test` and the `--update-snapshots` Flag
225
-
* langs: js
226
-
227
-
When using the Playwright test runner (`@playwright/test`), you can automatically update snapshots by running tests with
228
-
the `--update-snapshots` flag:
229
-
230
-
```bash
231
-
npx playwright test --update-snapshots
232
-
```
233
-
234
-
This command regenerates snapshots for assertions, including accessibility snapshots, replacing outdated ones. It’s
235
-
useful when application structure changes require new snapshots as a baseline. Note that Playwright will wait for the
236
-
maximum timeout specified in the test runner configuration to ensure the page is fully loaded before taking the
237
-
snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
238
-
while generating snapshots.
239
-
240
-
#### Empty Template for Snapshot Generation
241
-
242
-
Passing an empty string as the template in an assertion generates a snapshot on-the-fly:
243
-
244
-
```js
245
-
await expect(locator).toMatchAriaSnapshot('');
246
-
```
247
-
248
-
Note that Playwright will wait for the maximum timeout specified in the test runner configuration to ensure the page is
249
-
fully loaded before taking the snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
250
-
while generating snapshots.
251
-
252
-
#### Snapshot Patch Files
253
-
254
-
When updating snapshots, Playwright creates patch files that capture differences. These patch files can be reviewed,
255
-
approved, and committed to source control, allowing teams to track structural changes over time and ensure updates are
256
-
consistent with application requirements.
257
-
258
-
## Accessibility Tree Examples
255
+
## Accessibility tree examples
259
256
260
-
### Headings with Level Attributes
257
+
### Headings with level attributes
261
258
262
259
Headings can include a `level` attribute indicating their heading level.
263
260
@@ -266,36 +263,36 @@ Headings can include a `level` attribute indicating their heading level.
266
263
<h2>Subtitle</h2>
267
264
```
268
265
269
-
*accessibility tree*
266
+
*aria snapshot*
270
267
271
268
```yaml
272
269
- heading "Title" [level=1]
273
270
- heading "Subtitle" [level=2]
274
271
```
275
272
276
-
### Text Nodes
273
+
### Text nodes
277
274
278
275
Standalone or descriptive text elements appear as text nodes.
279
276
280
277
```html
281
278
<div>Sample accessible name</div>
282
279
```
283
280
284
-
*accessibility tree*
281
+
*aria snapshot*
285
282
286
283
```yaml
287
284
- text: Sample accessible name
288
285
```
289
286
290
-
### Inline Multiline Text
287
+
### Inline multiline text
291
288
292
-
Multiline text, such as paragraphs, is normalized in the accessibility tree.
289
+
Multiline text, such as paragraphs, is normalized in the aria snapshot.
293
290
294
291
```html
295
292
<p>Line 1<br>Line 2</p>
296
293
```
297
294
298
-
*accessibility tree*
295
+
*aria snapshot*
299
296
300
297
```yaml
301
298
- paragraph: Line 1 Line 2
@@ -309,7 +306,7 @@ Links display their text or composed content from pseudo-elements.
309
306
<a href="#more-info">Read more about Accessibility</a>
310
307
```
311
308
312
-
*accessibility tree*
309
+
*aria snapshot*
313
310
314
311
```yaml
315
312
- link "Read more about Accessibility"
@@ -323,13 +320,13 @@ Input elements of type `text` show their `value` attribute content.
323
320
<input type="text" value="Enter your name">
324
321
```
325
322
326
-
*accessibility tree*
323
+
*aria snapshot*
327
324
328
325
```yaml
329
326
- textbox: Enter your name
330
327
```
331
328
332
-
### Lists with Items
329
+
### Lists with items
333
330
334
331
Ordered and unordered lists include their list items.
335
332
@@ -340,15 +337,15 @@ Ordered and unordered lists include their list items.
340
337
</ul>
341
338
```
342
339
343
-
*accessibility tree*
340
+
*aria snapshot*
344
341
345
342
```yaml
346
343
- list "Main Features":
347
344
- listitem: Feature 1
348
345
- listitem: Feature 2
349
346
```
350
347
351
-
### Grouped Elements
348
+
### Grouped elements
352
349
353
350
Groups capture nested elements, such as `<details>` elements with summary content.
354
351
@@ -359,36 +356,36 @@ Groups capture nested elements, such as `<details>` elements with summary conten
359
356
</details>
360
357
```
361
358
362
-
*accessibility tree*
359
+
*aria snapshot*
363
360
364
361
```yaml
365
362
- group: Summary
366
363
```
367
364
368
-
### Attributes and States
365
+
### Attributes and states
369
366
370
367
Commonly used ARIA attributes, like `checked`, `disabled`, `expanded`, `level`, `pressed`, and `selected`, represent
0 commit comments