chore: allow specifying output dir (#285)

Ref: https://github.com/microsoft/playwright-mcp/issues/279
This commit is contained in:
Pavel Feldman
2025-04-28 16:35:33 -07:00
committed by GitHub
parent 6e76d5e550
commit 697a69a8c2
6 changed files with 52 additions and 11 deletions

View File

@@ -14,6 +14,7 @@
* limitations under the License.
*/
import fs from 'fs';
import path from 'path';
import { chromium } from 'playwright';
@@ -23,10 +24,12 @@ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { spawn } from 'child_process';
import { TestServer } from './testserver';
import type { Config } from '../config';
type TestFixtures = {
client: Client;
visionClient: Client;
startClient: (options?: { args?: string[] }) => Promise<Client>;
startClient: (options?: { args?: string[], config?: Config }) => Promise<Client>;
wsEndpoint: string;
cdpEndpoint: string;
server: TestServer;
@@ -61,6 +64,11 @@ export const test = baseTest.extend<TestFixtures, WorkerFixtures>({
args.push(`--browser=${mcpBrowser}`);
if (options?.args)
args.push(...options.args);
if (options?.config) {
const configFile = testInfo.outputPath('config.json');
await fs.promises.writeFile(configFile, JSON.stringify(options.config, null, 2));
args.push(`--config=${configFile}`);
}
const transport = new StdioClientTransport({
command: 'node',
args: [path.join(__dirname, '../cli.js'), ...args],

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
import fs from 'fs';
import { test, expect } from './fixtures';
test('browser_take_screenshot (viewport)', async ({ client }) => {
@@ -70,3 +72,24 @@ test('browser_take_screenshot (element)', async ({ client }) => {
],
});
});
test('browser_take_screenshot (outputDir)', async ({ startClient }, testInfo) => {
const outputDir = testInfo.outputPath('output');
const client = await startClient({
config: { outputDir },
});
expect(await client.callTool({
name: 'browser_navigate',
arguments: {
url: 'data:text/html,<html><title>Title</title><body>Hello, world!</body></html>',
},
})).toContainTextContent(`Navigate to data:text/html`);
await client.callTool({
name: 'browser_take_screenshot',
arguments: {},
});
expect(fs.existsSync(outputDir)).toBeTruthy();
expect([...fs.readdirSync(outputDir)]).toHaveLength(1);
});