Skip to content

Commit c781173

Browse files
authored
fix(aria): fall back to aria-labelledby attribute without element reflection (#43722)
1 parent 217988d commit c781173

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

‎packages/injected/src/roleUtils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,8 @@ function parseCSSContentPropertyAsString(element: Element, content: string, isPs
566566
}
567567

568568
export function getAriaLabelledByElements(element: Element): Element[] | null {
569-
const refs = element.ariaLabelledByElements;
569+
// Reflected element references are not available in older browsers, e.g. Chromium < 135.
570+
const refs = element.ariaLabelledByElements || getIdRefs(element, element.getAttribute('aria-labelledby'));
570571
// step 2b:
571572
// "if the current node has an aria-labelledby attribute that contains at least one valid IDREF"
572573
// Therefore, if none of the refs match an element, we consider aria-labelledby to be missing.

‎tests/page/page-event-pageerror.spec.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { test as it, expect } from './pageTest';
1919

2020
it('ER-001 should fire', {
2121
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43450' },
22-
}, async ({ page, server, browserName, isBidi }) => {
22+
}, async ({ page, server, browserName, browserMajorVersion, isBidi }) => {
2323
const url = server.PREFIX + '/error.html';
2424
const [error] = await Promise.all([
2525
page.waitForEvent('pageerror'),
@@ -79,7 +79,7 @@ function c() {
7979
page.goto(server.PREFIX + '/cross-origin-error.html'),
8080
]);
8181
expect(crossOriginError.message).toBe('Fancy error!');
82-
if (browserName === 'chromium') {
82+
if (browserName === 'chromium' && browserMajorVersion >= 155) {
8383
expect(crossOriginError.stack).toBe(`Uncaught Error: Fancy error!
8484
at c (${crossOriginUrl}:14:11)
8585
at b (${crossOriginUrl}:10:5)

‎tests/page/selectors-role.spec.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ test('SE-059 should filter hidden, unless explicitly asked for', async ({ page }
435435

436436
test('SE-060 should support name', {
437437
annotation: { type: 'issue', description: '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/microsoft/playwright/issues/43549' },
438-
}, async ({ page }) => {
438+
}, async ({ page, browserName, browserMajorVersion }) => {
439439
await page.setContent(`
440440
<div role="button" aria-label=" Hello "></div>
441441
<div role="button" aria-label="Hallo"></div>
@@ -449,9 +449,6 @@ test('SE-060 should support name', {
449449

450450
const group = page.getByRole('group');
451451
await expect(group).toHaveAccessibleName('Chosen name');
452-
await group.evaluate(element => {
453-
element.ariaLabelledByElements = [document.querySelector('#title')!];
454-
});
455452

456453
expect(await page.locator(`role=button[name="Hello"]`).evaluateAll(els => els.map(e => e.outerHTML))).toEqual([
457454
`<div role="button" aria-label=" Hello "></div>`,
@@ -508,9 +505,16 @@ test('SE-060 should support name', {
508505
`<div role="button" aria-label="123" aria-hidden="true"></div>`,
509506
]);
510507

511-
expect(await page.getByRole('group', { name: 'Chosen name', exact: true }).evaluateAll(els => els.map(e => e.outerHTML))).toEqual([
512-
`<div role="group" aria-labelledby=""></div>`,
513-
]);
508+
// Old electron/android do not have this API.
509+
const isMissingAriaLabelledByElements = browserName === 'chromium' && browserMajorVersion < 135;
510+
if (!isMissingAriaLabelledByElements) {
511+
await group.evaluate(element => {
512+
element.ariaLabelledByElements = [document.querySelector('#title')!];
513+
});
514+
expect(await page.getByRole('group', { name: 'Chosen name', exact: true }).evaluateAll(els => els.map(e => e.outerHTML))).toEqual([
515+
`<div role="group" aria-labelledby=""></div>`,
516+
]);
517+
}
514518
});
515519

516520
test('SE-061 should support option name with html whitespace', async ({ page }) => {

0 commit comments

Comments
 (0)