Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrome: aria api review #33458

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/api/class-locator.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ Additional locator to match.
- returns: <[string]>

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

**Usage**

Expand Down
2 changes: 1 addition & 1 deletion docs/src/api/class-locatorassertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2109,7 +2109,7 @@ Expected options currently selected.
* langs:
- alias-java: matchesAriaSnapshot

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

**Usage**

Expand Down
179 changes: 88 additions & 91 deletions docs/src/aria-snapshot.md → docs/src/aria-snapshots.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
---
id: aria-snapshot
title: "Accessibility Snapshots"
id: aria-snapshots
title: "Aria snapshots"
---

## Overview

In Playwright, accessibility snapshots provide a YAML representation of the accessible elements on a page.
In Playwright, aria snapshots provide a YAML representation of the accessibility tree of a page.
These snapshots can be stored and compared later to verify if the page structure remains consistent or meets defined
expectations.

The YAML format describes the hierarchical structure of accessible elements on the page, detailing **roles**,
**attributes**, **values**, and **text content**. The structure follows a tree-like syntax, where each node represents
an accessible element, and indentation indicates nested elements.
The YAML format describes the hierarchical structure of accessible elements on the page, detailing **roles**, **attributes**, **values**, and **text content**.
The structure follows a tree-like syntax, where each node represents an accessible element, and indentation indicates
nested elements.

Following is a simple example of an accessibility snapshot for the playwright.dev homepage:
Following is a simple example of an aria snapshot for the playwright.dev homepage:

```yaml
- banner:
Expand All @@ -32,19 +32,19 @@ Each accessible element in the tree is represented as a YAML node:
```

- **role**: Specifies the ARIA or HTML role of the element (e.g., `heading`, `list`, `listitem`, `button`).
- **"name"**: Accessible name of the element. Quoted strings indicate exact values, or regular expression.
- **"name"**: Accessible name of the element. Quoted strings indicate exact values, `/patterns/` are used for regular expression.
- **[attribute=value]**: Attributes and values, in square brackets, represent specific ARIA attributes, such
as `checked`, `disabled`, `expanded`, `level`, `pressed`, or `selected`.

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


## Snapshot Matching
## Snapshot matching

The [`method: LocatorAssertions.toMatchAriaSnapshot`] assertion method in Playwright compares the accessible
structure of a page with a predefined accessibility snapshot template, helping validate the page's accessibility
state against testing requirements.
structure of the locator scope with a predefined aria snapshot template, helping validate the page's state against
testing requirements.

For the following DOM:

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


### Partial Matching
### Partial matching

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

*aria snapshot*

```yaml
# accessibility snapshot
- button
```

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

```html
<input type="checkbox" checked>
<input type="checkbox">
```

*accessibility tree for partial match*
*aria snapshot for partial match*

```yaml
- checkbox
Expand All @@ -142,17 +141,17 @@ Similarly, you can partially match children in lists or groups by omitting speci
</ul>
```

*accessibility tree for partial match*
*aria snapshot for partial match*

```yaml
- list
- listitem: Feature B
```

Partial matches let you create flexible accessibility tests that verify essential page structure without enforcing
Partial matches let you create flexible snapshot tests that verify essential page structure without enforcing
specific content or attributes.

### Matching with Regular Expressions
### Matching with regular expressions

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

*accessibility tree with regular expression*
*aria snapshot with regular expression*

```yaml
- heading /Issues \d+/
```

## Generating snapshots

## Generating Snapshots

Creating accessibility snapshots in Playwright helps ensure and maintain your application’s structure.
Creating aria snapshots in Playwright helps ensure and maintain your application’s structure.
You can generate snapshots in various ways depending on your testing setup and workflow.

### 1. Using the `Locator.ariaSnapshot` Method
### 1. Generating snapshots with the Playwright code generator

If you’re using Playwright’s [Code Generator](./codegen.md), generating aria snapshots is streamlined with its
interactive interface:

- **"Assert snapshot" Action**: In the code generator, you can use the "Assert snapshot" action to automatically create
a snapshot assertion for the selected elements. This is a quick way to capture the aria snapshot as part of your
recorded test flow.

- **"Aria snapshot" Tab**: The "Aria snapshot" tab within the code generator interface visually represents the
aria snapshot for a selected locator, letting you explore, inspect, and verify element roles, attributes, and
accessible names to aid snapshot creation and review.

### 2. Updating snapshots with `@playwright/test` and the `--update-snapshots` flag

When using the Playwright test runner (`@playwright/test`), you can automatically update snapshots by running tests with
the `--update-snapshots` flag:

```bash
npx playwright test --update-snapshots
```

This command regenerates snapshots for assertions, including aria snapshots, replacing outdated ones. It’s
useful when application structure changes require new snapshots as a baseline. Note that Playwright will wait for the
maximum expect timeout specified in the test runner configuration to ensure the
page is settled before taking the snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
while generating snapshots.

#### Empty template for snapshot generation

Passing an empty string as the template in an assertion generates a snapshot on-the-fly:

