mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2026-02-01 08:13:38 +00:00
chore: restart browser if page closed manually (#19)
Fixes https://github.com/microsoft/playwright-mcp/issues/18
This commit is contained in:
@@ -161,3 +161,69 @@ test('test browser_click', async ({ server }) => {
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
test('test reopen browser', async ({ server }) => {
|
||||
const response2 = await server.send({
|
||||
jsonrpc: '2.0',
|
||||
id: 2,
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'browser_navigate',
|
||||
arguments: {
|
||||
url: 'data:text/html,<html><title>Title</title><body>Hello, world!</body></html>',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response2).toEqual(expect.objectContaining({
|
||||
id: 2,
|
||||
}));
|
||||
|
||||
const response3 = await server.send({
|
||||
jsonrpc: '2.0',
|
||||
id: 3,
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'browser_close',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response3).toEqual(expect.objectContaining({
|
||||
id: 3,
|
||||
result: {
|
||||
content: [{
|
||||
text: 'Page closed',
|
||||
type: 'text',
|
||||
}],
|
||||
},
|
||||
}));
|
||||
|
||||
const response4 = await server.send({
|
||||
jsonrpc: '2.0',
|
||||
id: 4,
|
||||
method: 'tools/call',
|
||||
params: {
|
||||
name: 'browser_navigate',
|
||||
arguments: {
|
||||
url: 'data:text/html,<html><title>Title</title><body>Hello, world!</body></html>',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(response4).toEqual(expect.objectContaining({
|
||||
id: 4,
|
||||
result: {
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: `
|
||||
- Page URL: data:text/html,<html><title>Title</title><body>Hello, world!</body></html>
|
||||
- Page Title: Title
|
||||
- Page Snapshot
|
||||
\`\`\`yaml
|
||||
- document [ref=s1e2]: Hello, world!
|
||||
\`\`\`
|
||||
`,
|
||||
}],
|
||||
},
|
||||
}));
|
||||
});
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
import path from 'path';
|
||||
import { spawn } from 'child_process';
|
||||
import EventEmitter from 'events';
|
||||
import { chromium } from 'playwright';
|
||||
|
||||
import { test as baseTest, expect } from '@playwright/test';
|
||||
|
||||
@@ -30,10 +31,11 @@ class MCPServer extends EventEmitter {
|
||||
private _messageResolvers: ((value: any) => void)[] = [];
|
||||
private _buffer: string = '';
|
||||
|
||||
constructor(command: string, args: string[]) {
|
||||
constructor(command: string, args: string[], options?: { env?: NodeJS.ProcessEnv }) {
|
||||
super();
|
||||
this._child = spawn(command, args, {
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
env: { ...process.env, ...options?.env },
|
||||
});
|
||||
|
||||
this._child.stdout?.on('data', data => {
|
||||
@@ -108,44 +110,64 @@ class MCPServer extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
export const test = baseTest.extend<{ server: MCPServer }>({
|
||||
server: async ({}, use) => {
|
||||
const server = new MCPServer('node', [path.join(__dirname, '../cli.js'), '--headless']);
|
||||
const initialize = await server.send({
|
||||
jsonrpc: '2.0',
|
||||
id: 0,
|
||||
method: 'initialize',
|
||||
params: {
|
||||
protocolVersion: '2024-11-05',
|
||||
capabilities: {},
|
||||
clientInfo: {
|
||||
name: 'Playwright Test',
|
||||
version: '0.0.0',
|
||||
},
|
||||
},
|
||||
});
|
||||
type Fixtures = {
|
||||
server: MCPServer;
|
||||
startServer: (options?: { env?: NodeJS.ProcessEnv }) => Promise<MCPServer>;
|
||||
wsEndpoint: string;
|
||||
};
|
||||
|
||||
expect(initialize).toEqual(expect.objectContaining({
|
||||
id: 0,
|
||||
result: expect.objectContaining({
|
||||
protocolVersion: '2024-11-05',
|
||||
capabilities: {
|
||||
tools: {},
|
||||
resources: {},
|
||||
export const test = baseTest.extend<Fixtures>({
|
||||
server: async ({ startServer }, use) => {
|
||||
await use(await startServer());
|
||||
},
|
||||
|
||||
startServer: async ({ }, use) => {
|
||||
let server: MCPServer | undefined;
|
||||
|
||||
use(async options => {
|
||||
server = new MCPServer('node', [path.join(__dirname, '../cli.js'), '--headless'], options);
|
||||
const initialize = await server.send({
|
||||
jsonrpc: '2.0',
|
||||
id: 0,
|
||||
method: 'initialize',
|
||||
params: {
|
||||
protocolVersion: '2024-11-05',
|
||||
capabilities: {},
|
||||
clientInfo: {
|
||||
name: 'Playwright Test',
|
||||
version: '0.0.0',
|
||||
},
|
||||
},
|
||||
serverInfo: expect.objectContaining({
|
||||
name: 'Playwright',
|
||||
version: expect.any(String),
|
||||
});
|
||||
|
||||
expect(initialize).toEqual(expect.objectContaining({
|
||||
id: 0,
|
||||
result: expect.objectContaining({
|
||||
protocolVersion: '2024-11-05',
|
||||
capabilities: {
|
||||
tools: {},
|
||||
resources: {},
|
||||
},
|
||||
serverInfo: expect.objectContaining({
|
||||
name: 'Playwright',
|
||||
version: expect.any(String),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}));
|
||||
}));
|
||||
|
||||
await server.sendNoReply({
|
||||
jsonrpc: '2.0',
|
||||
method: 'notifications/initialized',
|
||||
await server.sendNoReply({
|
||||
jsonrpc: '2.0',
|
||||
method: 'notifications/initialized',
|
||||
});
|
||||
return server;
|
||||
});
|
||||
|
||||
await use(server);
|
||||
await server.close();
|
||||
await server?.close();
|
||||
},
|
||||
|
||||
wsEndpoint: async ({ }, use) => {
|
||||
const browserServer = await chromium.launchServer();
|
||||
await use(browserServer.wsEndpoint());
|
||||
await browserServer.close();
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user