Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: allow screenshot capture when filename is an empty string (#37678)

おがどら9eefb6e7dc7e
diff --git a/packages/playwright/src/mcp/browser/tools/screenshot.ts b/packages/playwright/src/mcp/browser/tools/screenshot.ts
index 0b357a7dc2..596cb7d194 100644
--- a/packages/playwright/src/mcp/browser/tools/screenshot.ts
+++ b/packages/playwright/src/mcp/browser/tools/screenshot.ts
@@ -51,7 +51,7 @@ const screenshot = defineTabTool({
throw new Error('fullPage cannot be used with element screenshots.');
const fileType = params.type || 'png';
- const fileName = await tab.context.outputFile(params.filename ?? dateAsFileName(fileType), { origin: 'llm', reason: 'Saving screenshot' });
+ const fileName = await tab.context.outputFile(params.filename || dateAsFileName(fileType), { origin: 'llm', reason: 'Saving screenshot' });
const options: playwright.PageScreenshotOptions = {
type: fileType,
quality: fileType === 'png' ? undefined : 90,
diff --git a/tests/mcp/screenshot.spec.ts b/tests/mcp/screenshot.spec.ts
index 18dbc5dbbc..2fb287e81f 100644
--- a/tests/mcp/screenshot.spec.ts
+++ b/tests/mcp/screenshot.spec.ts
@@ -176,6 +176,49 @@ test('browser_take_screenshot (default type should be png)', async ({ startClien
expect(files[0]).toMatch(/^page-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z\.png$/);
});
+test('browser_take_screenshot (filename is empty string)', async ({ startClient, server }, testInfo) => {
+ const outputDir = testInfo.outputPath('output');
+ const { client } = await startClient({
+ config: { outputDir },
+ });
+ expect(await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: server.HELLO_WORLD },
+ })).toHaveResponse({
+ code: expect.stringContaining(`page.goto('http://localhost`),
+ });
+
+ expect(await client.callTool({
+ name: 'browser_take_screenshot',
+ arguments: {
+ filename: '',
+ },
+ })).toEqual({
+ content: [
+ {
+ text: expect.stringMatching(
+ new RegExp(`page-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}\\-\\d{3}Z\\.png`)
+ ),
+ type: 'text',
+ },
+ {
+ data: expect.any(String),
+ mimeType: 'image/png',
+ type: 'image',
+ },
+ ],
+ });
+
+ const files = [...fs.readdirSync(outputDir)].filter(f => f.endsWith('.png'));
+
+ expect(fs.existsSync(outputDir)).toBeTruthy();
+ expect(files).toHaveLength(1);
+ expect(files[0]).toMatch(
+ new RegExp(`^page-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}-\\d{3}Z\\.png$`)
+ );
+});
+
+
test('browser_take_screenshot (filename: "output.png")', async ({ startClient, server }, testInfo) => {
const outputDir = testInfo.outputPath('output');
const { client } = await startClient({