chore: only include visible iframes, keep frame locators in own array (#60)

As discussed:

- hides invisible frames from snapshot
- keep our own frame locator array, so we don't rely on `page.frames()`
ordering to be stable
This commit is contained in:
Simon Knott 2025-03-27 20:22:44 +01:00 committed by GitHub
parent 1b18e31ffe
commit f033213618
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 60 additions and 40 deletions

View file

@ -74,29 +74,20 @@ async function waitForCompletion<R>(page: playwright.Page, callback: () => Promi
export async function runAndWait(context: Context, status: string, callback: (page: playwright.Page) => Promise<any>, snapshot: boolean = false): Promise<ToolResult> {
const page = context.existingPage();
await waitForCompletion(page, () => callback(page));
return snapshot ? captureAriaSnapshot(page, status) : {
return snapshot ? captureAriaSnapshot(context, status) : {
content: [{ type: 'text', text: status }],
};
}
export async function captureAllFrameSnapshot(page: playwright.Page): Promise<string> {
const snapshots = await Promise.all(page.frames().map(frame => frame.locator('html').ariaSnapshot({ ref: true })));
const scopedSnapshots = snapshots.map((snapshot, frameIndex) => {
if (frameIndex === 0)
return snapshot;
return snapshot.replaceAll('[ref=', `[ref=f${frameIndex}`);
});
return scopedSnapshots.join('\n');
}
export async function captureAriaSnapshot(page: playwright.Page, status: string = ''): Promise<ToolResult> {
export async function captureAriaSnapshot(context: Context, status: string = ''): Promise<ToolResult> {
const page = context.existingPage();
return {
content: [{ type: 'text', text: `${status ? `${status}\n` : ''}
- Page URL: ${page.url()}
- Page Title: ${await page.title()}
- Page Snapshot
\`\`\`yaml
${await captureAllFrameSnapshot(page)}
${await context.allFramesSnapshot()}
\`\`\`
`
}],