Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ Evaluate a JavaScript expression on the page, or on a specific element when a `r

#### `browser_take_screenshot`
Take a screenshot of the current page.
- Parameters: `filename` (optional), `type` (`png` or `jpeg`), `scale` (`css` or `device`, default `css`), `fullPage` (optional), `element`/`ref` pair (for element screenshots)
- Parameters: `filename` (optional), `type` (`png`, `jpeg` or `webp`; inferred from the filename extension when omitted, otherwise `png`), `scale` (`css` or `device`, default `css`), `fullPage` (optional), `element`/`ref` pair (for element screenshots)
Comment thread
JustasMonkev marked this conversation as resolved.
Resolved
- `scale: device` captures a high-resolution screenshot using device pixels (accounts for the device pixel ratio); `scale: css` keeps the image sized in CSS pixels.

#### `browser_pdf_save`
Expand Down
2 changes: 1 addition & 1 deletion SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ These tools are always available and work in the interactive REPL.
| `browser_close` | Close the page |
| `browser_resize` | Resize window: `{"width": 1280, "height": 720}` |
| `browser_snapshot` | Capture accessibility snapshot |
| `browser_take_screenshot` | Take screenshot (png/jpeg, fullPage option) |
| `browser_take_screenshot` | Take screenshot (png/jpeg/webp, fullPage option) |
| `browser_wait_for` | Wait for text/time: `{"text": "loaded"}` or `{"time": 5}` |

### Interaction
Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"debug": "^4.4.3",
"dotenv": "^17.4.2",
"mime": "^4.1.0",
"playwright": "1.61.1",
"playwright-core": "1.61.1",
"playwright": "1.62.0",
"playwright-core": "1.62.0",
Comment thread
JustasMonkev marked this conversation as resolved.
Resolved
"re2": "^1.24.1",
"ws": "^8.21.1",
"zod": "^4.4.3"
Expand Down
13 changes: 8 additions & 5 deletions src/tools/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import path from 'node:path';

import { z } from 'zod';

import { defineTabTool } from './tool.js';
Expand All @@ -23,9 +25,9 @@ import { generateLocator } from './utils.js';
import type * as playwright from 'playwright';

const screenshotSchema = z.object({
type: z.enum(['png', 'jpeg']).default('png').describe('Image format for the screenshot. Default is png.'),
type: z.enum(['png', 'jpeg', 'webp']).optional().describe('Image format. If unset, inferred from the filename extension, otherwise png.'),
scale: z.enum(['css', 'device']).default('css').describe(`Image resolution scale. 'css' produces a screenshot sized in CSS pixels (smaller, consistent across devices). 'device' produces a high-resolution screenshot using device pixels (larger, accounts for the device pixel ratio). Default is css.`),
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified.'),
filename: z.string().optional().describe('File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg|webp}` if not specified.'),
element: z.string().optional().describe('Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too.'),
ref: z.string().optional().describe('Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too.'),
fullPage: z.boolean().optional().describe('When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.'),
Expand All @@ -52,11 +54,12 @@ const screenshot = defineTabTool({
},

handle: async (tab, params, response) => {
const fileType = params.type || 'png';
const extension = (path.extname(params.filename ?? '') || path.basename(params.filename ?? '')).toLowerCase();
const fileType = params.type ?? ({ '.jpg': 'jpeg', '.jpeg': 'jpeg', '.webp': 'webp' } as const)[extension as '.jpg' | '.jpeg' | '.webp'] ?? 'png';
const fileName = await tab.context.outputFile(params.filename ?? `page-${new Date().toISOString()}.${fileType}`);
const options: playwright.PageScreenshotOptions = {
type: fileType,
quality: fileType === 'png' ? undefined : 90,
quality: fileType === 'jpeg' ? 90 : undefined,
scale: params.scale,
path: fileName,
...(params.fullPage !== undefined && { fullPage: params.fullPage })
Expand All @@ -81,7 +84,7 @@ const screenshot = defineTabTool({
// Never return large images to LLM, saving them to the file system is enough.
if (!params.fullPage) {
response.addImage({
contentType: fileType === 'png' ? 'image/png' : 'image/jpeg',
contentType: `image/${fileType}`,
data: buffer
});
}
Expand Down
31 changes: 31 additions & 0 deletions tests/tools-screenshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,35 @@ describe('Screenshot Tools', () => {

expect(mockPage.screenshot).toHaveBeenCalledWith(expect.objectContaining({ scale: 'device' }));
});

it('should accept the webp type and pass it through without quality', async () => {
const params = screenshotTool.schema.inputSchema.parse({ type: 'webp' });
await screenshotTool.handle(mockContext, params, response);

expect(mockPage.screenshot).toHaveBeenCalledWith(expect.objectContaining({ type: 'webp', quality: undefined }));
});

it('should infer the type from the filename', async () => {
for (const [filename, type] of [['page.WEBP', 'webp'], ['.webp', 'webp'], ['.jpeg', 'jpeg'], ['.jpg', 'jpeg']] as const) {
mockPage.screenshot.mockClear();
const params = screenshotTool.schema.inputSchema.parse({ filename });
await screenshotTool.handle(mockContext, params, new Response(mockContext, 'browser_take_screenshot', {}));

expect(mockPage.screenshot).toHaveBeenCalledWith(expect.objectContaining({ type }));
}
});

it('should only set quality for jpeg screenshots', async () => {
for (const [type, quality] of [['png', undefined], ['jpeg', 90], ['webp', undefined]] as const) {
mockPage.screenshot.mockClear();
const params = screenshotTool.schema.inputSchema.parse({ type });
await screenshotTool.handle(mockContext, params, new Response(mockContext, 'browser_take_screenshot', {}));

expect(mockPage.screenshot).toHaveBeenCalledWith(expect.objectContaining({ type, quality }));
}
});

it('should reject an invalid type value', () => {
expect(() => screenshotTool.schema.inputSchema.parse({ type: 'gif' })).toThrow();
});
});