Skip to content

Commit 1eaa648

Browse files
authored
Use document.title as fallback accessible name for html root element (#446)
1 parent eb364cc commit 1eaa648

3 files changed

Lines changed: 55 additions & 43 deletions

File tree

.changeset/popular-beans-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'pleasantest': major
3+
---
4+
5+
Use document.title as fallback implicit accessible name for html root element in accessibility tree snapshots

src/accessibility/browser.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ export const getAccessibilityTree = (
101101
);
102102
let text = (selfIsInAccessibilityTree && role) || '';
103103
if (selfIsInAccessibilityTree) {
104-
const name = computeAccessibleName(element);
104+
let name = computeAccessibleName(element);
105+
if (
106+
element === document.documentElement &&
107+
role === 'document' &&
108+
!name &&
109+
document.title
110+
) {
111+
name = document.title;
112+
}
105113
if (name) text += ` "${name}"`;
106114
if (
107115
element.ariaExpanded === 'true' ||

tests/accessibility/getAccessibilityTree.test.ts

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ test(
1212
expect(String(await getAccessibilityTree(htmlElement))).toEqual(
1313
String(await getAccessibilityTree(page)),
1414
);
15-
// TODO: document's name should be from document.title (breaking change)
1615
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
17-
document
16+
document "example title"
1817
list
1918
`);
2019

@@ -40,7 +39,7 @@ test(
4039
</main>
4140
`);
4241
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
43-
document
42+
document "pleasantest"
4443
main
4544
button "Add to cart"
4645
heading "hiiii"
@@ -54,7 +53,7 @@ test(
5453
</ul>
5554
`);
5655
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
57-
document
56+
document "pleasantest"
5857
list
5958
listitem
6059
text "something"
@@ -63,21 +62,21 @@ test(
6362
`);
6463
expect(await getAccessibilityTree(page, { includeText: true }))
6564
.toMatchInlineSnapshot(`
66-
document
67-
list
68-
listitem
69-
text "something"
70-
listitem
71-
text "something else"
72-
`);
65+
document "pleasantest"
66+
list
67+
listitem
68+
text "something"
69+
listitem
70+
text "something else"
71+
`);
7372
await utils.injectHTML(`
7473
<button aria-describedby="click-me-description">click me</button>
7574
<button aria-describedby="click-me-description"><div>click me</div></button>
7675
<button aria-describedby="click-me-description"><h1>click me</h1></button>
7776
<div id="click-me-description">extended description</div>
7877
`);
7978
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
80-
document
79+
document "pleasantest"
8180
button "click me"
8281
↳ description: "extended description"
8382
button "click me"
@@ -88,24 +87,24 @@ test(
8887
`);
8988
expect(await getAccessibilityTree(page, { includeText: true }))
9089
.toMatchInlineSnapshot(`
91-
document
92-
button "click me"
93-
↳ description: "extended description"
94-
button "click me"
95-
↳ description: "extended description"
96-
button "click me"
97-
↳ description: "extended description"
98-
text "extended description"
99-
`);
90+
document "pleasantest"
91+
button "click me"
92+
↳ description: "extended description"
93+
button "click me"
94+
↳ description: "extended description"
95+
button "click me"
96+
↳ description: "extended description"
97+
text "extended description"
98+
`);
10099

101100
expect(await getAccessibilityTree(page, { includeDescriptions: false }))
102101
.toMatchInlineSnapshot(`
103-
document
104-
button "click me"
105-
button "click me"
106-
button "click me"
107-
text "extended description"
108-
`);
102+
document "pleasantest"
103+
button "click me"
104+
button "click me"
105+
button "click me"
106+
text "extended description"
107+
`);
109108

110109
await utils.injectHTML(`
111110
<label>
@@ -119,12 +118,12 @@ test(
119118

120119
expect(await getAccessibilityTree(page, { includeText: true }))
121120
.toMatchInlineSnapshot(`
122-
document
123-
text "Label Text"
124-
textbox "Label Text"
125-
text "Label Text"
126-
textbox "Label Text"
127-
`);
121+
document "pleasantest"
122+
text "Label Text"
123+
textbox "Label Text"
124+
text "Label Text"
125+
textbox "Label Text"
126+
`);
128127
}),
129128
);
130129

@@ -157,7 +156,7 @@ test(
157156
</div>
158157
`);
159158
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
160-
document
159+
document "pleasantest"
161160
button "A"
162161
button "E"
163162
button "G"
@@ -174,14 +173,14 @@ test(
174173
// Role="presentation" and role="none" are equivalent
175174
// They make it as if the outer element wasn't there.
176175
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
177-
document
176+
document "pleasantest"
178177
text "Sample Content"
179178
`);
180179
await utils.injectHTML(`<h1 role="none">Sample Content</h1>`);
181180
// Role="presentation" and role="none" are equivalent
182181
// They make it as if the outer element wasn't there.
183182
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
184-
document
183+
document "pleasantest"
185184
text "Sample Content"
186185
`);
187186

@@ -198,7 +197,7 @@ test(
198197
</ul>
199198
`);
200199
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
201-
document
200+
document "pleasantest"
202201
text "Sample Content"
203202
text "More Sample Content"
204203
heading "Hi"
@@ -216,7 +215,7 @@ test(
216215
</ul>
217216
`);
218217
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
219-
document
218+
document "pleasantest"
220219
text "Sample Content"
221220
text "More Sample Content"
222221
listitem
@@ -233,7 +232,7 @@ test(
233232
</ul>
234233
`);
235234
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
236-
document
235+
document "pleasantest"
237236
text "Sample Content"
238237
text "More Sample Content"
239238
heading "Hi"
@@ -250,7 +249,7 @@ test(
250249
</ul>
251250
`);
252251
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
253-
document
252+
document "pleasantest"
254253
heading "Sample Content"
255254
listitem
256255
text "Sample Content"
@@ -269,14 +268,14 @@ test(
269268
`);
270269

271270
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
272-
document
271+
document "pleasantest"
273272
button "Click me!"
274273
`);
275274

276275
await user.click(await screen.getByRole('button'));
277276

278277
expect(await getAccessibilityTree(page)).toMatchInlineSnapshot(`
279-
document
278+
document "pleasantest"
280279
button "Click me!" (focused)
281280
`);
282281
}),

0 commit comments

Comments
 (0)