From bd89ddaca43f79de6d2cff9e52cffa81c0b75f80 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Mon, 21 Apr 2025 16:07:14 -0700 Subject: [PATCH] chore(breaking): do not include hidden elements in aria snapshot (#35684) --- packages/injected/src/ariaSnapshot.ts | 6 +++++- tests/library/inspector/cli-codegen-aria.spec.ts | 1 - tests/page/page-aria-snapshot.spec.ts | 12 ++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/injected/src/ariaSnapshot.ts b/packages/injected/src/ariaSnapshot.ts index af310747aa..7f7b72c356 100644 --- a/packages/injected/src/ariaSnapshot.ts +++ b/packages/injected/src/ariaSnapshot.ts @@ -17,7 +17,7 @@ import { Map, Set } from '@isomorphic/builtins'; import { escapeRegExp, longestCommonSubstring, normalizeWhiteSpace } from '@isomorphic/stringUtils'; -import { getElementComputedStyle, getGlobalOptions } from './domUtils'; +import { getElementComputedStyle, getGlobalOptions, isElementVisible } from './domUtils'; import * as roleUtils from './roleUtils'; import { yamlEscapeKeyIfNeeded, yamlEscapeValueIfNeeded } from './yaml'; @@ -86,6 +86,10 @@ export function generateAriaTree(rootElement: Element, generation: number): Aria } } + // Skip all the leaf nodes that are not visible as they can't be interacted with. + if (!ariaChildren.length && !isElementVisible(element)) + return; + addElement(element); const childAriaNode = toAriaNode(element); if (childAriaNode) diff --git a/tests/library/inspector/cli-codegen-aria.spec.ts b/tests/library/inspector/cli-codegen-aria.spec.ts index a11cf33342..9a81f25f73 100644 --- a/tests/library/inspector/cli-codegen-aria.spec.ts +++ b/tests/library/inspector/cli-codegen-aria.spec.ts @@ -68,7 +68,6 @@ test.describe(() => { await recorder.trustedClick(); await recorder.recorderPage.getByRole('tab', { name: 'Aria' }).click(); await expect(recorder.recorderPage.locator('.tab-aria .CodeMirror')).toMatchAriaSnapshot(` - - textbox - text: '- button "Submit"' `); }); diff --git a/tests/page/page-aria-snapshot.spec.ts b/tests/page/page-aria-snapshot.spec.ts index 196402361f..5e3f0cf93a 100644 --- a/tests/page/page-aria-snapshot.spec.ts +++ b/tests/page/page-aria-snapshot.spec.ts @@ -730,3 +730,15 @@ it('ref mode can be used to stitch all frame snapshots', async ({ page, server } - text: Hi, I'm frame `.trim()); }); + +it('should not include hidden input elements', async ({ page }) => { + await page.setContent(` + + + + `); + + const snapshot = await page.locator('body').ariaSnapshot(); + expect(snapshot).toContain(`- button \"One\" +- button \"Three\"`); +});