```js
await expect(locator).toMatchAriaSnapshot('');
```

Note that Playwright will wait for the maximum expect timeout specified in the test runner configuration to ensure the
page is settled before taking the snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
while generating snapshots.

#### Snapshot patch files

When updating snapshots, Playwright creates patch files that capture differences. These patch files can be reviewed,
applied, and committed to source control, allowing teams to track structural changes over time and ensure updates are
consistent with application requirements.

### 3. Using the `Locator.ariaSnapshot` method

The [`method: Locator.ariaSnapshot`] method allows you to programmatically create a YAML representation of accessible
elements within a locator’s scope, especially helpful for generating snapshots dynamically during test execution.
Expand Down Expand Up @@ -205,59 +249,12 @@ var snapshot = await page.Locator("body").AriaSnapshotAsync();
Console.WriteLine(snapshot);
```

This command outputs the accessibility tree within the specified locator’s scope in YAML format, which you can validate
This command outputs the aria snapshot within the specified locator’s scope in YAML format, which you can validate
or store as needed.

### 2. Generating Snapshots with the Playwright Code Generator

If you’re using Playwright’s [Code Generator](./codegen.md), generating accessibility snapshots is streamlined with its
interactive interface:

- **"Assert Snapshot" Action**: In the code generator, you can select elements and use the "Assert snapshot" action to
automatically create a snapshot assertion for those elements. This is a quick way to capture the accessibility structure
as part of your recorded test flow.

- **"Accessibility" Tab**: The "Accessibility" tab within the code generator interface visually represents the
accessibility tree for a selected locator, letting you explore, inspect, and verify element roles, attributes, and
accessible names to aid snapshot creation and review.

### 3. Updating Snapshots with `@playwright/test` and the `--update-snapshots` Flag
* langs: js

When using the Playwright test runner (`@playwright/test`), you can automatically update snapshots by running tests with
the `--update-snapshots` flag:

```bash
npx playwright test --update-snapshots
```

This command regenerates snapshots for assertions, including accessibility snapshots, replacing outdated ones. It’s
useful when application structure changes require new snapshots as a baseline. Note that Playwright will wait for the
maximum timeout specified in the test runner configuration to ensure the page is fully loaded before taking the
snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
while generating snapshots.

#### Empty Template for Snapshot Generation

Passing an empty string as the template in an assertion generates a snapshot on-the-fly:

```js
await expect(locator).toMatchAriaSnapshot('');
```

Note that Playwright will wait for the maximum timeout specified in the test runner configuration to ensure the page is
fully loaded before taking the snapshot. It might be necessary to adjust the `--timeout` if the test hits the timeout
while generating snapshots.

#### Snapshot Patch Files

When updating snapshots, Playwright creates patch files that capture differences. These patch files can be reviewed,
approved, and committed to source control, allowing teams to track structural changes over time and ensure updates are
consistent with application requirements.

## Accessibility Tree Examples
## Accessibility tree examples

### Headings with Level Attributes
### Headings with level attributes

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

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

*accessibility tree*
*aria snapshot*

```yaml
- heading "Title" [level=1]
- heading "Subtitle" [level=2]
```

### Text Nodes
### Text nodes

Standalone or descriptive text elements appear as text nodes.

```html
<div>Sample accessible name</div>
```

*accessibility tree*
*aria snapshot*

```yaml
- text: Sample accessible name
```

### Inline Multiline Text
### Inline multiline text

Multiline text, such as paragraphs, is normalized in the accessibility tree.
Multiline text, such as paragraphs, is normalized in the aria snapshot.

```html
<p>Line 1<br>Line 2</p>
```

*accessibility tree*
*aria snapshot*

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

*accessibility tree*
*aria snapshot*

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

*accessibility tree*
*aria snapshot*

```yaml
- textbox: Enter your name
```

### Lists with Items
### Lists with items

Ordered and unordered lists include their list items.

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

*accessibility tree*
*aria snapshot*

```yaml
- list "Main Features":
- listitem: Feature 1
- listitem: Feature 2
```

### Grouped Elements
### Grouped elements

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

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

*accessibility tree*
*aria snapshot*

```yaml
- group: Summary
```

### Attributes and States
### Attributes and states

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

#### Checkbox with `checked` Attribute
#### Checkbox with `checked` attribute

```html
<input type="checkbox" checked>
```

*accessibility tree*
*aria snapshot*

```yaml
- checkbox [checked=true]
- checkbox [checked]
```

#### Button with `pressed` Attribute
#### Button with `pressed` attribute

```html
<button aria-pressed="true">Toggle</button>
```

*accessibility tree*
*aria snapshot*

```yaml
- button "Toggle" [pressed=true]
Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-core/src/server/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1828,5 +1828,7 @@ function renderUnexpectedValue(expression: string, received: any): string {
return received ? 'empty' : 'not empty';
if (expression === 'to.be.focused')
return received ? 'focused' : 'not focused';
if (expression === 'to.match.aria')
return received ? received.raw : received;
return received;
}
Loading