From 2d0e3c548feecca758e500c8def85cdcbbc412c0 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 12 Dec 2022 17:14:29 +0100 Subject: [PATCH] fix: improve a11y snapshot handling if the tree is not correct Bug #9404 --- .../src/common/Accessibility.ts | 5 ++- test/src/accessibility.spec.ts | 45 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/puppeteer-core/src/common/Accessibility.ts b/packages/puppeteer-core/src/common/Accessibility.ts index ea540c01d799d..a2c4846206d23 100644 --- a/packages/puppeteer-core/src/common/Accessibility.ts +++ b/packages/puppeteer-core/src/common/Accessibility.ts @@ -564,7 +564,10 @@ class AXNode { } for (const node of nodeById.values()) { for (const childId of node.payload.childIds || []) { - node.children.push(nodeById.get(childId)!); + const child = nodeById.get(childId); + if (child) { + node.children.push(child); + } } } return nodeById.values().next().value; diff --git a/test/src/accessibility.spec.ts b/test/src/accessibility.spec.ts index 3f4ec0163fee5..277fea86a0fb7 100644 --- a/test/src/accessibility.spec.ts +++ b/test/src/accessibility.spec.ts @@ -163,6 +163,51 @@ describe('Accessibility', function () { ) ).toEqual(golden); }); + it('get snapshots while the tree is re-calculated', async () => { + // see https://github.com/puppeteer/puppeteer/issues/9404 + const {page} = getTestState(); + + await page.setContent( + ` + + + + + + Accessible name + aria-expanded puppeteer bug + + + + +

Some content

+ + + ` + ); + async function getAccessibleName(page: any, element: any) { + return (await page.accessibility.snapshot({root: element})).name; + } + const button = await page.$('button'); + expect(await getAccessibleName(page, button)).toEqual('Show'); + await button?.click(); + await page.waitForSelector('aria/Hide'); + }); it('roledescription', async () => { const {page} = getTestState();