Skip to content

Commit c8e41e3

Browse files
Skn0ttogadra
andauthored
chore(snapshot for ai): add timeout to limit waiting for iframes (#36712)
Co-authored-by: ogadra <61941819+ogadra@users.noreply.github.com>
1 parent c84b1a0 commit c8e41e3

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

packages/playwright-core/src/client/page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,8 +832,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
832832
return result.pdf;
833833
}
834834

835-
async _snapshotForAI(): Promise<string> {
836-
const result = await this._channel.snapshotForAI();
835+
async _snapshotForAI(options: TimeoutOptions = {}): Promise<string> {
836+
const result = await this._channel.snapshotForAI({ timeout: this._timeoutSettings.timeout(options) });
837837
return result.snapshot;
838838
}
839839
}

packages/playwright-core/src/protocol/validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,9 @@ scheme.PagePdfParams = tObject({
14541454
scheme.PagePdfResult = tObject({
14551455
pdf: tBinary,
14561456
});
1457-
scheme.PageSnapshotForAIParams = tOptional(tObject({}));
1457+
scheme.PageSnapshotForAIParams = tObject({
1458+
timeout: tNumber,
1459+
});
14581460
scheme.PageSnapshotForAIResult = tObject({
14591461
snapshot: tString,
14601462
});

packages/protocol/src/channels.d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,7 +2111,7 @@ export interface PageChannel extends PageEventTarget, EventTargetChannel {
21112111
touchscreenTap(params: PageTouchscreenTapParams, progress?: Progress): Promise<PageTouchscreenTapResult>;
21122112
accessibilitySnapshot(params: PageAccessibilitySnapshotParams, progress?: Progress): Promise<PageAccessibilitySnapshotResult>;
21132113
pdf(params: PagePdfParams, progress?: Progress): Promise<PagePdfResult>;
2114-
snapshotForAI(params?: PageSnapshotForAIParams, progress?: Progress): Promise<PageSnapshotForAIResult>;
2114+
snapshotForAI(params: PageSnapshotForAIParams, progress?: Progress): Promise<PageSnapshotForAIResult>;
21152115
startJSCoverage(params: PageStartJSCoverageParams, progress?: Progress): Promise<PageStartJSCoverageResult>;
21162116
stopJSCoverage(params?: PageStopJSCoverageParams, progress?: Progress): Promise<PageStopJSCoverageResult>;
21172117
startCSSCoverage(params: PageStartCSSCoverageParams, progress?: Progress): Promise<PageStartCSSCoverageResult>;
@@ -2540,8 +2540,12 @@ export type PagePdfOptions = {
25402540
export type PagePdfResult = {
25412541
pdf: Binary,
25422542
};
2543-
export type PageSnapshotForAIParams = {};
2544-
export type PageSnapshotForAIOptions = {};
2543+
export type PageSnapshotForAIParams = {
2544+
timeout: number,
2545+
};
2546+
export type PageSnapshotForAIOptions = {
2547+
2548+
};
25452549
export type PageSnapshotForAIResult = {
25462550
snapshot: string,
25472551
};

packages/protocol/src/protocol.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,8 @@ Page:
19181918

19191919
snapshotForAI:
19201920
internal: true
1921+
parameters:
1922+
timeout: number
19211923
returns:
19221924
snapshot: string
19231925
flags:

tests/page/page-aria-snapshot-ai.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import { asLocator } from 'playwright-core/lib/utils';
1919

2020
import { test as it, expect, unshift } from './pageTest';
2121

22-
function snapshotForAI(page: any): Promise<string> {
23-
return page._snapshotForAI();
22+
function snapshotForAI(page: any, options?: { timeout?: number }): Promise<string> {
23+
return page._snapshotForAI(options);
2424
}
2525

2626
it('should generate refs', async ({ page }) => {
@@ -354,3 +354,23 @@ it('should mark iframe as active when it contains focused element', async ({ pag
354354
- textbox "Input in iframe" [active] [ref=f1e2]
355355
`);
356356
});
357+
358+
it('return empty snapshot when iframe is not loaded', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/pull/36710' } }, async ({ page, server }) => {
359+
await page.setContent(`
360+
<div style="height: 5000px;">Test</div>
361+
<iframe loading="lazy" src="${server.PREFIX}/frame.html"></iframe>
362+
`);
363+
364+
// Wait for the iframe to load
365+
await page.waitForSelector('iframe');
366+
367+
// Get the snapshot of the page
368+
const snapshot = await snapshotForAI(page, { timeout: 100 });
369+
370+
// The iframe should be present but empty
371+
expect(snapshot).toContainYaml(`
372+
- generic [active] [ref=e1]:
373+
- generic [ref=e2]: Test
374+
- iframe [ref=e3]
375+
`);
376+
});

0 commit comments

Comments
 (0)