Skip to content

Commit 9eefb6e

Browse files
authored
fix: allow screenshot capture when filename is an empty string (#37678)
1 parent 766a93b commit 9eefb6e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

packages/playwright/src/mcp/browser/tools/screenshot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const screenshot = defineTabTool({
5151
throw new Error('fullPage cannot be used with element screenshots.');
5252

5353
const fileType = params.type || 'png';
54-
const fileName = await tab.context.outputFile(params.filename ?? dateAsFileName(fileType), { origin: 'llm', reason: 'Saving screenshot' });
54+
const fileName = await tab.context.outputFile(params.filename || dateAsFileName(fileType), { origin: 'llm', reason: 'Saving screenshot' });
5555
const options: playwright.PageScreenshotOptions = {
5656
type: fileType,
5757
quality: fileType === 'png' ? undefined : 90,

tests/mcp/screenshot.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,49 @@ test('browser_take_screenshot (default type should be png)', async ({ startClien
176176
expect(files[0]).toMatch(/^page-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z\.png$/);
177177
});
178178

179+
test('browser_take_screenshot (filename is empty string)', async ({ startClient, server }, testInfo) => {
180+
const outputDir = testInfo.outputPath('output');
181+
const { client } = await startClient({
182+
config: { outputDir },
183+
});
184+
expect(await client.callTool({
185+
name: 'browser_navigate',
186+
arguments: { url: server.HELLO_WORLD },
187+
})).toHaveResponse({
188+
code: expect.stringContaining(`page.goto('http://localhost`),
189+
});
190+
191+
expect(await client.callTool({
192+
name: 'browser_take_screenshot',
193+
arguments: {
194+
filename: '',
195+
},
196+
})).toEqual({
197+
content: [
198+
{
199+
text: expect.stringMatching(
200+
new RegExp(`page-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}\\-\\d{3}Z\\.png`)
201+
),
202+
type: 'text',
203+
},
204+
{
205+
data: expect.any(String),
206+
mimeType: 'image/png',
207+
type: 'image',
208+
},
209+
],
210+
});
211+
212+
const files = [...fs.readdirSync(outputDir)].filter(f => f.endsWith('.png'));
213+
214+
expect(fs.existsSync(outputDir)).toBeTruthy();
215+
expect(files).toHaveLength(1);
216+
expect(files[0]).toMatch(
217+
new RegExp(`^page-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}-\\d{3}Z\\.png$`)
218+
);
219+
});
220+
221+
179222
test('browser_take_screenshot (filename: "output.png")', async ({ startClient, server }, testInfo) => {
180223
const outputDir = testInfo.outputPath('output');
181224
const { client } = await startClient({

0 commit comments

Comments
 (0)