feat: browser_resize (#92)

This commit is contained in:
Simon Knott 2025-04-15 01:09:48 +02:00 committed by GitHub
parent 77080e8ca4
commit e729494bd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 4 deletions

View File

@ -32,7 +32,7 @@ import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
import type { LaunchOptions } from 'playwright';
const snapshotTools: Tool[] = [
...common,
...common(true),
...files(true),
...install,
...keyboard(true),
@ -43,7 +43,7 @@ const snapshotTools: Tool[] = [
];
const screenshotTools: Tool[] = [
...common,
...common(false),
...files(false),
...install,
...keyboard(false),

View File

@ -17,7 +17,7 @@
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
import type { Tool } from './tool';
import type { Tool, ToolFactory } from './tool';
const waitSchema = z.object({
time: z.number().describe('The time to wait in seconds'),
@ -62,7 +62,34 @@ const close: Tool = {
},
};
export default [
const resizeSchema = z.object({
width: z.number().describe('Width of the browser window'),
height: z.number().describe('Height of the browser window'),
});
const resize: ToolFactory = captureSnapshot => ({
capability: 'core',
schema: {
name: 'browser_resize',
description: 'Resize the browser window',
inputSchema: zodToJsonSchema(resizeSchema),
},
handle: async (context, params) => {
const validatedParams = resizeSchema.parse(params);
const tab = context.currentTab();
return await tab.run(
tab => tab.page.setViewportSize({ width: validatedParams.width, height: validatedParams.height }),
{
status: `Resized browser window`,
captureSnapshot,
}
);
},
});
export default (captureSnapshot: boolean) => [
close,
wait,
resize(captureSnapshot)
];

View File

@ -233,3 +233,22 @@ test('browser_type (slowly)', async ({ client }) => {
].join('\n'),
}]);
});
test('browser_resize', async ({ client }) => {
await client.callTool({
name: 'browser_navigate',
arguments: {
url: 'data:text/html,<html><title>Resize Test</title><body><div id="size">Waiting for resize...</div><script>new ResizeObserver(() => { document.getElementById("size").textContent = `Window size: ${window.innerWidth}x${window.innerHeight}`; }).observe(document.body);</script></body></html>',
},
});
const response = await client.callTool({
name: 'browser_resize',
arguments: {
width: 390,
height: 780,
},
});
expect(response).toContainTextContent('Resized browser window');
expect(response).toContainTextContent('Window size: 390x780');
});

View File

@ -32,6 +32,7 @@ test('test snapshot tool list', async ({ client }) => {
'browser_navigate',
'browser_pdf_save',
'browser_press_key',
'browser_resize',
'browser_snapshot',
'browser_tab_close',
'browser_tab_list',
@ -53,6 +54,7 @@ test('test vision tool list', async ({ visionClient }) => {
'browser_navigate',
'browser_pdf_save',
'browser_press_key',
'browser_resize',
'browser_screen_capture',
'browser_screen_click',
'browser_screen_drag',