Skip to content

Commit a655b0b

Browse files
authored
chrome: aria api review (#33458)
1 parent 697d7a4 commit a655b0b

File tree

12 files changed

+138
-102
lines changed

12 files changed

+138
-102
lines changed

docs/src/api/class-locator.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ Additional locator to match.
155155
- returns: <[string]>
156156

157157
Captures the aria snapshot of the given element.
158-
Read more about [accessibility snapshots](../aria-snapshot.md) and [`method: LocatorAssertions.toMatchAriaSnapshot`] for the corresponding assertion.
158+
Read more about [aria snapshots](../aria-snapshots.md) and [`method: LocatorAssertions.toMatchAriaSnapshot`] for the corresponding assertion.
159159

160160
**Usage**
161161

docs/src/api/class-locatorassertions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2109,7 +2109,7 @@ Expected options currently selected.
21092109
* langs:
21102110
- alias-java: matchesAriaSnapshot
21112111

2112-
Asserts that the target element matches the given [accessibility snapshot](../aria-snapshot.md).
2112+
Asserts that the target element matches the given [accessibility snapshot](../aria-snapshots.md).
21132113

21142114
**Usage**
21152115

docs/src/aria-snapshot.md renamed to docs/src/aria-snapshots.md

Lines changed: 88 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
---
2-
id: aria-snapshot
3-
title: "Accessibility Snapshots"
2+
id: aria-snapshots
3+
title: "Aria snapshots"
44
---
55

66
## Overview
77

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.
99
These snapshots can be stored and compared later to verify if the page structure remains consistent or meets defined
1010
expectations.
1111

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.
1515

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:
1717

1818
```yaml
1919
- banner:
@@ -32,19 +32,19 @@ Each accessible element in the tree is represented as a YAML node:
3232
```
3333
3434
- **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.
3636
- **[attribute=value]**: Attributes and values, in square brackets, represent specific ARIA attributes, such
3737
as `checked`, `disabled`, `expanded`, `level`, `pressed`, or `selected`.
3838

3939
These values are derived from ARIA attributes or calculated based on HTML semantics. To inspect the accessibility tree
4040
structure of a page, use the [Chrome DevTools Accessibility Pane](https://developer.chrome.com/docs/devtools/accessibility/reference#pane).
4141

4242

43-
## Snapshot Matching
43+
## Snapshot matching
4444

4545
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.
4848

4949
For the following DOM:
5050

@@ -93,7 +93,7 @@ When matching, the snapshot template is compared to the current accessibility tr
9393
page's accessibility tree.
9494

9595

96-
### Partial Matching
96+
### Partial matching
9797

9898
You can perform partial matches on nodes by omitting attributes or accessible names, enabling verification of specific
9999
parts of the accessibility tree without requiring exact matches. This flexibility is helpful for dynamic or irrelevant
@@ -103,9 +103,9 @@ attributes.
103103
<button>Submit</button>
104104
```
105105

106+
*aria snapshot*
106107

107108
```yaml
108-
# accessibility snapshot
109109
- button
110110
```
111111

@@ -119,10 +119,9 @@ focusing solely on role and hierarchy.
119119

120120
```html
121121
<input type="checkbox" checked>
122-
<input type="checkbox">
123122
```
124123

125-
*accessibility tree for partial match*
124+
*aria snapshot for partial match*
126125

127126
```yaml
128127
- checkbox
@@ -142,17 +141,17 @@ Similarly, you can partially match children in lists or groups by omitting speci
142141
</ul>
143142
```
144143

145-
*accessibility tree for partial match*
144+
*aria snapshot for partial match*
146145

147146
```yaml
148147
- list
149148
- listitem: Feature B
150149
```
151150

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
153152
specific content or attributes.
154153

155-
### Matching with Regular Expressions
154+
### Matching with regular expressions
156155

157156
Regular expressions allow flexible matching for elements with dynamic or variable text. Accessible names and text can
158157
support regex patterns.
@@ -161,19 +160,64 @@ support regex patterns.
161160
<h1>Issues 12</h1>
162161
```
163162

164-
*accessibility tree with regular expression*
163+
*aria snapshot with regular expression*
165164

166165
```yaml
167166
- heading /Issues \d+/
168167
```
169168

169+
## Generating snapshots
170170

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.
174172
You can generate snapshots in various ways depending on your testing setup and workflow.
175173

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
177221

178222
The [`method: Locator.ariaSnapshot`] method allows you to programmatically create a YAML representation of accessible
179223
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();
205249
Console.WriteLine(snapshot);
206250
```
207251

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
209253
or store as needed.
210254

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
259256

260-
### Headings with Level Attributes
257+
### Headings with level attributes
261258

262259
Headings can include a `level` attribute indicating their heading level.
263260

@@ -266,36 +263,36 @@ Headings can include a `level` attribute indicating their heading level.
266263
<h2>Subtitle</h2>
267264
```
268265

269-
*accessibility tree*
266+
*aria snapshot*
270267

271268
```yaml
272269
- heading "Title" [level=1]
273270
- heading "Subtitle" [level=2]
274271
```
275272

276-
### Text Nodes
273+
### Text nodes
277274

278275
Standalone or descriptive text elements appear as text nodes.
279276

280277
```html
281278
<div>Sample accessible name</div>
282279
```
283280

284-
*accessibility tree*
281+
*aria snapshot*
285282

286283
```yaml
287284
- text: Sample accessible name
288285
```
289286

290-
### Inline Multiline Text
287+
### Inline multiline text
291288

292-
Multiline text, such as paragraphs, is normalized in the accessibility tree.
289+
Multiline text, such as paragraphs, is normalized in the aria snapshot.
293290

294291
```html
295292
<p>Line 1<br>Line 2</p>
296293
```
297294

298-
*accessibility tree*
295+
*aria snapshot*
299296

300297
```yaml
301298
- paragraph: Line 1 Line 2
@@ -309,7 +306,7 @@ Links display their text or composed content from pseudo-elements.
309306
<a href="#more-info">Read more about Accessibility</a>
310307
```
311308

312-
*accessibility tree*
309+
*aria snapshot*
313310

314311
```yaml
315312
- link "Read more about Accessibility"
@@ -323,13 +320,13 @@ Input elements of type `text` show their `value` attribute content.
323320
<input type="text" value="Enter your name">
324321
```
325322

326-
*accessibility tree*
323+
*aria snapshot*
327324

328325
```yaml
329326
- textbox: Enter your name
330327
```
331328

332-
### Lists with Items
329+
### Lists with items
333330

334331
Ordered and unordered lists include their list items.
335332

@@ -340,15 +337,15 @@ Ordered and unordered lists include their list items.
340337
</ul>
341338
```
342339

343-
*accessibility tree*
340+
*aria snapshot*
344341

345342
```yaml
346343
- list "Main Features":
347344
- listitem: Feature 1
348345
- listitem: Feature 2
349346
```
350347

351-
### Grouped Elements
348+
### Grouped elements
352349

353350
Groups capture nested elements, such as `<details>` elements with summary content.
354351

@@ -359,36 +356,36 @@ Groups capture nested elements, such as `<details>` elements with summary conten
359356
</details>
360357
```
361358

362-
*accessibility tree*
359+
*aria snapshot*
363360

364361
```yaml
365362
- group: Summary
366363
```
367364

368-
### Attributes and States
365+
### Attributes and states
369366

370367
Commonly used ARIA attributes, like `checked`, `disabled`, `expanded`, `level`, `pressed`, and `selected`, represent
371368
control states.
372369

373-
#### Checkbox with `checked` Attribute
370+
#### Checkbox with `checked` attribute
374371

375372
```html
376373
<input type="checkbox" checked>
377374
```
378375

379-
*accessibility tree*
376+
*aria snapshot*
380377

381378
```yaml
382-
- checkbox [checked=true]
379+
- checkbox [checked]
383380
```
384381

385-
#### Button with `pressed` Attribute
382+
#### Button with `pressed` attribute
386383

387384
```html
388385
<button aria-pressed="true">Toggle</button>
389386
```
390387

391-
*accessibility tree*
388+
*aria snapshot*
392389

393390
```yaml
394391
- button "Toggle" [pressed=true]

packages/playwright-core/src/server/frames.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,5 +1828,7 @@ function renderUnexpectedValue(expression: string, received: any): string {
18281828
return received ? 'empty' : 'not empty';
18291829
if (expression === 'to.be.focused')
18301830
return received ? 'focused' : 'not focused';
1831+
if (expression === 'to.match.aria')
1832+
return received ? received.raw : received;
18311833
return received;
18321834
}

0 commit comments

Comments
 (0)