Skip to content

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

chore: inline the dom-inline generic nodes (#37197)

Pavel Feldmanfe5803e5543b
diff --git a/packages/injected/src/ariaSnapshot.ts b/packages/injected/src/ariaSnapshot.ts
index ad4ccbd233..89d447299a 100644
--- a/packages/injected/src/ariaSnapshot.ts
+++ b/packages/injected/src/ariaSnapshot.ts
@@ -16,7 +16,7 @@
import { escapeRegExp, longestCommonSubstring, normalizeWhiteSpace } from '@isomorphic/stringUtils';
-import { box, getElementComputedStyle, isElementVisible } from './domUtils';
+import { computeBox, getElementComputedStyle, isElementVisible } from './domUtils';
import * as roleUtils from './roleUtils';
import { yamlEscapeKeyIfNeeded, yamlEscapeValueIfNeeded } from './yaml';
@@ -92,7 +92,7 @@ export function generateAriaTree(rootElement: Element, publicOptions: AriaTreeOp
const visited = new Set<Node>();
const snapshot: AriaSnapshot = {
- root: { role: 'fragment', name: '', children: [], element: rootElement, props: {}, box: box(rootElement), receivesPointerEvents: true },
+ root: { role: 'fragment', name: '', children: [], element: rootElement, props: {}, box: computeBox(rootElement), receivesPointerEvents: true },
elements: new Map<string, Element>(),
refs: new Map<Element, string>(),
};
@@ -231,7 +231,7 @@ function toAriaNode(element: Element, options: InternalOptions): AriaNode | null
children: [],
props: {},
element,
- box: box(element),
+ box: computeBox(element),
receivesPointerEvents: true,
active
};
@@ -247,13 +247,17 @@ function toAriaNode(element: Element, options: InternalOptions): AriaNode | null
const name = normalizeWhiteSpace(roleUtils.getElementAccessibleName(element, false) || '');
const receivesPointerEvents = roleUtils.receivesPointerEvents(element);
+ const box = computeBox(element);
+ if (role === 'generic' && box.inline && element.childNodes.length === 1 && element.childNodes[0].nodeType === Node.TEXT_NODE)
+ return null;
+
const result: AriaNode = {
role,
name,
children: [],
props: {},
element,
- box: box(element),
+ box,
receivesPointerEvents,
active
};
diff --git a/packages/injected/src/domUtils.ts b/packages/injected/src/domUtils.ts
index e11d647694..c40c6918f0 100644
--- a/packages/injected/src/domUtils.ts
+++ b/packages/injected/src/domUtils.ts
@@ -105,33 +105,34 @@ export function isElementStyleVisibilityVisible(element: Element, style?: CSSSty
export type Box = {
visible: boolean;
+ inline: boolean;
rect?: DOMRect;
style?: CSSStyleDeclaration;
};
-export function box(element: Element): Box {
+export function computeBox(element: Element): Box {
// Note: this logic should be similar to waitForDisplayedAtStablePosition() to avoid surprises.
const style = getElementComputedStyle(element);
if (!style)
- return { visible: true };
+ return { visible: true, inline: false };
if (style.display === 'contents') {
// display:contents is not rendered itself, but its child nodes are.
for (let child = element.firstChild; child; child = child.nextSibling) {
if (child.nodeType === 1 /* Node.ELEMENT_NODE */ && isElementVisible(child as Element))
- return { visible: true, style };
+ return { visible: true, inline: false, style };
if (child.nodeType === 3 /* Node.TEXT_NODE */ && isVisibleTextNode(child as Text))
- return { visible: true, style };
+ return { visible: true, inline: true, style };
}
- return { visible: false, style };
+ return { visible: false, inline: false, style };
}
if (!isElementStyleVisibilityVisible(element, style))
- return { style, visible: false };
+ return { style, visible: false, inline: false };
const rect = element.getBoundingClientRect();
- return { rect, style, visible: rect.width > 0 && rect.height > 0 };
+ return { rect, style, visible: rect.width > 0 && rect.height > 0, inline: style.display === 'inline' };
}
export function isElementVisible(element: Element): boolean {
- return box(element).visible;
+ return computeBox(element).visible;
}
export function isVisibleTextNode(node: Text) {
diff --git a/tests/page/page-aria-snapshot-ai.spec.ts b/tests/page/page-aria-snapshot-ai.spec.ts
index 1493f65291..39a716294d 100644
--- a/tests/page/page-aria-snapshot-ai.spec.ts
+++ b/tests/page/page-aria-snapshot-ai.spec.ts
@@ -184,9 +184,9 @@ it('emit generic roles for nodes w/o roles', async ({ page }) => {
expect(snapshot).toContainYaml(`
- generic [ref=e2]:
- - generic [ref=e5]: Apple
- - generic [ref=e8]: Pear
- - generic [ref=e11]: Orange
+ - generic [ref=e3]: Apple
+ - generic [ref=e5]: Pear
+ - generic [ref=e7]: Orange
`);
});
@@ -400,3 +400,33 @@ it('should support many properties on iframes', async ({ page }) => {
- textbox "Input in iframe" [active] [ref=f1e2]
`);
});
+
+it('should collapse inline generic nodes', async ({ page }) => {
+ await page.setContent(`
+ <ul>
+ <li><b>3</b> <abbr>bds</abbr></li>
+ <li><b>2</b> <abbr>ba</abbr></li>
+ <li><b>1,200</b> <abbr>sqft</abbr></li>
+ </ul>
+ <ul>
+ <li><div>3</div></li>
+ <li><div>2</div></li>
+ <li><div>1,200</div></li>
+ </ul>`);
+
+ const snapshot1 = await snapshotForAI(page);
+ expect(snapshot1).toContainYaml(`
+ - generic [active] [ref=e1]:
+ - list [ref=e2]:
+ - listitem [ref=e3]: 3 bds
+ - listitem [ref=e4]: 2 ba
+ - listitem [ref=e5]: 1,200 sqft
+ - list [ref=e6]:
+ - listitem [ref=e7]:
+ - generic [ref=e8]: "3"
+ - listitem [ref=e9]:
+ - generic [ref=e10]: "2"
+ - listitem [ref=e11]:
+ - generic [ref=e12]: 1,200
+ `);
+});