Skip to content

chore(snapshot for ai): add timeout to limit waiting for iframes #36712

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,8 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
return result.pdf;
}

async _snapshotForAI(): Promise<string> {
const result = await this._channel.snapshotForAI();
async _snapshotForAI(options: TimeoutOptions = {}): Promise<string> {
const result = await this._channel.snapshotForAI({ timeout: this._timeoutSettings.timeout(options) });
return result.snapshot;
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1454,7 +1454,9 @@ scheme.PagePdfParams = tObject({
scheme.PagePdfResult = tObject({
pdf: tBinary,
});
scheme.PageSnapshotForAIParams = tOptional(tObject({}));
scheme.PageSnapshotForAIParams = tObject({
timeout: tNumber,
});
scheme.PageSnapshotForAIResult = tObject({
snapshot: tString,
});
Expand Down
10 changes: 7 additions & 3 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,7 @@ export interface PageChannel extends PageEventTarget, EventTargetChannel {
touchscreenTap(params: PageTouchscreenTapParams, progress?: Progress): Promise<PageTouchscreenTapResult>;
accessibilitySnapshot(params: PageAccessibilitySnapshotParams, progress?: Progress): Promise<PageAccessibilitySnapshotResult>;
pdf(params: PagePdfParams, progress?: Progress): Promise<PagePdfResult>;
snapshotForAI(params?: PageSnapshotForAIParams, progress?: Progress): Promise<PageSnapshotForAIResult>;
snapshotForAI(params: PageSnapshotForAIParams, progress?: Progress): Promise<PageSnapshotForAIResult>;
startJSCoverage(params: PageStartJSCoverageParams, progress?: Progress): Promise<PageStartJSCoverageResult>;
stopJSCoverage(params?: PageStopJSCoverageParams, progress?: Progress): Promise<PageStopJSCoverageResult>;
startCSSCoverage(params: PageStartCSSCoverageParams, progress?: Progress): Promise<PageStartCSSCoverageResult>;
Expand Down Expand Up @@ -2540,8 +2540,12 @@ export type PagePdfOptions = {
export type PagePdfResult = {
pdf: Binary,
};
export type PageSnapshotForAIParams = {};
export type PageSnapshotForAIOptions = {};
export type PageSnapshotForAIParams = {
timeout: number,
};
export type PageSnapshotForAIOptions = {

};
export type PageSnapshotForAIResult = {
snapshot: string,
};
Expand Down
2 changes: 2 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1918,6 +1918,8 @@ Page:

snapshotForAI:
internal: true
parameters:
timeout: number
returns:
snapshot: string
flags:
Expand Down
24 changes: 22 additions & 2 deletions tests/page/page-aria-snapshot-ai.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { asLocator } from 'playwright-core/lib/utils';

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

function snapshotForAI(page: any): Promise<string> {
return page._snapshotForAI();
function snapshotForAI(page: any, options?: { timeout?: number }): Promise<string> {
return page._snapshotForAI(options);
}

it('should generate refs', async ({ page }) => {
Expand Down Expand Up @@ -354,3 +354,23 @@ it('should mark iframe as active when it contains focused element', async ({ pag
- textbox "Input in iframe" [active] [ref=f1e2]
`);
});

it('return empty snapshot when iframe is not loaded', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/pull/36710' } }, async ({ page, server }) => {
await page.setContent(`
<div style="height: 5000px;">Test</div>
<iframe loading="lazy" src="${server.PREFIX}/frame.html"></iframe>
`);

// Wait for the iframe to load
await page.waitForSelector('iframe');

// Get the snapshot of the page
const snapshot = await snapshotForAI(page, { timeout: 100 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test will fail because Playwright tests have a default timeout of 30 seconds. You might want to set the timeout value to less than 30 seconds.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is 100ms, so there's enough time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My previous comment was off the mark—sorry about that.


// The iframe should be present but empty
expect(snapshot).toContainYaml(`
- generic [active] [ref=e1]:
- generic [ref=e2]: Test
- iframe [ref=e3]
`);
});
Loading