mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2025-07-25 07:52:27 +08:00
281 lines
7.9 KiB
TypeScript
281 lines
7.9 KiB
TypeScript
/**
|
|
* Copyright (c) Microsoft Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import fs from 'fs';
|
|
|
|
import { test, expect } from './fixtures.js';
|
|
|
|
test('browser_take_screenshot (viewport)', async ({ startClient, server }, testInfo) => {
|
|
const { client } = await startClient({
|
|
config: { outputDir: testInfo.outputPath('output') },
|
|
});
|
|
expect(await client.callTool({
|
|
name: 'browser_navigate',
|
|
arguments: { url: server.HELLO_WORLD },
|
|
})).toContainTextContent(`Navigate to http://localhost`);
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
})).toEqual({
|
|
content: [
|
|
{
|
|
text: expect.stringContaining(`Screenshot viewport and save it as`),
|
|
type: 'text',
|
|
},
|
|
{
|
|
data: expect.any(String),
|
|
mimeType: 'image/jpeg',
|
|
type: 'image',
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('browser_take_screenshot (element)', async ({ startClient, server }, testInfo) => {
|
|
const { client } = await startClient({
|
|
config: { outputDir: testInfo.outputPath('output') },
|
|
});
|
|
expect(await client.callTool({
|
|
name: 'browser_navigate',
|
|
arguments: { url: server.HELLO_WORLD },
|
|
})).toContainTextContent(`[ref=e1]`);
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
arguments: {
|
|
element: 'hello button',
|
|
ref: 'e1',
|
|
},
|
|
})).toEqual({
|
|
content: [
|
|
{
|
|
text: expect.stringContaining(`page.getByText('Hello, world!').screenshot`),
|
|
type: 'text',
|
|
},
|
|
{
|
|
data: expect.any(String),
|
|
mimeType: 'image/jpeg',
|
|
type: 'image',
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('--output-dir should work', 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 },
|
|
})).toContainTextContent(`Navigate to http://localhost`);
|
|
|
|
await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
});
|
|
|
|
expect(fs.existsSync(outputDir)).toBeTruthy();
|
|
const files = [...fs.readdirSync(outputDir)].filter(f => f.endsWith('.jpeg'));
|
|
expect(files).toHaveLength(1);
|
|
expect(files[0]).toMatch(/^page-\d{4}-\d{2}-\d{2}T\d{2}-\d{2}-\d{2}-\d{3}Z\.jpeg$/);
|
|
});
|
|
|
|
for (const raw of [undefined, true]) {
|
|
test(`browser_take_screenshot (raw: ${raw})`, async ({ startClient, server }, testInfo) => {
|
|
const outputDir = testInfo.outputPath('output');
|
|
const ext = raw ? 'png' : 'jpeg';
|
|
const { client } = await startClient({
|
|
config: { outputDir },
|
|
});
|
|
expect(await client.callTool({
|
|
name: 'browser_navigate',
|
|
arguments: { url: server.PREFIX },
|
|
})).toContainTextContent(`Navigate to http://localhost`);
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
arguments: { raw },
|
|
})).toEqual({
|
|
content: [
|
|
{
|
|
text: expect.stringMatching(
|
|
new RegExp(`page-\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2}\\-\\d{3}Z\\.${ext}`)
|
|
),
|
|
type: 'text',
|
|
},
|
|
{
|
|
data: expect.any(String),
|
|
mimeType: `image/${ext}`,
|
|
type: 'image',
|
|
},
|
|
],
|
|
});
|
|
|
|
const files = [...fs.readdirSync(outputDir)].filter(f => f.endsWith(`.${ext}`));
|
|
|
|
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\\.${ext}$`)
|
|
);
|
|
});
|
|
|
|
}
|
|
|
|
test('browser_take_screenshot (filename: "output.jpeg")', 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 },
|
|
})).toContainTextContent(`Navigate to http://localhost`);
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
arguments: {
|
|
filename: 'output.jpeg',
|
|
},
|
|
})).toEqual({
|
|
content: [
|
|
{
|
|
text: expect.stringContaining(`output.jpeg`),
|
|
type: 'text',
|
|
},
|
|
{
|
|
data: expect.any(String),
|
|
mimeType: 'image/jpeg',
|
|
type: 'image',
|
|
},
|
|
],
|
|
});
|
|
|
|
const files = [...fs.readdirSync(outputDir)].filter(f => f.endsWith('.jpeg'));
|
|
|
|
expect(fs.existsSync(outputDir)).toBeTruthy();
|
|
expect(files).toHaveLength(1);
|
|
expect(files[0]).toMatch(/^output\.jpeg$/);
|
|
});
|
|
|
|
test('browser_take_screenshot (imageResponses=omit)', async ({ startClient, server }, testInfo) => {
|
|
const outputDir = testInfo.outputPath('output');
|
|
const { client } = await startClient({
|
|
config: {
|
|
outputDir,
|
|
imageResponses: 'omit',
|
|
},
|
|
});
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_navigate',
|
|
arguments: { url: server.HELLO_WORLD },
|
|
})).toContainTextContent(`Navigate to http://localhost`);
|
|
|
|
await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
});
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
})).toEqual({
|
|
content: [
|
|
{
|
|
text: expect.stringContaining(`Screenshot viewport and save it as`),
|
|
type: 'text',
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('browser_take_screenshot (fullPage: true)', async ({ startClient, server }, testInfo) => {
|
|
const { client } = await startClient({
|
|
config: { outputDir: testInfo.outputPath('output') },
|
|
});
|
|
expect(await client.callTool({
|
|
name: 'browser_navigate',
|
|
arguments: { url: server.HELLO_WORLD },
|
|
})).toContainTextContent(`Navigate to http://localhost`);
|
|
|
|
expect(await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
arguments: { fullPage: true },
|
|
})).toEqual({
|
|
content: [
|
|
{
|
|
text: expect.stringContaining(`Screenshot full page and save it as`),
|
|
type: 'text',
|
|
},
|
|
{
|
|
data: expect.any(String),
|
|
mimeType: 'image/jpeg',
|
|
type: 'image',
|
|
},
|
|
],
|
|
});
|
|
});
|
|
|
|
test('browser_take_screenshot (fullPage with element should error)', async ({ startClient, server }, testInfo) => {
|
|
const { client } = await startClient({
|
|
config: { outputDir: testInfo.outputPath('output') },
|
|
});
|
|
expect(await client.callTool({
|
|
name: 'browser_navigate',
|
|
arguments: { url: server.HELLO_WORLD },
|
|
})).toContainTextContent(`[ref=e1]`);
|
|
|
|
const result = await client.callTool({
|
|
name: 'browser_take_screenshot',
|
|
arguments: {
|
|
fullPage: true,
|
|
element: 'hello button',
|
|
ref: 'e1',
|
|
},
|
|
});
|
|
|
|
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: [
|
|
{
|
|
text: expect.stringContaining(`Screenshot viewport and save it as`),
|
|
type: 'text',
|
|
},
|
|
{
|
|
data: expect.any(String),
|
|
mimeType: 'image/jpeg',
|
|
type: 'image',
|
|
},
|
|
],
|
|
});
|
|
});
|