Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
chore(cli): tray icon (#39218)
Pavel Feldman19b4dcd33d4a
diff --git a/package-lock.json b/package-lock.jsonindex d152d379aa..d589976358 100644--- a/package-lock.json+++ b/package-lock.json@@ -22,6 +22,7 @@"@modelcontextprotocol/sdk": "^1.26.0","@octokit/graphql-schema": "^15.26.0","@stylistic/eslint-plugin": "^5.2.3",+ "@trayjs/trayjs": "^0.0.9","@types/codemirror": "^5.60.7","@types/formidable": "^2.0.4","@types/mdast": "^4.0.4",@@ -1757,6 +1758,105 @@"node": ">=10"}},+ "node_modules/@trayjs/darwin-arm64": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/darwin-arm64/-/darwin-arm64-0.0.9.tgz",+ "integrity": "sha512-VE94XlLstnKIuO+OmDQNUJLiw1xF9t1QEtHwpisRIAYGinL6Zw4Icvx8gFdMNSKV2PVELfMrEZ1cFugggQENZA==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "darwin"+ ]+ },+ "node_modules/@trayjs/darwin-x64": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/darwin-x64/-/darwin-x64-0.0.9.tgz",+ "integrity": "sha512-o2bI0oZmX5QegmgJrko9Lvv414Og44iR4Ul3NMN+xyr7P+enOl4WnyqD+x+02ITXLS7sqEEpj3/q4KKo0soTwA==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "darwin"+ ]+ },+ "node_modules/@trayjs/linux-arm64": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/linux-arm64/-/linux-arm64-0.0.9.tgz",+ "integrity": "sha512-d4XDkP4DmquyDyI4im5AHZIFyqrVNywL4F4x2z4FPAonOwnpHIWehui5f6FcmPauVfFY0T3PkDi8l8THykR+jw==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@trayjs/linux-x64": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/linux-x64/-/linux-x64-0.0.9.tgz",+ "integrity": "sha512-o+6JEWMXX9jPbD8zmdTxoL+PxeKi6aII2Ig1xySPtlWFwuCdE4c/FjsCn0TnSOhLsImglmy9eg1fa9GZOdj3vA==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "linux"+ ]+ },+ "node_modules/@trayjs/trayjs": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/trayjs/-/trayjs-0.0.9.tgz",+ "integrity": "sha512-on6HVfynUkSJFanUcpQKz5PZptCRtp61AV6FZHGy1lea8wPOwgIOigVJAJ4a2EQYBghsJdcVqoY1XPyFlzvQRQ==",+ "dev": true,+ "license": "Apache-2.0",+ "optionalDependencies": {+ "@trayjs/darwin-arm64": "0.0.9",+ "@trayjs/darwin-x64": "0.0.9",+ "@trayjs/linux-arm64": "0.0.9",+ "@trayjs/linux-x64": "0.0.9",+ "@trayjs/win32-arm64": "0.0.9",+ "@trayjs/win32-x64": "0.0.9"+ }+ },+ "node_modules/@trayjs/win32-arm64": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/win32-arm64/-/win32-arm64-0.0.9.tgz",+ "integrity": "sha512-2Wc5GGrVtGZFOMcyIXaJSCUaC079BRE4NdkX+PWeFAjBon8fK2Q5h3JSSslQOG/W9gXG9gzGFGUOt2w2KRI88Q==",+ "cpu": [+ "arm64"+ ],+ "dev": true,+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "win32"+ ]+ },+ "node_modules/@trayjs/win32-x64": {+ "version": "0.0.9",+ "resolved": "https://registry.npmjs.org/@trayjs/win32-x64/-/win32-x64-0.0.9.tgz",+ "integrity": "sha512-2KxmPIszxFVTsJ4h4QPv7Z89KjIGRwyA6jhB1dNblMJv5qUEfwFK766GjkmMLAi510iyhp8YrMkS+U6sD0Jblw==",+ "cpu": [+ "x64"+ ],+ "dev": true,+ "license": "Apache-2.0",+ "optional": true,+ "os": [+ "win32"+ ]+ },"node_modules/@types/babel__core": {"version": "7.20.5","resolved": "https://registry.npmjs.org/@types/babel__core/-/babel__core-7.20.5.tgz",diff --git a/package.json b/package.jsonindex 812df6211f..8eba09aa61 100644--- a/package.json+++ b/package.json@@ -62,6 +62,7 @@"@modelcontextprotocol/sdk": "^1.26.0","@octokit/graphql-schema": "^15.26.0","@stylistic/eslint-plugin": "^5.2.3",+ "@trayjs/trayjs": "^0.0.9","@types/codemirror": "^5.60.7","@types/formidable": "^2.0.4","@types/mdast": "^4.0.4",diff --git a/packages/playwright/package.json b/packages/playwright/package.jsonindex 1bc3d12b4f..2c60bd0b99 100644--- a/packages/playwright/package.json+++ b/packages/playwright/package.json@@ -27,7 +27,6 @@"./lib/mcp/sdk/bundle": "./lib/mcp/sdk/bundle.js","./lib/mcp/sdk/exports": "./lib/mcp/sdk/exports.js","./lib/mcp/terminal/program": "./lib/mcp/terminal/program.js",- "./lib/mcp/terminal/registry": "./lib/mcp/terminal/registry.js","./lib/program": "./lib/program.js","./lib/reporters/base": "./lib/reporters/base.js","./lib/reporters/list": "./lib/reporters/list.js",diff --git a/packages/playwright/src/mcp/terminal/cli.ts b/packages/playwright/src/mcp/terminal/cli.tsindex 4c40048529..6db3e40ee0 100644--- a/packages/playwright/src/mcp/terminal/cli.ts+++ b/packages/playwright/src/mcp/terminal/cli.ts@@ -16,7 +16,7 @@import { program } from './program';-program(require.resolve('../../../package.json')).catch(e => {+program().catch(e => {/* eslint-disable no-console */console.error(e.message);/* eslint-disable no-restricted-properties */diff --git a/packages/playwright/src/mcp/terminal/commands.ts b/packages/playwright/src/mcp/terminal/commands.tsindex 9349268edd..a87176600d 100644--- a/packages/playwright/src/mcp/terminal/commands.ts+++ b/packages/playwright/src/mcp/terminal/commands.ts@@ -833,6 +833,15 @@ const installBrowser = declareCommand({toolParams: () => ({}),});+const tray = declareCommand({+ name: 'tray',+ description: 'Run tray',+ category: 'config',+ hidden: true,+ toolName: '',+ toolParams: () => ({}),+});+const commandsArray: AnyCommandSchema[] = [// core categoryopen,@@ -925,6 +934,9 @@ const commandsArray: AnyCommandSchema[] = [sessionList,sessionCloseAll,killAll,++ // Hidden commands+ tray,];export const commands = Object.fromEntries(commandsArray.map(cmd => [cmd.name, cmd]));diff --git a/packages/playwright/src/mcp/terminal/icon.ico b/packages/playwright/src/mcp/terminal/icon.iconew file mode 100644index 0000000000..30bf01f485Binary files /dev/null and b/packages/playwright/src/mcp/terminal/icon.ico differdiff --git a/packages/playwright/src/mcp/terminal/icon.png b/packages/playwright/src/mcp/terminal/icon.pngnew file mode 100644index 0000000000..40eab8ca05Binary files /dev/null and b/packages/playwright/src/mcp/terminal/icon.png differdiff --git a/packages/playwright/src/mcp/terminal/program.ts b/packages/playwright/src/mcp/terminal/program.tsindex e94d418cf3..9f5e652bda 100644--- a/packages/playwright/src/mcp/terminal/program.ts+++ b/packages/playwright/src/mcp/terminal/program.ts@@ -36,7 +36,7 @@ type MinimistArgs = {[key: string]: any;};-class Session {+export class Session {readonly name: string;private _connection: SocketConnection | undefined;private _nextMessageId = 1;@@ -309,7 +309,8 @@ function resolveSessionName(sessionName?: string): string {return 'default';}-function createClientInfo(packageLocation: string): ClientInfo {+export function createClientInfo(): ClientInfo {+ const packageLocation = require.resolve('../../../package.json');const packageJSON = require(packageLocation);const workspaceDir = findWorkspaceDir(process.cwd());const version = process.env.PLAYWRIGHT_CLI_VERSION_FOR_TEST || packageJSON.version;@@ -376,8 +377,8 @@ const booleanOptions: (keyof (GlobalOptions & OpenOptions & { all?: boolean }))['version',];-export async function program(packageLocation: string) {- const clientInfo = createClientInfo(packageLocation);+export async function program() {+ const clientInfo = createClientInfo();const help = require('./help.json');const argv = process.argv.slice(2);@@ -478,6 +479,15 @@ export async function program(packageLocation: string) {case 'install':await install(args);return;+ case 'tray': {+ const daemonScript = path.join(__dirname, 'trayDaemon.js');+ const child = spawn(process.execPath, [daemonScript], {+ detached: true,+ stdio: 'ignore',+ });+ child.unref();+ return;+ }default: {const config = registry.config(clientInfo, sessionName);if (!config) {diff --git a/packages/playwright/src/mcp/terminal/trayDaemon.ts b/packages/playwright/src/mcp/terminal/trayDaemon.tsnew file mode 100644index 0000000000..d3abcc818c--- /dev/null+++ b/packages/playwright/src/mcp/terminal/trayDaemon.ts@@ -0,0 +1,132 @@+/**+ * 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 net from 'net';+import path from 'path';+import os from 'os';+import { gracefullyProcessExitDoNotHang } from 'playwright-core/lib/utils';++import { createClientInfo, Session } from './program';+import { Registry } from './registry';++import type { SessionConfig } from './registry';+import type { MenuItem } from '@trayjs/trayjs';++const socketsDir = process.env.PLAYWRIGHT_DAEMON_SOCKETS_DIR || path.join(os.tmpdir(), 'playwright-cli');++const socketPath = process.platform === 'win32'+ ? `\\\\.\\pipe\\playwright-tray-${process.env.USERNAME || 'default'}`+ : path.join(socketsDir, 'tray.sock');++function acquireSingleton(): Promise<net.Server> {+ return new Promise((resolve, reject) => {+ const server = net.createServer();+ server.listen(socketPath, () => resolve(server));+ server.on('error', (err: NodeJS.ErrnoException) => {+ if (err.code !== 'EADDRINUSE')+ return reject(err);+ const client = net.connect(socketPath, () => {+ client.destroy();+ reject(new Error('already running'));+ });+ client.on('error', () => {+ fs.unlinkSync(socketPath);+ server.listen(socketPath, () => resolve(server));+ });+ });+ });+}++let lastId = 0;+const idToConfig = new Map<string, SessionConfig>();++async function onMenuRequested(): Promise<MenuItem[]> {+ const registry = await Registry.load();+ const map = registry.configMap();+ const items: MenuItem[] = [];+ let first = true;+ idToConfig.clear();+ for (const [workspace, configs] of map) {+ if (!first)+ items.push({ id: '', separator: true });+ first = false;+ const shortName = path.basename(workspace);+ items.push({ id: '', title: shortName, enabled: false });+ for (const config of configs) {+ idToConfig.set(String(++lastId), config);+ items.push({+ id: ``,+ title: config.name,+ items: [+ { id: `close:${lastId}`, title: 'Close' },+ ],+ });+ }+ }++ if (!items.length)+ items.push({ id: '', title: 'No sessions', enabled: false });++ items.push({ id: '', separator: true });+ items.push({ id: 'quit', title: 'Quit' });+ return items;+}++async function main() {+ let server: net.Server;+ try {+ server = await acquireSingleton();+ } catch {+ return;+ }++ const { Tray } = await import('@trayjs/trayjs');++ const tray = new Tray({+ tooltip: 'Playwright',+ icon: {+ png: path.join(__dirname, 'icon.png'),+ ico: path.join(__dirname, 'icon.ico'),+ },+ onClicked: (id: string) => {+ if (id === 'quit')+ tray.quit();+ if (id.startsWith('close:'))+ session(id.substring('close:'.length))?.stop().catch(() => {});+ },+ onMenuRequested,+ });++ const shutdown = () => {+ server.close();+ tray.quit();+ };+ tray.on('ready', () => {});+ process.on('SIGINT', shutdown);+ process.on('SIGTERM', shutdown);+ tray.on('close', () => gracefullyProcessExitDoNotHang(0));+}++function session(id: string): Session | undefined {+ const config = idToConfig.get(id);+ if (!config)+ return;+ const clientInfo = createClientInfo();+ return new Session(clientInfo, config);+}++void main();diff --git a/utils/build/build.js b/utils/build/build.jsindex 4746aa92ff..a58e99096e 100644--- a/utils/build/build.js+++ b/utils/build/build.js@@ -653,6 +653,12 @@ copyFiles.push({to: 'packages/playwright/lib',});+copyFiles.push({+ files: 'packages/playwright/src/mcp/terminal/*.{png,ico}',+ from: 'packages/playwright/src',+ to: 'packages/playwright/lib',+});+if (watchMode) {// Run TypeScript for type checking.steps.push(new ProgramStep({