Skip to content

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

fix(ai): properly capture iframe properties in AI snapshots (#36933)

Adam Gastineaucc09a085ef0b
diff --git a/packages/playwright-core/src/server/page.ts b/packages/playwright-core/src/server/page.ts
index 7753982667..7f8f7b7163 100644
--- a/packages/playwright-core/src/server/page.ts
+++ b/packages/playwright-core/src/server/page.ts
@@ -1034,7 +1034,7 @@ async function snapshotFrameForAI(progress: Progress, frame: frames.Frame, frame
const lines = snapshot.split('\n');
const result = [];
for (const line of lines) {
- const match = line.match(/^(\s*)- iframe (?:\[active\] )?\[ref=(.*)\]/);
+ const match = line.match(/^(\s*)- iframe (?:\[active\] )?\[ref=([^\]]*)\]/);
if (!match) {
result.push(line);
continue;
diff --git a/tests/page/page-aria-snapshot-ai.spec.ts b/tests/page/page-aria-snapshot-ai.spec.ts
index dac1bc3fcb..c7afd03cf3 100644
--- a/tests/page/page-aria-snapshot-ai.spec.ts
+++ b/tests/page/page-aria-snapshot-ai.spec.ts
@@ -374,3 +374,21 @@ it('return empty snapshot when iframe is not loaded', { annotation: { type: 'iss
- iframe [ref=e3]
`);
});
+
+it('should support many properties on iframes', async ({ page }) => {
+ await page.setContent(`
+ <input id="regular-input" placeholder="Regular input">
+ <iframe style='cursor: pointer' src="data:text/html,<input id='iframe-input' placeholder='Input in iframe'/>" tabindex="0"></iframe>
+ `);
+
+ // Test 1: Focus the input inside the iframe
+ await page.frameLocator('iframe').locator('#iframe-input').focus();
+ const inputInIframeFocusedSnapshot = await snapshotForAI(page);
+
+ expect(inputInIframeFocusedSnapshot).toContainYaml(`
+ - generic [ref=e1]:
+ - textbox "Regular input" [ref=e2]
+ - iframe [active] [ref=e3] [cursor=pointer]:
+ - textbox "Input in iframe" [active] [ref=f1e2]
+ `);
+});