Skip to content

Commit

Permalink
feat(roll): roll to ToT Playwright (roll/next-06-11-24) (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
playwrightmachine authored Nov 6, 2024
1 parent 2a5d1c1 commit a0fa9d9
Show file tree
Hide file tree
Showing 16 changed files with 271 additions and 219 deletions.
2 changes: 1 addition & 1 deletion dotnet/docs/api/class-locator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ var button = page.GetByRole(AriaRole.Button).And(page.GetByTitle("Subscribe"));

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locator.AriaSnapshotAsync</x-search>

Captures the aria snapshot of the given element. Read more about [accessibility snapshots](../aria-snapshot.mdx) and [Expect(Locator).ToMatchAriaSnapshotAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) for the corresponding assertion.
Captures the aria snapshot of the given element. Read more about [aria snapshots](../aria-snapshots.mdx) and [Expect(Locator).ToMatchAriaSnapshotAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) for the corresponding assertion.

**Usage**

Expand Down
2 changes: 1 addition & 1 deletion dotnet/docs/api/class-locatorassertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ await Expect(locator).ToHaveValuesAsync(new Regex[] { new Regex("R"), new Regex(

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locatorAssertions.ToMatchAriaSnapshotAsync</x-search>

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

**Usage**

Expand Down
114 changes: 66 additions & 48 deletions dotnet/docs/aria-snapshot.mdx → dotnet/docs/aria-snapshots.mdx
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
---
id: aria-snapshot
title: "Accessibility Snapshots"
id: aria-snapshots
title: "Aria snapshots"
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import HTMLCard from '@site/src/components/HTMLCard';

## Overview

In Playwright, accessibility snapshots provide a YAML representation of the accessible elements on a page. These snapshots can be stored and compared later to verify if the page structure remains consistent or meets defined expectations.
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**,
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.

**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 @@ -33,14 +31,14 @@ 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 [Expect(Locator).ToMatchAriaSnapshotAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) 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.
The [Expect(Locator).ToMatchAriaSnapshotAsync()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) assertion method in Playwright compares the accessible 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 All @@ -61,16 +59,17 @@ When matching, the snapshot template is compared to the current accessibility tr
* The comparison is case-sensitive and collapses whitespace, so indentation and line breaks are ignored.
* The comparison is order-sensitive, meaning the order of elements in the snapshot template must match the order in the 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 attributes.

```html
<button>Submit</button>
```

*aria snapshot*

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

Expand All @@ -82,10 +81,9 @@ For elements with ARIA attributes like `checked` or `disabled`, omitting these a

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

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

```yaml
- checkbox
Expand All @@ -105,34 +103,60 @@ 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 specific content or attributes.
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.

```html
<h1>Issues 12</h1>
```

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

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

## Generating Snapshots
## Generating snapshots

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. Generating snapshots with the Playwright code generator

If you’re using Playwright’s [Code Generator](./codegen.mdx), 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:

Creating accessibility 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.
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.

### 1. Using the `Locator.ariaSnapshot` Method
#### 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 [Locator.AriaSnapshotAsync()](/api/class-locator.mdx#locator-aria-snapshot) 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 All @@ -143,17 +167,11 @@ 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 or store as needed.

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

If you’re using Playwright’s [Code Generator](./codegen.mdx), 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.
This command outputs the aria snapshot within the specified locator’s scope in YAML format, which you can validate or store as needed.

## 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 @@ -162,36 +180,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 @@ -205,7 +223,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 @@ -219,13 +237,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 @@ -236,15 +254,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 @@ -255,35 +273,35 @@ 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: 1 addition & 1 deletion dotnet/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
type: 'category',
label: 'Guides',
items: [
{ type: 'doc', id: 'aria-snapshot' },
{ type: 'doc', id: 'aria-snapshots' },
{ type: 'doc', id: 'input' },
{ type: 'doc', id: 'actionability' },
{ type: 'doc', id: 'api-testing' },
Expand Down
2 changes: 1 addition & 1 deletion java/docs/api/class-locator.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Locator button = page.getByRole(AriaRole.BUTTON).and(page.getByTitle("Subscribe"

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locator.ariaSnapshot</x-search>

Captures the aria snapshot of the given element. Read more about [accessibility snapshots](../aria-snapshot.mdx) and [assertThat(locator).matchesAriaSnapshot()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) for the corresponding assertion.
Captures the aria snapshot of the given element. Read more about [aria snapshots](../aria-snapshots.mdx) and [assertThat(locator).matchesAriaSnapshot()](/api/class-locatorassertions.mdx#locator-assertions-to-match-aria-snapshot) for the corresponding assertion.

**Usage**

Expand Down
2 changes: 1 addition & 1 deletion java/docs/api/class-locatorassertions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ assertThat(

<font size="2" style={{position: "relative", top: "-20px"}}>Added in: v1.49</font><x-search>locatorAssertions.matchesAriaSnapshot</x-search>

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

**Usage**

Expand Down
Loading

0 comments on commit a0fa9d9

Please sign in to comment.