diff --git a/src/tools/screenshot.ts b/src/tools/screenshot.ts index 5160f2e..9544737 100644 --- a/src/tools/screenshot.ts +++ b/src/tools/screenshot.ts @@ -53,7 +53,6 @@ const screenshot = defineTool({ handle: async (context, params) => { const tab = context.currentTabOrDie(); - const snapshot = tab.snapshotOrDie(); const fileType = params.raw ? 'png' : 'jpeg'; const fileName = await outputFile(context.config, params.filename ?? `page-${new Date().toISOString()}.${fileType}`); const options: playwright.PageScreenshotOptions = { @@ -70,7 +69,8 @@ const screenshot = defineTool({ `// Screenshot ${screenshotTarget} and save it as ${fileName}`, ]; - const locator = params.ref ? snapshot.refLocator({ element: params.element || '', ref: params.ref }) : null; + // Only get snapshot when element screenshot is needed + const locator = params.ref ? tab.snapshotOrDie().refLocator({ element: params.element || '', ref: params.ref }) : null; if (locator) code.push(`await page.${await generateLocator(locator)}.screenshot(${javascript.formatObject(options)});`); diff --git a/tests/screenshot.spec.ts b/tests/screenshot.spec.ts index 78dc09d..a90ae69 100644 --- a/tests/screenshot.spec.ts +++ b/tests/screenshot.spec.ts @@ -250,3 +250,31 @@ test('browser_take_screenshot (fullPage with element should error)', async ({ st expect(result.isError).toBe(true); expect(result.content?.[0]?.text).toContain('fullPage cannot be used with element screenshots'); }); + +test('browser_take_screenshot (viewport without snapshot)', async ({ startClient, server }, testInfo) => { + const { client } = await startClient({ + config: { outputDir: testInfo.outputPath('output') }, + }); + + // Ensure we have a tab but don't navigate anywhere (no snapshot captured) + expect(await client.callTool({ + name: 'browser_tab_list', + })).toContainTextContent('about:blank'); + + // This should work without requiring a snapshot since it's a viewport screenshot + expect(await client.callTool({ + name: 'browser_take_screenshot', + })).toEqual({ + content: [ + { + data: expect.any(String), + mimeType: 'image/jpeg', + type: 'image', + }, + { + text: expect.stringContaining(`Screenshot viewport and save it as`), + type: 'text', + }, + ], + }); +});