chore: follow up on tab snapshot capture (#739)

This commit is contained in:
Pavel Feldman
2025-07-22 17:43:42 -07:00
committed by GitHub
parent 601a74305c
commit 6320b08173
13 changed files with 80 additions and 101 deletions

View File

@@ -52,9 +52,9 @@ const resize = defineTabTool({
response.addCode(`// Resize browser window to ${params.width}x${params.height}`);
response.addCode(`await page.setViewportSize({ width: ${params.width}, height: ${params.height} });`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await tab.page.setViewportSize({ width: params.width, height: params.height });
}, response);
});
},
});

View File

@@ -39,12 +39,12 @@ const handleDialog = defineTabTool({
throw new Error('No dialog visible');
tab.clearModalState(dialogState);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
if (params.accept)
await dialogState.dialog.accept(params.promptText);
else
await dialogState.dialog.dismiss();
}, response);
});
},
clearsModalState: 'dialog',

View File

@@ -49,11 +49,11 @@ const evaluate = defineTabTool({
response.addCode(`await page.evaluate(${javascript.quote(params.function)});`);
}
await tab.run(async () => {
await tab.waitForCompletion(async () => {
const receiver = locator ?? tab.page as any;
const result = await receiver._evaluateFunction(params.function);
response.addResult(JSON.stringify(result, null, 2) || 'undefined');
}, response);
});
},
});

View File

@@ -40,10 +40,10 @@ const uploadFile = defineTabTool({
response.addCode(`// Select files for upload`);
response.addCode(`await fileChooser.setFiles(${JSON.stringify(params.paths)})`);
await tab.run(async () => {
tab.clearModalState(modalState);
await tab.waitForCompletion(async () => {
await modalState.fileChooser.setFiles(params.paths);
tab.clearModalState(modalState);
}, response);
});
},
clearsModalState: 'fileChooser',
});

View File

@@ -39,9 +39,9 @@ const pressKey = defineTabTool({
response.addCode(`// Press ${params.key}`);
response.addCode(`await page.keyboard.press('${params.key}');`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await tab.page.keyboard.press(params.key);
}, response);
});
},
});
@@ -66,7 +66,7 @@ const type = defineTabTool({
const locator = await tab.refLocator(params);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
if (params.slowly) {
response.addCode(`// Press "${params.text}" sequentially into "${params.element}"`);
response.addCode(`await page.${await generateLocator(locator)}.pressSequentially(${javascript.quote(params.text)});`);
@@ -81,7 +81,7 @@ const type = defineTabTool({
response.addCode(`await page.${await generateLocator(locator)}.press('Enter');`);
await locator.press('Enter');
}
}, response);
});
},
});

View File

@@ -38,9 +38,9 @@ const mouseMove = defineTabTool({
response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await tab.page.mouse.move(params.x, params.y);
}, response);
});
},
});
@@ -65,11 +65,11 @@ const mouseClick = defineTabTool({
response.addCode(`await page.mouse.down();`);
response.addCode(`await page.mouse.up();`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await tab.page.mouse.move(params.x, params.y);
await tab.page.mouse.down();
await tab.page.mouse.up();
}, response);
});
},
});
@@ -97,12 +97,12 @@ const mouseDrag = defineTabTool({
response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
response.addCode(`await page.mouse.up();`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await tab.page.mouse.move(params.startX, params.startY);
await tab.page.mouse.down();
await tab.page.mouse.move(params.endX, params.endY);
await tab.page.mouse.up();
}, response);
});
},
});

View File

@@ -34,9 +34,9 @@ const navigate = defineTool({
const tab = await context.ensureTab();
await tab.navigate(params.url);
response.setIncludeSnapshot();
response.addCode(`// Navigate to ${params.url}`);
response.addCode(`await page.goto('${params.url}');`);
response.addSnapshot(await tab.captureSnapshot());
},
});
@@ -54,9 +54,9 @@ const goBack = defineTabTool({
response.setIncludeSnapshot();
await tab.page.goBack();
response.setIncludeSnapshot();
response.addCode(`// Navigate back`);
response.addCode(`await page.goBack();`);
response.addSnapshot(await tab.captureSnapshot());
},
});
@@ -73,9 +73,9 @@ const goForward = defineTabTool({
response.setIncludeSnapshot();
await tab.page.goForward();
response.setIncludeSnapshot();
response.addCode(`// Navigate forward`);
response.addCode(`await page.goForward();`);
response.addSnapshot(await tab.captureSnapshot());
},
});

View File

@@ -31,8 +31,8 @@ const snapshot = defineTool({
},
handle: async (context, params, response) => {
const tab = await context.ensureTab();
response.addSnapshot(await tab.captureSnapshot());
await context.ensureTab();
response.setIncludeSnapshot();
},
});
@@ -71,12 +71,12 @@ const click = defineTabTool({
response.addCode(`await page.${await generateLocator(locator)}.click(${buttonAttr});`);
}
await tab.run(async () => {
await tab.waitForCompletion(async () => {
if (params.doubleClick)
await locator.dblclick({ button });
else
await locator.click({ button });
}, response);
});
},
});
@@ -103,9 +103,9 @@ const drag = defineTabTool({
{ ref: params.endRef, element: params.endElement },
]);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await startLocator.dragTo(endLocator);
}, response);
});
response.addCode(`await page.${await generateLocator(startLocator)}.dragTo(page.${await generateLocator(endLocator)});`);
},
@@ -127,9 +127,9 @@ const hover = defineTabTool({
const locator = await tab.refLocator(params);
response.addCode(`await page.${await generateLocator(locator)}.hover();`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await locator.hover();
}, response);
});
},
});
@@ -154,9 +154,9 @@ const selectOption = defineTabTool({
response.addCode(`// Select options [${params.values.join(', ')}] in ${params.element}`);
response.addCode(`await page.${await generateLocator(locator)}.selectOption(${javascript.formatObject(params.values)});`);
await tab.run(async () => {
await tab.waitForCompletion(async () => {
await locator.selectOption(params.values);
}, response);
});
},
});

View File

@@ -48,9 +48,8 @@ const selectTab = defineTool({
},
handle: async (context, params, response) => {
const tab = await context.selectTab(params.index);
await context.selectTab(params.index);
response.setIncludeSnapshot();
response.addSnapshot(await tab.captureSnapshot());
},
});
@@ -71,9 +70,7 @@ const newTab = defineTool({
const tab = await context.newTab();
if (params.url)
await tab.navigate(params.url);
response.setIncludeSnapshot();
response.addSnapshot(await tab.captureSnapshot());
},
});
@@ -92,10 +89,7 @@ const closeTab = defineTool({
handle: async (context, params, response) => {
await context.closeTab(params.index);
response.setIncludeTabs();
response.addCode(`await myPage.close();`);
if (context.tabs().length)
response.addSnapshot(await context.currentTabOrDie().captureSnapshot());
response.setIncludeSnapshot();
},
});

View File

@@ -58,7 +58,7 @@ const wait = defineTool({
}
response.addResult(`Waited for ${params.text || params.textGone || params.time}`);
response.addSnapshot(await tab.captureSnapshot());
response.setIncludeSnapshot();
},
});