Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(aria): process children of hidden elements (#36316)

Simon Knott0b3c79ab4df1
diff --git a/packages/injected/src/ariaSnapshot.ts b/packages/injected/src/ariaSnapshot.ts
index c2344910da..92b3059495 100644
--- a/packages/injected/src/ariaSnapshot.ts
+++ b/packages/injected/src/ariaSnapshot.ts
@@ -55,12 +55,15 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
elements: new Map<string, Element>(),
};
- const visit = (ariaNode: AriaNode, node: Node) => {
+ const visit = (ariaNode: AriaNode, node: Node, parentElementVisible: boolean) => {
if (visited.has(node))
return;
visited.add(node);
if (node.nodeType === Node.TEXT_NODE && node.nodeValue) {
+ if (!parentElementVisible)
+ return;
+
const text = node.nodeValue;
// <textarea>AAA</textarea> should not report AAA as a child of the textarea.
if (ariaNode.role !== 'textbox' && text)
@@ -72,10 +75,8 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
return;
const element = node as Element;
- let isVisible = !roleUtils.isElementHiddenForAria(element);
- if (options?.forAI)
- isVisible = isVisible || isElementVisible(element);
- if (!isVisible)
+ const isElementHiddenForAria = roleUtils.isElementHiddenForAria(element);
+ if (isElementHiddenForAria && !options?.forAI)
return;
const ariaChildren: Element[] = [];
@@ -88,16 +89,17 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
}
}
- const childAriaNode = toAriaNode(element, options);
+ const visible = !isElementHiddenForAria || isElementVisible(element);
+ const childAriaNode = visible ? toAriaNode(element, options) : null;
if (childAriaNode) {
if (childAriaNode.ref)
snapshot.elements.set(childAriaNode.ref, element);
ariaNode.children.push(childAriaNode);
}
- processElement(childAriaNode || ariaNode, element, ariaChildren);
+ processElement(childAriaNode || ariaNode, element, ariaChildren, visible);
};
- function processElement(ariaNode: AriaNode, element: Element, ariaChildren: Element[] = []) {
+ function processElement(ariaNode: AriaNode, element: Element, ariaChildren: Element[], parentElementVisible: boolean) {
// Surround every element with spaces for the sake of concatenated text nodes.
const display = getElementComputedStyle(element)?.display || 'inline';
const treatAsBlock = (display !== 'inline' || element.nodeName === 'BR') ? ' ' : '';
@@ -108,20 +110,20 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
const assignedNodes = element.nodeName === 'SLOT' ? (element as HTMLSlotElement).assignedNodes() : [];
if (assignedNodes.length) {
for (const child of assignedNodes)
- visit(ariaNode, child);
+ visit(ariaNode, child, parentElementVisible);
} else {
for (let child = element.firstChild; child; child = child.nextSibling) {
if (!(child as Element | Text).assignedSlot)
- visit(ariaNode, child);
+ visit(ariaNode, child, parentElementVisible);
}
if (element.shadowRoot) {
for (let child = element.shadowRoot.firstChild; child; child = child.nextSibling)
- visit(ariaNode, child);
+ visit(ariaNode, child, parentElementVisible);
}
}
for (const child of ariaChildren)
- visit(ariaNode, child);
+ visit(ariaNode, child, parentElementVisible);
ariaNode.children.push(roleUtils.getCSSContent(element, '::after') || '');
@@ -139,7 +141,7 @@ export function generateAriaTree(rootElement: Element, options?: { forAI?: boole
roleUtils.beginAriaCaches();
try {
- visit(snapshot.root, rootElement);
+ visit(snapshot.root, rootElement, true);
} finally {
roleUtils.endAriaCaches();
}
diff --git a/tests/page/page-aria-snapshot-ai.spec.ts b/tests/page/page-aria-snapshot-ai.spec.ts
index 12bc618632..09829911ed 100644
--- a/tests/page/page-aria-snapshot-ai.spec.ts
+++ b/tests/page/page-aria-snapshot-ai.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import { test as it, expect } from './pageTest';
+import { test as it, expect, unshift } from './pageTest';
function snapshotForAI(page: any): Promise<string> {
return page._snapshotForAI();
@@ -257,6 +257,31 @@ it('should auto-wait for blocking CSS', async ({ page, server }) => {
expect(await snapshotForAI(page)).toContainYaml('Hello World');
});
+it('should show visible children of hidden elements', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36296' } }, async ({ page }) => {
+ await page.setContent(`
+ <div style="visibility: hidden">
+ <div style="visibility: visible">
+ <button>Visible</button>
+ </div>
+ <div style="visibility: hidden">
+ <button style="visibility: visible">Visible</button>
+ </div>
+ <div>
+ <div style="visibility: visible">
+ <button style="visibility: hidden">Hidden</button>
+ </div>
+ <button>Hidden</button>
+ </div>
+ </div>
+ `);
+
+ expect(await snapshotForAI(page)).toEqual(unshift(`
+ - generic [active] [ref=e1]:
+ - button "Visible" [ref=e3]
+ - button "Visible" [ref=e4]
+ `));
+});
+
it('should include active element information', async ({ page }) => {
await page.setContent(`
<button id="btn1">Button 1</button>
diff --git a/tests/page/page-aria-snapshot.spec.ts b/tests/page/page-aria-snapshot.spec.ts
index ce19ea4351..e08f4830da 100644
--- a/tests/page/page-aria-snapshot.spec.ts
+++ b/tests/page/page-aria-snapshot.spec.ts
@@ -15,20 +15,7 @@
*/
import type { Locator } from '@playwright/test';
-import { test as it, expect } from './pageTest';
-
-function unshift(snapshot: string): string {
- const lines = snapshot.split('\n');
- let whitespacePrefixLength = 100;
- for (const line of lines) {
- if (!line.trim())
- continue;
- const match = line.match(/^(\s*)/);
- if (match && match[1].length < whitespacePrefixLength)
- whitespacePrefixLength = match[1].length;
- }
- return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');
-}
+import { test as it, expect, unshift } from './pageTest';
async function checkAndMatchSnapshot(locator: Locator, snapshot: string) {
expect.soft(await locator.ariaSnapshot()).toBe(unshift(snapshot));
@@ -658,3 +645,27 @@ it('should not report textarea textContent', async ({ page }) => {
- textbox: After
`);
});
+
+it('should not show visible children of hidden elements', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36296' } }, async ({ page }) => {
+ await page.setContent(`
+ <div style="visibility: hidden;">
+ <div style="visibility: visible;">
+ <button>Button</button>
+ </div>
+ </div>
+ `);
+
+ expect(await page.locator('body').ariaSnapshot()).toBe('');
+});
+
+it('should not show unhidden children of aria-hidden elements', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/36296' } }, async ({ page }) => {
+ await page.setContent(`
+ <div aria-hidden="true">
+ <div aria-hidden="false">
+ <button>Button</button>
+ </div>
+ </div>
+ `);
+
+ expect(await page.locator('body').ariaSnapshot()).toBe('');
+});
diff --git a/tests/page/pageTest.ts b/tests/page/pageTest.ts
index f664756388..52d9b95c67 100644
--- a/tests/page/pageTest.ts
+++ b/tests/page/pageTest.ts
@@ -54,6 +54,19 @@ export function roundBox(box: BoundingBox): BoundingBox {
};
}
+export function unshift(snapshot: string): string {
+ const lines = snapshot.split('\n');
+ let whitespacePrefixLength = 100;
+ for (const line of lines) {
+ if (!line.trim())
+ continue;
+ const match = line.match(/^(\s*)/);
+ if (match && match[1].length < whitespacePrefixLength)
+ whitespacePrefixLength = match[1].length;
+ }
+ return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');
+}
+
export const expect = baseExpect.extend({
toContainYaml(received: string, expected: string) {
const trimmed = expected.split('\n').filter(a => !!a.trim());