mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2025-07-25 07:52:27 +08:00
fix: browser_take_screenshot to not require snapshot unless element is specified (#725)
This commit is contained in:
parent
efe3ff0c7c
commit
eeeab4f042
@ -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)});`);
|
||||
|
@ -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',
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user