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): produce snapshot diffs for iframes (#37991)

Dmitry Gozman59d69724a9ad
diff --git a/packages/injected/src/ariaSnapshot.ts b/packages/injected/src/ariaSnapshot.ts
index fb0c9e15a8..7db583e957 100644
--- a/packages/injected/src/ariaSnapshot.ts
+++ b/packages/injected/src/ariaSnapshot.ts
@@ -50,6 +50,7 @@ export type AriaSnapshot = {
root: AriaNode;
elements: Map<string, Element>;
refs: Map<Element, string>;
+ iframeRefs: string[];
};
type AriaRef = {
@@ -107,6 +108,7 @@ export function generateAriaTree(rootElement: Element, publicOptions: AriaTreeOp
root: { role: 'fragment', name: '', children: [], element: rootElement, props: {}, box: computeBox(rootElement), receivesPointerEvents: true },
elements: new Map<string, Element>(),
refs: new Map<Element, string>(),
+ iframeRefs: [],
};
const visit = (ariaNode: AriaNode, node: Node, parentElementVisible: boolean) => {
@@ -156,6 +158,8 @@ export function generateAriaTree(rootElement: Element, publicOptions: AriaTreeOp
if (childAriaNode.ref) {
snapshot.elements.set(childAriaNode.ref, element);
snapshot.refs.set(element, childAriaNode.ref);
+ if (childAriaNode.role === 'iframe')
+ snapshot.iframeRefs.push(childAriaNode.ref);
}
ariaNode.children.push(childAriaNode);
}
@@ -524,8 +528,6 @@ function compareSnapshots(ariaSnapshot: AriaSnapshot, previousSnapshot: AriaSnap
// Returns whether ariaNode is the same as previousNode.
const visit = (ariaNode: AriaNode, previousNode: AriaNode | undefined): boolean => {
let same: boolean = ariaNode.children.length === previousNode?.children.length && ariaNodesEqual(ariaNode, previousNode);
- if (ariaNode.role === 'iframe')
- same = false;
let canBeSkipped = same;
for (let childIndex = 0 ; childIndex < ariaNode.children.length; childIndex++) {
diff --git a/packages/injected/src/injectedScript.ts b/packages/injected/src/injectedScript.ts
index 18a97bf83b..f93c7292cf 100644
--- a/packages/injected/src/injectedScript.ts
+++ b/packages/injected/src/injectedScript.ts
@@ -300,18 +300,22 @@ export class InjectedScript {
return new Set<Element>(result.map(r => r.element));
}
- ariaSnapshot(node: Node, options: AriaTreeOptions & { track?: string, incremental?: boolean }): string {
+ ariaSnapshot(node: Node, options: AriaTreeOptions): string {
+ return this.incrementalAriaSnapshot(node, options).snapshot;
+ }
+
+ incrementalAriaSnapshot(node: Node, options: AriaTreeOptions & { track?: string, incremental?: boolean }): { snapshot: string, iframeRefs: string[], isIncremental: boolean } {
if (node.nodeType !== Node.ELEMENT_NODE)
throw this.createStacklessError('Can only capture aria snapshot of Element nodes.');
const ariaSnapshot = generateAriaTree(node as Element, options);
let previous: AriaSnapshot | undefined;
if (options.incremental)
previous = options.track ? this._lastAriaSnapshotForTrack.get(options.track) : undefined;
- const result = renderAriaTree(ariaSnapshot, options, previous);
+ const snapshot = renderAriaTree(ariaSnapshot, options, previous);
if (options.track)
this._lastAriaSnapshotForTrack.set(options.track, ariaSnapshot);
this._lastAriaSnapshotForQuery = ariaSnapshot;
- return result;
+ return { snapshot, iframeRefs: ariaSnapshot.iframeRefs, isIncremental: !!previous };
}
ariaSnapshotForRecorder(): { ariaSnapshot: string, refs: Map<Element, string> } {
diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.ts
index 179332f683..21a5ef43d4 100644
--- a/packages/playwright-core/src/server/page.ts
+++ b/packages/playwright-core/src/server/page.ts
@@ -860,7 +860,7 @@ export class Page extends SdkObject {
async snapshotForAI(progress: Progress, options: { track?: string, mode?: 'full' | 'incremental' }): Promise<string> {
const snapshot = await snapshotFrameForAI(progress, this.mainFrame(), options);
- return snapshot.join('\n');
+ return snapshot.lines.join('\n');
}
}
@@ -1035,9 +1035,9 @@ class FrameThrottler {
}
}
-async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, options: { track?: string, mode?: 'full' | 'incremental' }): Promise<string[]> {
+async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, options: { track?: string, mode?: 'full' | 'incremental' }): Promise<{ lines: string[], isIncremental: boolean }> {
// Only await the topmost navigations, inner frames will be empty when racing.
- const snapshot = await frame.retryWithProgressAndTimeouts(progress, [1000, 2000, 4000, 8000], async continuePolling => {
+ const { snapshot, iframeRefs, isIncremental } = await frame.retryWithProgressAndTimeouts(progress, [1000, 2000, 4000, 8000], async continuePolling => {
try {
const context = await progress.race(frame._utilityContext());
const injectedScript = await progress.race(context.injectedScript());
@@ -1045,8 +1045,8 @@ async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, optio
const node = injected.document.body;
if (!node)
return true;
- return injected.ariaSnapshot(node, { mode: 'ai', ...options });
- }, { refPrefix: frame.seq ? 'f' + frame.seq : '', incremental: options.mode === 'incremental' && !frame.parentFrame(), track: options.track }));
+ return injected.incrementalAriaSnapshot(node, { mode: 'ai', ...options });
+ }, { refPrefix: frame.seq ? 'f' + frame.seq : '', incremental: options.mode === 'incremental', track: options.track }));
if (snapshotOrRetry === true)
return continuePolling;
return snapshotOrRetry;
@@ -1059,8 +1059,23 @@ async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, optio
const lines = snapshot.split('\n');
const result = [];
+
+ if (isIncremental) {
+ result.push(...lines);
+ for (const ref of iframeRefs) {
+ const childSnapshot = await snapshotFrameRefForAI(progress, frame, ref, options);
+ if (!childSnapshot.lines.length)
+ continue;
+ if (childSnapshot.isIncremental)
+ result.push(...childSnapshot.lines);
+ else
+ result.push('- <changed> iframe [ref=' + ref + ']:', ...childSnapshot.lines.map(l => ' ' + l));
+ }
+ return { lines: result, isIncremental };
+ }
+
for (const line of lines) {
- const match = line.match(/^(\s*)-(?: <changed>)? iframe (?:\[active\] )?\[ref=([^\]]*)\]/);
+ const match = line.match(/^(\s*)- iframe (?:\[active\] )?\[ref=([^\]]*)\]/);
if (!match) {
result.push(line);
continue;
@@ -1068,21 +1083,25 @@ async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, optio
const leadingSpace = match[1];
const ref = match[2];
- const frameSelector = `aria-ref=${ref} >> internal:control=enter-frame`;
- const frameBodySelector = `${frameSelector} >> body`;
- const child = await progress.race(frame.selectors.resolveFrameForSelector(frameBodySelector, { strict: true }));
- if (!child) {
- result.push(line);
- continue;
- }
- try {
- const childSnapshot = await snapshotFrameForAI(progress, child.frame, options);
- result.push(line + ':', ...childSnapshot.map(l => leadingSpace + ' ' + l));
- } catch {
- result.push(line);
- }
+ const childSnapshot = await snapshotFrameRefForAI(progress, frame, ref, options);
+ result.push(childSnapshot.lines.length ? line + ':' : line);
+ result.push(...childSnapshot.lines.map(l => leadingSpace + ' ' + l));
+ }
+
+ return { lines: result, isIncremental };
+}
+
+async function snapshotFrameRefForAI(progress: Progress, parentFrame: frames.Frame, frameRef: string, options: { track?: string, mode?: 'full' | 'incremental' }): Promise<{ lines: string[], isIncremental: boolean }> {
+ const frameSelector = `aria-ref=${frameRef} >> internal:control=enter-frame`;
+ const frameBodySelector = `${frameSelector} >> body`;
+ const child = await progress.race(parentFrame.selectors.resolveFrameForSelector(frameBodySelector, { strict: true }));
+ if (!child)
+ return { lines: [], isIncremental: false };
+ try {
+ return await snapshotFrameForAI(progress, child.frame, options);
+ } catch {
+ return { lines: [], isIncremental: false };
}
- return result;
}
function ensureArrayLimit<T>(array: T[], limit: number): T[] {
diff --git a/tests/page/page-aria-snapshot-ai.spec.ts b/tests/page/page-aria-snapshot-ai.spec.ts
index 920cfbbcba..1273e7ebe3 100644
--- a/tests/page/page-aria-snapshot-ai.spec.ts
+++ b/tests/page/page-aria-snapshot-ai.spec.ts
@@ -638,17 +638,33 @@ it('should create incremental snapshot for text change', async ({ page }) => {
`);
});
-it('should not produce incremental snapshot for iframes', async ({ page }) => {
+it('should produce incremental snapshot for iframes', async ({ page }) => {
await page.setContent(`
- <iframe srcdoc="<h1>hello</h1>"></iframe>
+ <iframe srcdoc="
+ <li>
+ <span style='display:none'>outer text</span>
+ <button>a button</button>
+ <iframe src='data:text/html,<li>inner text</li>' style='display:none'></iframe>
+ </li>
+ "></iframe>
`);
expect(await snapshotForAI(page, { track: 'track', mode: 'incremental' })).toContainYaml(`
- iframe [ref=e2]:
- - heading "hello" [level=1] [ref=f1e2]
+ - listitem [ref=f1e2]:
+ - button "a button" [ref=f1e3]
`);
+
+ await page.frames()[1].evaluate(() => {
+ document.querySelector('span').style.display = 'block';
+ document.querySelector('iframe').style.display = 'block';
+ });
expect(await snapshotForAI(page, { track: 'track', mode: 'incremental' })).toContainYaml(`
- - <changed> iframe [ref=e2]:
- - heading "hello" [level=1] [ref=f1e2]
+ - <changed> listitem [ref=f1e2]:
+ - generic [ref=f1e4]: outer text
+ - ref=f1e3 [unchanged]
+ - iframe [ref=f1e5]
+ - <changed> iframe [ref=f1e5]:
+ - listitem [ref=f2e2]: inner text
`);
});