Skip to content

Commit 6be7eeb

Browse files
committed
Add Testing Library section to Playwright page
1 parent ad27f95 commit 6be7eeb

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed
167 KB
Loading

lib/components_guide_web/templates/accessibility_first/accessibility-tree-snapshots.html.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# Accessibility Tree Snapshots
1+
# Accessibility Testing in Playwright
2+
3+
## Accessibility tree snapshots
24

35
I usually don’t like snapshot “tests” because they don’t really test anything. Instead of documenting behaviour you want demonstrated, they take a shortcut and just save a dump of everything to disk. Also they usually capture implementation details (like the HTML tree) that should be allowed to vary as you refactor — adding or removing whitespace or a `<div>` shouldn’t affect a test.
46

@@ -21,6 +23,17 @@ test.describe("accessibility", () => {
2123
});
2224
```
2325

26+
Here’s how to install Playwright in a Node.js project and run the tests:
27+
28+
```bash
29+
# Add Playwright as a dev dependency
30+
npm i -D @playwright/test
31+
# install supported browsers
32+
npx playwright install
33+
# Run the tests
34+
npx playwright test
35+
```
36+
2437
Here’s what the tree looks like:
2538

2639
```json
@@ -53,3 +66,69 @@ Here’s what the tree looks like:
5366
]
5467
}
5568
```
69+
70+
If you make changes to your app implementation and run the tests again, you’ll see a helpful diff like so:
71+
72+
<collected-figure image="accessibility-tree-snapshot-failure-diff">
73+
Diff comparing the accessibility tree after making changes to the components.guide home page.
74+
</collected-figure>
75+
76+
Once you have checked the diff of the accessibility tree and are happy with the results, re-run the tests and pass the option to update the snapshots:
77+
78+
```bash
79+
npx playwright test -u
80+
81+
# Or if you prefer the longer form:
82+
# npx playwright test --update-snapshots
83+
```
84+
85+
----
86+
87+
## Asserting landmarks exist with Testing Library
88+
89+
If you’d like to go beyond snapshots, you can write tests that assert that specific [landmarks](/accessibility-first/landmarks) exist.
90+
91+
Here’s a test that checks for landmarks like `main`, `banner`, a `navigation` named “Main menu”, and a `search` form inside the `banner` are all visible on the page. These tests are run against the excellent MDN home page.
92+
93+
```ts
94+
// tests/mdn.spec.ts
95+
import { test, within } from "./helpers";
96+
97+
test.beforeEach(async ({ page }) => {
98+
await page.goto("https://developer.mozilla.org/en-US/");
99+
});
100+
101+
test.describe("landmarks", () => {
102+
test("has main nav", async ({ queries: { getByRole } }) => {
103+
await getByRole('navigation', { name: 'Main menu' });
104+
});
105+
106+
test("has main landmark", async ({ queries: { getByRole } }) => {
107+
await getByRole('main');
108+
});
109+
110+
test("has banner landmark", async ({ queries: { getByRole } }) => {
111+
await getByRole('banner');
112+
});
113+
114+
test("has search form inside banner", async ({ queries: { getByRole } }) => {
115+
const banner = await getByRole('banner');
116+
await within(banner).getByRole('search');
117+
});
118+
});
119+
```
120+
121+
And here’s the boilerplate in `helpers.ts` to get Testing Library and Playwright working together:
122+
123+
```ts
124+
// tests/helpers.ts
125+
import { test as baseTest } from "@playwright/test";
126+
import {
127+
fixtures,
128+
type TestingLibraryFixtures,
129+
} from "@playwright-testing-library/test/fixture";
130+
export { within } from "@playwright-testing-library/test";
131+
132+
export const test = baseTest.extend<TestingLibraryFixtures>(fixtures);
133+
export const { expect } = test;
134+
```

0 commit comments

Comments
 (0)