first test
Playwright Tests / test (push) Successful in 5m57s

This commit is contained in:
2026-05-02 22:34:21 +02:00
parent 47114f85f2
commit e67a4a1edf
801 changed files with 228099 additions and 15 deletions
+79
View File
@@ -0,0 +1,79 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var browserBackend_exports = {};
__export(browserBackend_exports, {
BrowserBackend: () => BrowserBackend
});
module.exports = __toCommonJS(browserBackend_exports);
var import_context = require("./context");
var import_response = require("./response");
var import_sessionLog = require("./sessionLog");
var import_utilsBundle = require("../../utilsBundle");
class BrowserBackend {
constructor(config, browserContext, tools) {
this._config = config;
this._tools = tools;
this.browserContext = browserContext;
}
async initialize(clientInfo) {
this._sessionLog = this._config.saveSession ? await import_sessionLog.SessionLog.create(this._config, clientInfo.cwd) : void 0;
this._context = new import_context.Context(this.browserContext, {
config: this._config,
sessionLog: this._sessionLog,
cwd: clientInfo.cwd
});
}
async dispose() {
await this._context?.dispose().catch((e) => (0, import_utilsBundle.debug)("pw:tools:error")(e));
}
async callTool(name, rawArguments = {}) {
const tool = this._tools.find((tool2) => tool2.schema.name === name);
if (!tool) {
return {
content: [{ type: "text", text: `### Error
Tool "${name}" not found` }],
isError: true
};
}
const parsedArguments = tool.schema.inputSchema.parse(rawArguments);
const cwd = rawArguments._meta?.cwd;
const context = this._context;
const response = new import_response.Response(context, name, parsedArguments, cwd);
context.setRunningTool(name);
let responseObject;
try {
await tool.handle(context, parsedArguments, response);
responseObject = await response.serialize();
this._sessionLog?.logResponse(name, parsedArguments, responseObject);
} catch (error) {
return {
content: [{ type: "text", text: `### Error
${String(error)}` }],
isError: true
};
} finally {
context.setRunningTool(void 0);
}
return responseObject;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BrowserBackend
});
+63
View File
@@ -0,0 +1,63 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var common_exports = {};
__export(common_exports, {
default: () => common_default
});
module.exports = __toCommonJS(common_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
var import_response = require("./response");
const close = (0, import_tool.defineTool)({
capability: "core",
schema: {
name: "browser_close",
title: "Close browser",
description: "Close the page",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (context, params, response) => {
const result = (0, import_response.renderTabsMarkdown)([]);
response.addTextResult(result.join("\n"));
response.addCode(`await page.close()`);
response.setClose();
}
});
const resize = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_resize",
title: "Resize browser window",
description: "Resize the browser window",
inputSchema: import_zodBundle.z.object({
width: import_zodBundle.z.number().describe("Width of the browser window"),
height: import_zodBundle.z.number().describe("Height of the browser window")
}),
type: "action"
},
handle: async (tab, params, response) => {
response.addCode(`await page.setViewportSize({ width: ${params.width}, height: ${params.height} });`);
await tab.page.setViewportSize({ width: params.width, height: params.height });
}
});
var common_default = [
close,
resize
];
+41
View File
@@ -0,0 +1,41 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var config_exports = {};
__export(config_exports, {
default: () => config_default
});
module.exports = __toCommonJS(config_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const configShow = (0, import_tool.defineTool)({
capability: "config",
schema: {
name: "browser_get_config",
title: "Get config",
description: "Get the final resolved config after merging CLI options, environment variables and config file.",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (context, params, response) => {
response.addTextResult(JSON.stringify(context.config, null, 2));
}
});
var config_default = [
configShow
];
+66
View File
@@ -0,0 +1,66 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var console_exports = {};
__export(console_exports, {
default: () => console_default
});
module.exports = __toCommonJS(console_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const console = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_console_messages",
title: "Get console messages",
description: "Returns all console messages",
inputSchema: import_zodBundle.z.object({
level: import_zodBundle.z.enum(["error", "warning", "info", "debug"]).default("info").describe('Level of the console messages to return. Each level includes the messages of more severe levels. Defaults to "info".'),
all: import_zodBundle.z.boolean().optional().describe("Return all console messages since the beginning of the session, not just since the last navigation. Defaults to false."),
filename: import_zodBundle.z.string().optional().describe("Filename to save the console messages to. If not provided, messages are returned as text.")
}),
type: "readOnly"
},
handle: async (tab, params, response) => {
const count = await tab.consoleMessageCount();
const header = [`Total messages: ${count.total} (Errors: ${count.errors}, Warnings: ${count.warnings})`];
const messages = await tab.consoleMessages(params.level, params.all);
if (messages.length !== count.total)
header.push(`Returning ${messages.length} messages for level "${params.level}"`);
const text = [...header, "", ...messages.map((message) => message.toString())].join("\n");
await response.addResult("Console", text, { prefix: "console", ext: "log", suggestedFilename: params.filename });
}
});
const consoleClear = (0, import_tool.defineTabTool)({
capability: "core",
skillOnly: true,
schema: {
name: "browser_console_clear",
title: "Clear console messages",
description: "Clear all console messages",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (tab) => {
await tab.clearConsoleMessages();
}
});
var console_default = [
console,
consoleClear
];
+296
View File
@@ -0,0 +1,296 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var context_exports = {};
__export(context_exports, {
Context: () => Context,
outputDir: () => outputDir,
outputFile: () => outputFile,
workspaceFile: () => workspaceFile
});
module.exports = __toCommonJS(context_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_utilsBundle = require("../../utilsBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import__ = require("../../..");
var import_tab = require("./tab");
var import_disposable = require("../../server/utils/disposable");
var import_eventsHelper = require("../../server/utils/eventsHelper");
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
class Context {
constructor(browserContext, options) {
this._tabs = [];
this._routes = [];
this._disposables = [];
this.config = options.config;
this.sessionLog = options.sessionLog;
this.options = options;
this._rawBrowserContext = browserContext;
testDebug("create context");
}
async dispose() {
await (0, import_disposable.disposeAll)(this._disposables);
for (const tab of this._tabs)
await tab.dispose();
this._tabs.length = 0;
this._currentTab = void 0;
await this.stopVideoRecording();
}
debugger() {
return this._rawBrowserContext.debugger;
}
tabs() {
return this._tabs;
}
currentTab() {
return this._currentTab;
}
currentTabOrDie() {
if (!this._currentTab)
throw new Error("No open pages available.");
return this._currentTab;
}
async newTab() {
const browserContext = await this.ensureBrowserContext();
const page = await browserContext.newPage();
this._currentTab = this._tabs.find((t) => t.page === page);
return this._currentTab;
}
async selectTab(index) {
const tab = this._tabs[index];
if (!tab)
throw new Error(`Tab ${index} not found`);
await tab.page.bringToFront();
this._currentTab = tab;
return tab;
}
async ensureTab() {
const browserContext = await this.ensureBrowserContext();
if (!this._currentTab)
await browserContext.newPage();
return this._currentTab;
}
async closeTab(index) {
const tab = index === void 0 ? this._currentTab : this._tabs[index];
if (!tab)
throw new Error(`Tab ${index} not found`);
const url = tab.page.url();
await tab.page.close();
return url;
}
async workspaceFile(fileName, perCallWorkspaceDir) {
return await workspaceFile(this.options, fileName, perCallWorkspaceDir);
}
async outputFile(template, options) {
const baseName = template.suggestedFilename || `${template.prefix}-${(template.date ?? /* @__PURE__ */ new Date()).toISOString().replace(/[:.]/g, "-")}${template.ext ? "." + template.ext : ""}`;
return await outputFile(this.options, baseName, options);
}
async startVideoRecording(fileName, params) {
if (this._video)
throw new Error("Video recording has already been started.");
this._video = { params, fileName, fileNames: [] };
const browserContext = await this.ensureBrowserContext();
for (const page of browserContext.pages())
await this._startPageVideo(page);
}
async stopVideoRecording() {
if (!this._video)
return [];
const video = this._video;
for (const page of this._rawBrowserContext.pages())
await page.screencast.stop();
this._video = void 0;
return [...video.fileNames];
}
async _startPageVideo(page) {
if (!this._video)
return;
const suffix = this._video.fileNames.length ? `-${this._video.fileNames.length}` : "";
let fileName = this._video.fileName;
if (fileName && suffix) {
const ext = import_path.default.extname(fileName);
fileName = import_path.default.basename(fileName, ext) + suffix + ext;
}
this._video.fileNames.push(fileName);
await page.screencast.start({ path: fileName, ...this._video.params });
}
_onPageCreated(page) {
const tab = new import_tab.Tab(this, page, (tab2) => this._onPageClosed(tab2));
this._tabs.push(tab);
if (!this._currentTab)
this._currentTab = tab;
this._startPageVideo(page).catch(() => {
});
}
_onPageClosed(tab) {
const index = this._tabs.indexOf(tab);
if (index === -1)
return;
this._tabs.splice(index, 1);
if (this._currentTab === tab)
this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)];
}
routes() {
return this._routes;
}
async addRoute(entry) {
const browserContext = await this.ensureBrowserContext();
await browserContext.route(entry.pattern, entry.handler);
this._routes.push(entry);
}
async removeRoute(pattern) {
let removed = 0;
const browserContext = await this.ensureBrowserContext();
if (pattern) {
const toRemove = this._routes.filter((r) => r.pattern === pattern);
for (const route of toRemove)
await browserContext.unroute(route.pattern, route.handler);
this._routes = this._routes.filter((r) => r.pattern !== pattern);
removed = toRemove.length;
} else {
for (const route of this._routes)
await browserContext.unroute(route.pattern, route.handler);
removed = this._routes.length;
this._routes = [];
}
return removed;
}
isRunningTool() {
return this._runningToolName !== void 0;
}
setRunningTool(name) {
this._runningToolName = name;
}
async _setupRequestInterception(context) {
if (this.config.network?.allowedOrigins?.length) {
this._disposables.push(await context.route("**", (route) => route.abort("blockedbyclient")));
for (const origin of this.config.network.allowedOrigins) {
const glob = originOrHostGlob(origin);
this._disposables.push(await context.route(glob, (route) => route.continue()));
}
}
if (this.config.network?.blockedOrigins?.length) {
for (const origin of this.config.network.blockedOrigins)
this._disposables.push(await context.route(originOrHostGlob(origin), (route) => route.abort("blockedbyclient")));
}
}
async ensureBrowserContext() {
if (this._browserContextPromise)
return this._browserContextPromise;
this._browserContextPromise = this._initializeBrowserContext();
return this._browserContextPromise;
}
async _initializeBrowserContext() {
if (this.config.testIdAttribute)
import__.selectors.setTestIdAttribute(this.config.testIdAttribute);
const browserContext = this._rawBrowserContext;
await this._setupRequestInterception(browserContext);
if (this.config.saveTrace) {
await browserContext.tracing.start({
name: "trace-" + Date.now(),
screenshots: true,
snapshots: true,
live: true
});
this._disposables.push({
dispose: async () => {
await browserContext.tracing.stop();
}
});
}
for (const initScript of this.config.browser?.initScript || [])
this._disposables.push(await browserContext.addInitScript({ path: import_path.default.resolve(this.options.cwd, initScript) }));
for (const page of browserContext.pages())
this._onPageCreated(page);
this._disposables.push(import_eventsHelper.eventsHelper.addEventListener(browserContext, "page", (page) => this._onPageCreated(page)));
return browserContext;
}
checkUrlAllowed(url) {
if (this.config.allowUnrestrictedFileAccess)
return;
if (!URL.canParse(url))
return;
if (new URL(url).protocol === "file:")
throw new Error(`Access to "file:" protocol is blocked. Attempted URL: "${url}"`);
}
lookupSecret(secretName) {
if (!this.config.secrets?.[secretName])
return { value: secretName, code: (0, import_stringUtils.escapeWithQuotes)(secretName, "'") };
return {
value: this.config.secrets[secretName],
code: `process.env['${secretName}']`
};
}
}
function originOrHostGlob(originOrHost) {
const wildcardPortMatch = originOrHost.match(/^(https?:\/\/[^/:]+):\*$/);
if (wildcardPortMatch)
return `${wildcardPortMatch[1]}:*/**`;
try {
const url = new URL(originOrHost);
if (url.origin !== "null")
return `${url.origin}/**`;
} catch {
}
return `*://${originOrHost}/**`;
}
async function workspaceFile(options, fileName, perCallWorkspaceDir) {
const workspace = perCallWorkspaceDir ?? options.cwd;
const resolvedName = import_path.default.resolve(workspace, fileName);
await checkFile(options, resolvedName, { origin: "llm" });
return resolvedName;
}
function outputDir(options) {
if (options.config.outputDir)
return import_path.default.resolve(options.config.outputDir);
return import_path.default.resolve(options.cwd, options.config.skillMode ? ".playwright-cli" : ".playwright-mcp");
}
async function outputFile(options, fileName, flags) {
const resolvedFile = import_path.default.resolve(outputDir(options), fileName);
await checkFile(options, resolvedFile, flags);
await import_fs.default.promises.mkdir(import_path.default.dirname(resolvedFile), { recursive: true });
(0, import_utilsBundle.debug)("pw:mcp:file")(resolvedFile);
return resolvedFile;
}
async function checkFile(options, resolvedFilename, flags) {
if (flags.origin === "code" || options.config.allowUnrestrictedFileAccess)
return;
const output = outputDir(options);
const workspace = options.cwd;
const withinDir = (root) => resolvedFilename === root || resolvedFilename.startsWith(root + import_path.default.sep);
if (!withinDir(output) && !withinDir(workspace))
throw new Error(`File access denied: ${resolvedFilename} is outside allowed roots. Allowed roots: ${output}, ${workspace}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Context,
outputDir,
outputFile,
workspaceFile
});
+152
View File
@@ -0,0 +1,152 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cookies_exports = {};
__export(cookies_exports, {
default: () => cookies_default
});
module.exports = __toCommonJS(cookies_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const cookieList = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_cookie_list",
title: "List cookies",
description: "List all cookies (optionally filtered by domain/path)",
inputSchema: import_zodBundle.z.object({
domain: import_zodBundle.z.string().optional().describe("Filter cookies by domain"),
path: import_zodBundle.z.string().optional().describe("Filter cookies by path")
}),
type: "readOnly"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
let cookies = await browserContext.cookies();
if (params.domain)
cookies = cookies.filter((c) => c.domain.includes(params.domain));
if (params.path)
cookies = cookies.filter((c) => c.path.startsWith(params.path));
if (cookies.length === 0)
response.addTextResult("No cookies found");
else
response.addTextResult(cookies.map((c) => `${c.name}=${c.value} (domain: ${c.domain}, path: ${c.path})`).join("\n"));
response.addCode(`await page.context().cookies();`);
}
});
const cookieGet = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_cookie_get",
title: "Get cookie",
description: "Get a specific cookie by name",
inputSchema: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Cookie name to get")
}),
type: "readOnly"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const cookies = await browserContext.cookies();
const cookie = cookies.find((c) => c.name === params.name);
if (!cookie)
response.addTextResult(`Cookie '${params.name}' not found`);
else
response.addTextResult(`${cookie.name}=${cookie.value} (domain: ${cookie.domain}, path: ${cookie.path}, httpOnly: ${cookie.httpOnly}, secure: ${cookie.secure}, sameSite: ${cookie.sameSite})`);
response.addCode(`await page.context().cookies();`);
}
});
const cookieSet = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_cookie_set",
title: "Set cookie",
description: "Set a cookie with optional flags (domain, path, expires, httpOnly, secure, sameSite)",
inputSchema: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Cookie name"),
value: import_zodBundle.z.string().describe("Cookie value"),
domain: import_zodBundle.z.string().optional().describe("Cookie domain"),
path: import_zodBundle.z.string().optional().describe("Cookie path"),
expires: import_zodBundle.z.number().optional().describe("Cookie expiration as Unix timestamp"),
httpOnly: import_zodBundle.z.boolean().optional().describe("Whether the cookie is HTTP only"),
secure: import_zodBundle.z.boolean().optional().describe("Whether the cookie is secure"),
sameSite: import_zodBundle.z.enum(["Strict", "Lax", "None"]).optional().describe("Cookie SameSite attribute")
}),
type: "action"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const tab = await context.ensureTab();
const url = new URL(tab.page.url());
const cookie = {
name: params.name,
value: params.value,
domain: params.domain || url.hostname,
path: params.path || "/"
};
if (params.expires !== void 0)
cookie.expires = params.expires;
if (params.httpOnly !== void 0)
cookie.httpOnly = params.httpOnly;
if (params.secure !== void 0)
cookie.secure = params.secure;
if (params.sameSite !== void 0)
cookie.sameSite = params.sameSite;
await browserContext.addCookies([cookie]);
response.addCode(`await page.context().addCookies([${JSON.stringify(cookie)}]);`);
}
});
const cookieDelete = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_cookie_delete",
title: "Delete cookie",
description: "Delete a specific cookie",
inputSchema: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Cookie name to delete")
}),
type: "action"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
await browserContext.clearCookies({ name: params.name });
response.addCode(`await page.context().clearCookies({ name: '${params.name}' });`);
}
});
const cookieClear = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_cookie_clear",
title: "Clear cookies",
description: "Clear all cookies",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
await browserContext.clearCookies();
response.addCode(`await page.context().clearCookies();`);
}
});
var cookies_default = [
cookieList,
cookieGet,
cookieSet,
cookieDelete,
cookieClear
];
+69
View File
@@ -0,0 +1,69 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var devtools_exports = {};
__export(devtools_exports, {
default: () => devtools_default
});
module.exports = __toCommonJS(devtools_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const resume = (0, import_tool.defineTool)({
capability: "devtools",
schema: {
name: "browser_resume",
title: "Resume paused script execution",
description: "Resume script execution after it was paused. When called with step set to true, execution will pause again before the next action.",
inputSchema: import_zodBundle.z.object({
step: import_zodBundle.z.boolean().optional().describe("When true, execution will pause again before the next action, allowing step-by-step debugging."),
location: import_zodBundle.z.string().optional().describe('Pause execution at a specific <file>:<line>, e.g. "example.spec.ts:42".')
}),
type: "action"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const pausedPromise = new Promise((resolve) => {
const listener = () => {
if (browserContext.debugger.pausedDetails()) {
browserContext.debugger.off("pausedstatechanged", listener);
resolve();
}
};
browserContext.debugger.on("pausedstatechanged", listener);
});
if (params.location) {
const [file, lineStr] = params.location.split(":");
let location;
if (lineStr) {
const line = Number(lineStr);
if (isNaN(line))
throw new Error(`Invalid location "${params.location}", expected format is <file>:<line>, e.g. "example.spec.ts:42"`);
location = { file, line };
} else {
location = { file: params.location };
}
await browserContext.debugger.runTo(location);
} else if (params.step) {
await browserContext.debugger.next();
} else {
await browserContext.debugger.resume();
}
await pausedPromise;
}
});
var devtools_default = [resume];
+59
View File
@@ -0,0 +1,59 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var dialogs_exports = {};
__export(dialogs_exports, {
default: () => dialogs_default,
handleDialog: () => handleDialog
});
module.exports = __toCommonJS(dialogs_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const handleDialog = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_handle_dialog",
title: "Handle a dialog",
description: "Handle a dialog",
inputSchema: import_zodBundle.z.object({
accept: import_zodBundle.z.boolean().describe("Whether to accept the dialog."),
promptText: import_zodBundle.z.string().optional().describe("The text of the prompt in case of a prompt dialog.")
}),
type: "action"
},
handle: async (tab, params, response) => {
const dialogState = tab.modalStates().find((state) => state.type === "dialog");
if (!dialogState)
throw new Error("No dialog visible");
tab.clearModalState(dialogState);
await tab.waitForCompletion(async () => {
if (params.accept)
await dialogState.dialog.accept(params.promptText);
else
await dialogState.dialog.dismiss();
});
},
clearsModalState: "dialog"
});
var dialogs_default = [
handleDialog
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
handleDialog
});
+64
View File
@@ -0,0 +1,64 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var evaluate_exports = {};
__export(evaluate_exports, {
default: () => evaluate_default
});
module.exports = __toCommonJS(evaluate_exports);
var import_zodBundle = require("../../zodBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_tool = require("./tool");
const evaluateSchema = import_zodBundle.z.object({
function: import_zodBundle.z.string().describe("() => { /* code */ } or (element) => { /* code */ } when element is provided"),
element: import_zodBundle.z.string().optional().describe("Human-readable element description used to obtain permission to interact with the element"),
ref: import_zodBundle.z.string().optional().describe("Exact target element reference from the page snapshot"),
selector: import_zodBundle.z.string().optional().describe('CSS or role selector for the target element, when "ref" is not available.'),
filename: import_zodBundle.z.string().optional().describe("Filename to save the result to. If not provided, result is returned as text.")
});
const evaluate = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_evaluate",
title: "Evaluate JavaScript",
description: "Evaluate JavaScript expression on page or element",
inputSchema: evaluateSchema,
type: "action"
},
handle: async (tab, params, response) => {
let locator;
if (!params.function.includes("=>"))
params.function = `() => (${params.function})`;
if (params.ref) {
locator = await tab.refLocator({ ref: params.ref, selector: params.selector, element: params.element || "element" });
response.addCode(`await page.${locator.resolved}.evaluate(${(0, import_stringUtils.escapeWithQuotes)(params.function)});`);
} else {
response.addCode(`await page.evaluate(${(0, import_stringUtils.escapeWithQuotes)(params.function)});`);
}
await tab.waitForCompletion(async () => {
const func = new Function();
func.toString = () => params.function;
const result = locator?.locator ? await locator?.locator.evaluate(func) : await tab.page.evaluate(func);
const text = JSON.stringify(result, null, 2) || "undefined";
await response.addResult("Evaluation result", text, { prefix: "result", ext: "json", suggestedFilename: params.filename });
});
}
});
var evaluate_default = [
evaluate
];
+60
View File
@@ -0,0 +1,60 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var files_exports = {};
__export(files_exports, {
default: () => files_default,
uploadFile: () => uploadFile
});
module.exports = __toCommonJS(files_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const uploadFile = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_file_upload",
title: "Upload files",
description: "Upload one or multiple files",
inputSchema: import_zodBundle.z.object({
paths: import_zodBundle.z.array(import_zodBundle.z.string()).optional().describe("The absolute paths to the files to upload. Can be single file or multiple files. If omitted, file chooser is cancelled.")
}),
type: "action"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
const modalState = tab.modalStates().find((state) => state.type === "fileChooser");
if (!modalState)
throw new Error("No file chooser visible");
if (params.paths)
await Promise.all(params.paths.map((filePath) => response.resolveClientFilename(filePath)));
response.addCode(`await fileChooser.setFiles(${JSON.stringify(params.paths)})`);
tab.clearModalState(modalState);
await tab.waitForCompletion(async () => {
if (params.paths)
await modalState.fileChooser.setFiles(params.paths);
});
},
clearsModalState: "fileChooser"
});
var files_default = [
uploadFile
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
uploadFile
});
+64
View File
@@ -0,0 +1,64 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var form_exports = {};
__export(form_exports, {
default: () => form_default
});
module.exports = __toCommonJS(form_exports);
var import_zodBundle = require("../../zodBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_tool = require("./tool");
const fillForm = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_fill_form",
title: "Fill form",
description: "Fill multiple form fields",
inputSchema: import_zodBundle.z.object({
fields: import_zodBundle.z.array(import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Human-readable field name"),
type: import_zodBundle.z.enum(["textbox", "checkbox", "radio", "combobox", "slider"]).describe("Type of the field"),
ref: import_zodBundle.z.string().describe("Exact target field reference from the page snapshot"),
selector: import_zodBundle.z.string().optional().describe('CSS or role selector for the field element, when "ref" is not available. Either "selector" or "ref" is required.'),
value: import_zodBundle.z.string().describe("Value to fill in the field. If the field is a checkbox, the value should be `true` or `false`. If the field is a combobox, the value should be the text of the option.")
})).describe("Fields to fill in")
}),
type: "input"
},
handle: async (tab, params, response) => {
for (const field of params.fields) {
const { locator, resolved } = await tab.refLocator({ element: field.name, ref: field.ref, selector: field.selector });
const locatorSource = `await page.${resolved}`;
if (field.type === "textbox" || field.type === "slider") {
const secret = tab.context.lookupSecret(field.value);
await locator.fill(secret.value, tab.actionTimeoutOptions);
response.addCode(`${locatorSource}.fill(${secret.code});`);
} else if (field.type === "checkbox" || field.type === "radio") {
await locator.setChecked(field.value === "true", tab.actionTimeoutOptions);
response.addCode(`${locatorSource}.setChecked(${field.value});`);
} else if (field.type === "combobox") {
await locator.selectOption({ label: field.value }, tab.actionTimeoutOptions);
response.addCode(`${locatorSource}.selectOption(${(0, import_stringUtils.escapeWithQuotes)(field.value)});`);
}
}
}
});
var form_default = [
fillForm
];
+155
View File
@@ -0,0 +1,155 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var keyboard_exports = {};
__export(keyboard_exports, {
default: () => keyboard_default
});
module.exports = __toCommonJS(keyboard_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
var import_snapshot = require("./snapshot");
const press = (0, import_tool.defineTabTool)({
capability: "core-input",
schema: {
name: "browser_press_key",
title: "Press a key",
description: "Press a key on the keyboard",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.addCode(`// Press ${params.key}`);
response.addCode(`await page.keyboard.press('${params.key}');`);
if (params.key === "Enter") {
response.setIncludeSnapshot();
await tab.waitForCompletion(async () => {
await tab.page.keyboard.press("Enter");
});
} else {
await tab.page.keyboard.press(params.key);
}
}
});
const pressSequentially = (0, import_tool.defineTabTool)({
capability: "core-input",
skillOnly: true,
schema: {
name: "browser_press_sequentially",
title: "Type text key by key",
description: "Type text key by key on the keyboard",
inputSchema: import_zodBundle.z.object({
text: import_zodBundle.z.string().describe("Text to type"),
submit: import_zodBundle.z.boolean().optional().describe("Whether to submit entered text (press Enter after)")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.addCode(`// Press ${params.text}`);
response.addCode(`await page.keyboard.type('${params.text}');`);
await tab.page.keyboard.type(params.text);
if (params.submit) {
response.addCode(`await page.keyboard.press('Enter');`);
response.setIncludeSnapshot();
await tab.waitForCompletion(async () => {
await tab.page.keyboard.press("Enter");
});
}
}
});
const typeSchema = import_snapshot.elementSchema.extend({
text: import_zodBundle.z.string().describe("Text to type into the element"),
submit: import_zodBundle.z.boolean().optional().describe("Whether to submit entered text (press Enter after)"),
slowly: import_zodBundle.z.boolean().optional().describe("Whether to type one character at a time. Useful for triggering key handlers in the page. By default entire text is filled in at once.")
});
const type = (0, import_tool.defineTabTool)({
capability: "core-input",
schema: {
name: "browser_type",
title: "Type text",
description: "Type text into editable element",
inputSchema: typeSchema,
type: "input"
},
handle: async (tab, params, response) => {
const { locator, resolved } = await tab.refLocator(params);
const secret = tab.context.lookupSecret(params.text);
const action = async () => {
if (params.slowly) {
response.setIncludeSnapshot();
response.addCode(`await page.${resolved}.pressSequentially(${secret.code});`);
await locator.pressSequentially(secret.value, tab.actionTimeoutOptions);
} else {
response.addCode(`await page.${resolved}.fill(${secret.code});`);
await locator.fill(secret.value, tab.actionTimeoutOptions);
}
if (params.submit) {
response.setIncludeSnapshot();
response.addCode(`await page.${resolved}.press('Enter');`);
await locator.press("Enter", tab.actionTimeoutOptions);
}
};
if (params.submit || params.slowly)
await tab.waitForCompletion(action);
else
await action();
}
});
const keydown = (0, import_tool.defineTabTool)({
capability: "core-input",
skillOnly: true,
schema: {
name: "browser_keydown",
title: "Press a key down",
description: "Press a key down on the keyboard",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.addCode(`await page.keyboard.down('${params.key}');`);
await tab.page.keyboard.down(params.key);
}
});
const keyup = (0, import_tool.defineTabTool)({
capability: "core-input",
skillOnly: true,
schema: {
name: "browser_keyup",
title: "Press a key up",
description: "Press a key up on the keyboard",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.addCode(`await page.keyboard.up('${params.key}');`);
await tab.page.keyboard.up(params.key);
}
});
var keyboard_default = [
press,
type,
pressSequentially,
keydown,
keyup
];
+95
View File
@@ -0,0 +1,95 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var logFile_exports = {};
__export(logFile_exports, {
LogFile: () => LogFile
});
module.exports = __toCommonJS(logFile_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_utilsBundle = require("../../utilsBundle");
class LogFile {
constructor(context, startTime, filePrefix, title) {
this._stopped = false;
this._line = 0;
this._entries = 0;
this._lastLine = 0;
this._lastEntries = 0;
this._writeChain = Promise.resolve();
this._context = context;
this._startTime = startTime;
this._filePrefix = filePrefix;
this._title = title;
}
appendLine(wallTime, text) {
this._writeChain = this._writeChain.then(() => this._write(wallTime, text)).catch((e) => (0, import_utilsBundle.debug)("pw:tools:error")(e));
}
stop() {
this._stopped = true;
}
async take(relativeTo) {
const logChunk = await this._take();
if (!logChunk)
return void 0;
const logFilePath = relativeTo ? import_path.default.relative(relativeTo, logChunk.file) : logChunk.file;
const lineRange = logChunk.fromLine === logChunk.toLine ? `#L${logChunk.fromLine}` : `#L${logChunk.fromLine}-L${logChunk.toLine}`;
return `${logFilePath}${lineRange}`;
}
async _take() {
await this._writeChain;
if (!this._file || this._entries === this._lastEntries)
return void 0;
const chunk = {
type: this._title.toLowerCase(),
file: this._file,
fromLine: this._lastLine + 1,
toLine: this._line,
entryCount: this._entries - this._lastEntries
};
this._lastLine = this._line;
this._lastEntries = this._entries;
return chunk;
}
async _write(wallTime, text) {
if (this._stopped)
return;
this._file ??= await this._context.outputFile({ prefix: this._filePrefix, ext: "log", date: new Date(this._startTime) }, { origin: "code" });
const relativeTime = Math.round(wallTime - this._startTime);
const logLine = `[${String(relativeTime).padStart(8, " ")}ms] ${text}
`;
await import_fs.default.promises.appendFile(this._file, logLine);
const lineCount = logLine.split("\n").length - 1;
this._line += lineCount;
this._entries++;
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LogFile
});
+168
View File
@@ -0,0 +1,168 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var mouse_exports = {};
__export(mouse_exports, {
default: () => mouse_default
});
module.exports = __toCommonJS(mouse_exports);
var import_zodBundle = require("../../zodBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_tool = require("./tool");
const mouseMove = (0, import_tool.defineTabTool)({
capability: "vision",
schema: {
name: "browser_mouse_move_xy",
title: "Move mouse",
description: "Move mouse to a given position",
inputSchema: import_zodBundle.z.object({
x: import_zodBundle.z.number().describe("X coordinate"),
y: import_zodBundle.z.number().describe("Y coordinate")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.addCode(`// Move mouse to (${params.x}, ${params.y})`);
response.addCode(`await page.mouse.move(${params.x}, ${params.y});`);
await tab.page.mouse.move(params.x, params.y);
}
});
const mouseDown = (0, import_tool.defineTabTool)({
capability: "vision",
schema: {
name: "browser_mouse_down",
title: "Press mouse down",
description: "Press mouse down",
inputSchema: import_zodBundle.z.object({
button: import_zodBundle.z.enum(["left", "right", "middle"]).optional().describe("Button to press, defaults to left")
}),
type: "input"
},
handle: async (tab, params, response) => {
const options = { button: params.button };
const optionsArg = (0, import_stringUtils.formatObjectOrVoid)(options);
response.addCode(`// Press mouse down`);
response.addCode(`await page.mouse.down(${optionsArg});`);
await tab.page.mouse.down(options);
}
});
const mouseUp = (0, import_tool.defineTabTool)({
capability: "vision",
schema: {
name: "browser_mouse_up",
title: "Press mouse up",
description: "Press mouse up",
inputSchema: import_zodBundle.z.object({
button: import_zodBundle.z.enum(["left", "right", "middle"]).optional().describe("Button to press, defaults to left")
}),
type: "input"
},
handle: async (tab, params, response) => {
const options = { button: params.button };
const optionsArg = (0, import_stringUtils.formatObjectOrVoid)(options);
response.addCode(`// Press mouse up`);
response.addCode(`await page.mouse.up(${optionsArg});`);
await tab.page.mouse.up(options);
}
});
const mouseWheel = (0, import_tool.defineTabTool)({
capability: "vision",
schema: {
name: "browser_mouse_wheel",
title: "Scroll mouse wheel",
description: "Scroll mouse wheel",
inputSchema: import_zodBundle.z.object({
deltaX: import_zodBundle.z.number().default(0).describe("X delta"),
deltaY: import_zodBundle.z.number().default(0).describe("Y delta")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.addCode(`// Scroll mouse wheel`);
response.addCode(`await page.mouse.wheel(${params.deltaX}, ${params.deltaY});`);
await tab.page.mouse.wheel(params.deltaX, params.deltaY);
}
});
const mouseClick = (0, import_tool.defineTabTool)({
capability: "vision",
schema: {
name: "browser_mouse_click_xy",
title: "Click",
description: "Click mouse button at a given position",
inputSchema: import_zodBundle.z.object({
x: import_zodBundle.z.number().describe("X coordinate"),
y: import_zodBundle.z.number().describe("Y coordinate"),
button: import_zodBundle.z.enum(["left", "right", "middle"]).optional().describe("Button to click, defaults to left"),
clickCount: import_zodBundle.z.number().optional().describe("Number of clicks, defaults to 1"),
delay: import_zodBundle.z.number().optional().describe("Time to wait between mouse down and mouse up in milliseconds, defaults to 0")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
const options = {
button: params.button,
clickCount: params.clickCount,
delay: params.delay
};
const formatted = (0, import_stringUtils.formatObjectOrVoid)(options);
const optionsArg = formatted ? `, ${formatted}` : "";
response.addCode(`// Click mouse at coordinates (${params.x}, ${params.y})`);
response.addCode(`await page.mouse.click(${params.x}, ${params.y}${optionsArg});`);
await tab.waitForCompletion(async () => {
await tab.page.mouse.click(params.x, params.y, options);
});
}
});
const mouseDrag = (0, import_tool.defineTabTool)({
capability: "vision",
schema: {
name: "browser_mouse_drag_xy",
title: "Drag mouse",
description: "Drag left mouse button to a given position",
inputSchema: import_zodBundle.z.object({
startX: import_zodBundle.z.number().describe("Start X coordinate"),
startY: import_zodBundle.z.number().describe("Start Y coordinate"),
endX: import_zodBundle.z.number().describe("End X coordinate"),
endY: import_zodBundle.z.number().describe("End Y coordinate")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
response.addCode(`// Drag mouse from (${params.startX}, ${params.startY}) to (${params.endX}, ${params.endY})`);
response.addCode(`await page.mouse.move(${params.startX}, ${params.startY});`);
response.addCode(`await page.mouse.down();`);
response.addCode(`await page.mouse.move(${params.endX}, ${params.endY});`);
response.addCode(`await page.mouse.up();`);
await tab.waitForCompletion(async () => {
await tab.page.mouse.move(params.startX, params.startY);
await tab.page.mouse.down();
await tab.page.mouse.move(params.endX, params.endY);
await tab.page.mouse.up();
});
}
});
var mouse_default = [
mouseMove,
mouseClick,
mouseDrag,
mouseDown,
mouseUp,
mouseWheel
];
+106
View File
@@ -0,0 +1,106 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var navigate_exports = {};
__export(navigate_exports, {
default: () => navigate_default
});
module.exports = __toCommonJS(navigate_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const navigate = (0, import_tool.defineTool)({
capability: "core-navigation",
schema: {
name: "browser_navigate",
title: "Navigate to a URL",
description: "Navigate to a URL",
inputSchema: import_zodBundle.z.object({
url: import_zodBundle.z.string().describe("The URL to navigate to")
}),
type: "action"
},
handle: async (context, params, response) => {
const tab = await context.ensureTab();
let url = params.url;
try {
new URL(url);
} catch (e) {
if (url.startsWith("localhost"))
url = "http://" + url;
else
url = "https://" + url;
}
context.checkUrlAllowed(url);
await tab.navigate(url);
response.setIncludeSnapshot();
response.addCode(`await page.goto('${url}');`);
}
});
const goBack = (0, import_tool.defineTabTool)({
capability: "core-navigation",
schema: {
name: "browser_navigate_back",
title: "Go back",
description: "Go back to the previous page in the history",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.goBack(tab.navigationTimeoutOptions);
response.setIncludeSnapshot();
response.addCode(`await page.goBack();`);
}
});
const goForward = (0, import_tool.defineTabTool)({
capability: "core-navigation",
skillOnly: true,
schema: {
name: "browser_navigate_forward",
title: "Go forward",
description: "Go forward to the next page in the history",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.goForward(tab.navigationTimeoutOptions);
response.setIncludeSnapshot();
response.addCode(`await page.goForward();`);
}
});
const reload = (0, import_tool.defineTabTool)({
capability: "core-navigation",
skillOnly: true,
schema: {
name: "browser_reload",
title: "Reload the page",
description: "Reload the current page",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.reload(tab.navigationTimeoutOptions);
response.setIncludeSnapshot();
response.addCode(`await page.reload();`);
}
});
var navigate_default = [
navigate,
goBack,
goForward,
reload
];
+135
View File
@@ -0,0 +1,135 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var network_exports = {};
__export(network_exports, {
default: () => network_default,
isFetch: () => isFetch,
renderRequest: () => renderRequest
});
module.exports = __toCommonJS(network_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const requests = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_network_requests",
title: "List network requests",
description: "Returns all network requests since loading the page",
inputSchema: import_zodBundle.z.object({
static: import_zodBundle.z.boolean().default(false).describe("Whether to include successful static resources like images, fonts, scripts, etc. Defaults to false."),
requestBody: import_zodBundle.z.boolean().default(false).describe("Whether to include request body. Defaults to false."),
requestHeaders: import_zodBundle.z.boolean().default(false).describe("Whether to include request headers. Defaults to false."),
filter: import_zodBundle.z.string().optional().describe('Only return requests whose URL matches this regexp (e.g. "/api/.*user").'),
filename: import_zodBundle.z.string().optional().describe("Filename to save the network requests to. If not provided, requests are returned as text.")
}),
type: "readOnly"
},
handle: async (tab, params, response) => {
const requests2 = await tab.requests();
const filter = params.filter ? new RegExp(params.filter) : void 0;
const text = [];
for (const request of requests2) {
if (!params.static && !isFetch(request) && isSuccessfulResponse(request))
continue;
if (filter) {
filter.lastIndex = 0;
if (!filter.test(request.url()))
continue;
}
text.push(await renderRequest(request, params.requestBody, params.requestHeaders));
}
await response.addResult("Network", text.join("\n"), { prefix: "network", ext: "log", suggestedFilename: params.filename });
}
});
const networkClear = (0, import_tool.defineTabTool)({
capability: "core",
skillOnly: true,
schema: {
name: "browser_network_clear",
title: "Clear network requests",
description: "Clear all network requests",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (tab, params, response) => {
await tab.clearRequests();
}
});
function isSuccessfulResponse(request) {
if (request.failure())
return false;
const response = request.existingResponse();
return !!response && response.status() < 400;
}
function isFetch(request) {
return ["fetch", "xhr"].includes(request.resourceType());
}
async function renderRequest(request, includeBody = false, includeHeaders = false) {
const response = request.existingResponse();
const result = [];
result.push(`[${request.method().toUpperCase()}] ${request.url()}`);
if (response)
result.push(` => [${response.status()}] ${response.statusText()}`);
else if (request.failure())
result.push(` => [FAILED] ${request.failure()?.errorText ?? "Unknown error"}`);
if (includeHeaders) {
const headers = request.headers();
const headerLines = Object.entries(headers).map(([k, v]) => ` ${k}: ${v}`).join("\n");
if (headerLines)
result.push(`
Request headers:
${headerLines}`);
}
if (includeBody) {
const postData = request.postData();
if (postData)
result.push(`
Request body: ${postData}`);
}
return result.join("");
}
const networkStateSet = (0, import_tool.defineTool)({
capability: "network",
schema: {
name: "browser_network_state_set",
title: "Set network state",
description: "Sets the browser network state to online or offline. When offline, all network requests will fail.",
inputSchema: import_zodBundle.z.object({
state: import_zodBundle.z.enum(["online", "offline"]).describe('Set to "offline" to simulate offline mode, "online" to restore network connectivity')
}),
type: "action"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const offline = params.state === "offline";
await browserContext.setOffline(offline);
response.addTextResult(`Network is now ${params.state}`);
response.addCode(`await page.context().setOffline(${offline});`);
}
});
var network_default = [
requests,
networkClear,
networkStateSet
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
isFetch,
renderRequest
});
+48
View File
@@ -0,0 +1,48 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var pdf_exports = {};
__export(pdf_exports, {
default: () => pdf_default
});
module.exports = __toCommonJS(pdf_exports);
var import_zodBundle = require("../../zodBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_tool = require("./tool");
const pdfSchema = import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified. Prefer relative file names to stay within the output directory.")
});
const pdf = (0, import_tool.defineTabTool)({
capability: "pdf",
schema: {
name: "browser_pdf_save",
title: "Save as PDF",
description: "Save page as PDF",
inputSchema: pdfSchema,
type: "readOnly"
},
handle: async (tab, params, response) => {
const data = await tab.page.pdf();
const result = await response.resolveClientFile({ prefix: "page", ext: "pdf", suggestedFilename: params.filename }, "Page as pdf");
await response.addFileResult(result, data);
response.addCode(`await page.pdf(${(0, import_stringUtils.formatObject)({ path: result.relativeName })});`);
}
});
var pdf_default = [
pdf
];
+305
View File
@@ -0,0 +1,305 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var response_exports = {};
__export(response_exports, {
Response: () => Response,
parseResponse: () => parseResponse,
renderTabMarkdown: () => renderTabMarkdown,
renderTabsMarkdown: () => renderTabsMarkdown,
requestDebug: () => requestDebug
});
module.exports = __toCommonJS(response_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_utilsBundle = require("../../utilsBundle");
var import_tab = require("./tab");
var import_screenshot = require("./screenshot");
const requestDebug = (0, import_utilsBundle.debug)("pw:mcp:request");
class Response {
constructor(context, toolName, toolArgs, relativeTo) {
this._results = [];
this._errors = [];
this._code = [];
this._includeSnapshot = "none";
this._isClose = false;
this._imageResults = [];
this._context = context;
this.toolName = toolName;
this.toolArgs = toolArgs;
this._clientWorkspace = relativeTo ?? context.options.cwd;
}
_computRelativeTo(fileName) {
return import_path.default.relative(this._clientWorkspace, fileName);
}
async resolveClientFile(template, title) {
let fileName;
if (template.suggestedFilename)
fileName = await this.resolveClientFilename(template.suggestedFilename);
else
fileName = await this._context.outputFile(template, { origin: "llm" });
const relativeName = this._computRelativeTo(fileName);
const printableLink = `- [${title}](${relativeName})`;
return { fileName, relativeName, printableLink };
}
async resolveClientFilename(filename) {
return await this._context.workspaceFile(filename, this._clientWorkspace);
}
addTextResult(text) {
this._results.push(text);
}
async addResult(title, data, file) {
if (file.suggestedFilename || typeof data !== "string") {
const resolvedFile = await this.resolveClientFile(file, title);
await this.addFileResult(resolvedFile, data);
} else {
this.addTextResult(data);
}
}
async _writeFile(resolvedFile, data) {
if (typeof data === "string")
await import_fs.default.promises.writeFile(resolvedFile.fileName, this._redactSecrets(data), "utf-8");
else if (data)
await import_fs.default.promises.writeFile(resolvedFile.fileName, data);
}
async addFileResult(resolvedFile, data) {
await this._writeFile(resolvedFile, data);
this.addTextResult(resolvedFile.printableLink);
}
addFileLink(title, fileName) {
const relativeName = this._computRelativeTo(fileName);
this.addTextResult(`- [${title}](${relativeName})`);
}
async registerImageResult(data, imageType) {
this._imageResults.push({ data, imageType });
}
setClose() {
this._isClose = true;
}
addError(error) {
this._errors.push(error);
}
addCode(code) {
this._code.push(code);
}
setIncludeSnapshot() {
this._includeSnapshot = this._context.config.snapshot?.mode ?? "full";
}
setIncludeFullSnapshot(includeSnapshotFileName, selector, depth) {
this._includeSnapshot = "explicit";
this._includeSnapshotFileName = includeSnapshotFileName;
this._includeSnapshotDepth = depth;
this._includeSnapshotSelector = selector;
}
_redactSecrets(text) {
for (const [secretName, secretValue] of Object.entries(this._context.config.secrets ?? {})) {
if (!secretValue)
continue;
text = text.replaceAll(secretValue, `<secret>${secretName}</secret>`);
}
return text;
}
async serialize() {
const sections = await this._build();
const text = [];
for (const section of sections) {
if (!section.content.length)
continue;
text.push(`### ${section.title}`);
if (section.codeframe)
text.push(`\`\`\`${section.codeframe}`);
text.push(...section.content);
if (section.codeframe)
text.push("```");
}
const content = [
{
type: "text",
text: sanitizeUnicode(this._redactSecrets(text.join("\n")))
}
];
if (this._context.config.imageResponses !== "omit") {
for (const imageResult of this._imageResults) {
const scaledData = (0, import_screenshot.scaleImageToFitMessage)(imageResult.data, imageResult.imageType);
content.push({ type: "image", data: scaledData.toString("base64"), mimeType: imageResult.imageType === "png" ? "image/png" : "image/jpeg" });
}
}
return {
content,
...this._isClose ? { isClose: true } : {},
...sections.some((section) => section.isError) ? { isError: true } : {}
};
}
async _build() {
const sections = [];
const addSection = (title, content, codeframe) => {
const section = { title, content, isError: title === "Error", codeframe };
sections.push(section);
return content;
};
if (this._errors.length)
addSection("Error", this._errors);
if (this._results.length)
addSection("Result", this._results);
if (this._context.config.codegen !== "none" && this._code.length)
addSection("Ran Playwright code", this._code, "js");
const tabSnapshot = this._context.currentTab() ? await this._context.currentTabOrDie().captureSnapshot(this._includeSnapshotSelector, this._includeSnapshotDepth, this._clientWorkspace) : void 0;
const tabHeaders = await Promise.all(this._context.tabs().map((tab) => tab.headerSnapshot()));
if (this._includeSnapshot !== "none" || tabHeaders.some((header) => header.changed)) {
if (tabHeaders.length !== 1)
addSection("Open tabs", renderTabsMarkdown(tabHeaders));
addSection("Page", renderTabMarkdown(tabHeaders.find((h) => h.current) ?? tabHeaders[0]));
}
if (this._context.tabs().length === 0)
this._isClose = true;
if (tabSnapshot?.modalStates.length)
addSection("Modal state", (0, import_tab.renderModalStates)(this._context.config, tabSnapshot.modalStates));
if (tabSnapshot && this._includeSnapshot !== "none") {
if (this._includeSnapshot !== "explicit" || this._includeSnapshotFileName) {
const suggestedFilename = this._includeSnapshotFileName === "<auto>" ? void 0 : this._includeSnapshotFileName;
const resolvedFile = await this.resolveClientFile({ prefix: "page", ext: "yml", suggestedFilename }, "Snapshot");
await this._writeFile(resolvedFile, tabSnapshot.ariaSnapshot);
addSection("Snapshot", [resolvedFile.printableLink]);
} else {
addSection("Snapshot", [tabSnapshot.ariaSnapshot], "yaml");
}
}
const text = [];
if (tabSnapshot?.consoleLink)
text.push(`- New console entries: ${tabSnapshot.consoleLink}`);
if (tabSnapshot?.events.filter((event) => event.type !== "request").length) {
for (const event of tabSnapshot.events) {
if (event.type === "download-start")
text.push(`- Downloading file ${event.download.download.suggestedFilename()} ...`);
else if (event.type === "download-finish")
text.push(`- Downloaded file ${event.download.download.suggestedFilename()} to "${this._computRelativeTo(event.download.outputFile)}"`);
}
}
if (text.length)
addSection("Events", text);
const pausedDetails = this._context.debugger().pausedDetails();
if (pausedDetails) {
addSection("Paused", [
`- ${pausedDetails.title} at ${this._computRelativeTo(pausedDetails.location.file)}${pausedDetails.location.line ? ":" + pausedDetails.location.line : ""}`,
"- Use any tools to explore and interact, resume by calling resume/step-over/pause-at"
]);
}
return sections;
}
}
function renderTabMarkdown(tab) {
const lines = [`- Page URL: ${tab.url}`];
if (tab.title)
lines.push(`- Page Title: ${tab.title}`);
if (tab.console.errors || tab.console.warnings)
lines.push(`- Console: ${tab.console.errors} errors, ${tab.console.warnings} warnings`);
return lines;
}
function renderTabsMarkdown(tabs) {
if (!tabs.length)
return ["No open tabs. Navigate to a URL to create one."];
const lines = [];
for (let i = 0; i < tabs.length; i++) {
const tab = tabs[i];
const current = tab.current ? " (current)" : "";
lines.push(`- ${i}:${current} [${tab.title}](${tab.url})`);
}
return lines;
}
function sanitizeUnicode(text) {
return text.toWellFormed?.() ?? text;
}
function parseSections(text) {
const sections = /* @__PURE__ */ new Map();
const sectionHeaders = text.split(/^### /m).slice(1);
for (const section of sectionHeaders) {
const firstNewlineIndex = section.indexOf("\n");
if (firstNewlineIndex === -1)
continue;
const sectionName = section.substring(0, firstNewlineIndex);
const sectionContent = section.substring(firstNewlineIndex + 1).trim();
sections.set(sectionName, sectionContent);
}
return sections;
}
function parseResponse(response, cwd) {
if (response.content?.[0].type !== "text")
return void 0;
const text = response.content[0].text;
const sections = parseSections(text);
const error = sections.get("Error");
const result = sections.get("Result");
const code = sections.get("Ran Playwright code");
const tabs = sections.get("Open tabs");
const page = sections.get("Page");
const snapshotSection = sections.get("Snapshot");
const events = sections.get("Events");
const modalState = sections.get("Modal state");
const paused = sections.get("Paused");
const codeNoFrame = code?.replace(/^```js\n/, "").replace(/\n```$/, "");
const isError = response.isError;
const attachments = response.content.length > 1 ? response.content.slice(1) : void 0;
let snapshot;
let inlineSnapshot;
if (snapshotSection) {
const match = snapshotSection.match(/\[Snapshot\]\(([^)]+)\)/);
if (match) {
if (cwd) {
try {
snapshot = import_fs.default.readFileSync(import_path.default.resolve(cwd, match[1]), "utf-8");
} catch {
}
}
} else {
inlineSnapshot = snapshotSection.replace(/^```yaml\n?/, "").replace(/\n?```$/, "");
}
}
return {
result,
error,
code: codeNoFrame,
tabs,
page,
snapshot,
inlineSnapshot,
events,
modalState,
paused,
isError,
attachments,
text
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Response,
parseResponse,
renderTabMarkdown,
renderTabsMarkdown,
requestDebug
});
+140
View File
@@ -0,0 +1,140 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var route_exports = {};
__export(route_exports, {
default: () => route_default
});
module.exports = __toCommonJS(route_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const route = (0, import_tool.defineTool)({
capability: "network",
schema: {
name: "browser_route",
title: "Mock network requests",
description: "Set up a route to mock network requests matching a URL pattern",
inputSchema: import_zodBundle.z.object({
pattern: import_zodBundle.z.string().describe('URL pattern to match (e.g., "**/api/users", "**/*.{png,jpg}")'),
status: import_zodBundle.z.number().optional().describe("HTTP status code to return (default: 200)"),
body: import_zodBundle.z.string().optional().describe("Response body (text or JSON string)"),
contentType: import_zodBundle.z.string().optional().describe('Content-Type header (e.g., "application/json", "text/html")'),
headers: import_zodBundle.z.array(import_zodBundle.z.string()).optional().describe('Headers to add in "Name: Value" format'),
removeHeaders: import_zodBundle.z.string().optional().describe("Comma-separated list of header names to remove from request")
}),
type: "action"
},
handle: async (context, params, response) => {
const addHeaders = params.headers ? Object.fromEntries(params.headers.map((h) => {
const colonIndex = h.indexOf(":");
return [h.substring(0, colonIndex).trim(), h.substring(colonIndex + 1).trim()];
})) : void 0;
const removeHeaders = params.removeHeaders ? params.removeHeaders.split(",").map((h) => h.trim()) : void 0;
const handler = async (route2) => {
if (params.body !== void 0 || params.status !== void 0) {
await route2.fulfill({
status: params.status ?? 200,
contentType: params.contentType,
body: params.body
});
return;
}
const headers = { ...route2.request().headers() };
if (addHeaders) {
for (const [key, value] of Object.entries(addHeaders))
headers[key] = value;
}
if (removeHeaders) {
for (const header of removeHeaders)
delete headers[header.toLowerCase()];
}
await route2.continue({ headers });
};
const entry = {
pattern: params.pattern,
status: params.status,
body: params.body,
contentType: params.contentType,
addHeaders,
removeHeaders,
handler
};
await context.addRoute(entry);
response.addTextResult(`Route added for pattern: ${params.pattern}`);
response.addCode(`await page.context().route('${params.pattern}', async route => { /* route handler */ });`);
}
});
const routeList = (0, import_tool.defineTool)({
capability: "network",
schema: {
name: "browser_route_list",
title: "List network routes",
description: "List all active network routes",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (context, params, response) => {
const routes = context.routes();
if (routes.length === 0) {
response.addTextResult("No active routes");
return;
}
const lines = [];
for (let i = 0; i < routes.length; i++) {
const route2 = routes[i];
const details = [];
if (route2.status !== void 0)
details.push(`status=${route2.status}`);
if (route2.body !== void 0)
details.push(`body=${route2.body.length > 50 ? route2.body.substring(0, 50) + "..." : route2.body}`);
if (route2.contentType)
details.push(`contentType=${route2.contentType}`);
if (route2.addHeaders)
details.push(`addHeaders=${JSON.stringify(route2.addHeaders)}`);
if (route2.removeHeaders)
details.push(`removeHeaders=${route2.removeHeaders.join(",")}`);
const detailsStr = details.length ? ` (${details.join(", ")})` : "";
lines.push(`${i + 1}. ${route2.pattern}${detailsStr}`);
}
response.addTextResult(lines.join("\n"));
}
});
const unroute = (0, import_tool.defineTool)({
capability: "network",
schema: {
name: "browser_unroute",
title: "Remove network routes",
description: "Remove network routes matching a pattern (or all routes if no pattern specified)",
inputSchema: import_zodBundle.z.object({
pattern: import_zodBundle.z.string().optional().describe("URL pattern to unroute (omit to remove all routes)")
}),
type: "action"
},
handle: async (context, params, response) => {
const removed = await context.removeRoute(params.pattern);
if (params.pattern)
response.addTextResult(`Removed ${removed} route(s) for pattern: ${params.pattern}`);
else
response.addTextResult(`Removed all ${removed} route(s)`);
}
});
var route_default = [
route,
routeList,
unroute
];
+77
View File
@@ -0,0 +1,77 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var runCode_exports = {};
__export(runCode_exports, {
default: () => runCode_default
});
module.exports = __toCommonJS(runCode_exports);
var import_fs = __toESM(require("fs"));
var import_vm = __toESM(require("vm"));
var import_manualPromise = require("../../utils/isomorphic/manualPromise");
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const codeSchema = import_zodBundle.z.object({
code: import_zodBundle.z.string().optional().describe(`A JavaScript function containing Playwright code to execute. It will be invoked with a single argument, page, which you can use for any page interaction. For example: \`async (page) => { await page.getByRole('button', { name: 'Submit' }).click(); return await page.title(); }\``),
filename: import_zodBundle.z.string().optional().describe("Load code from the specified file. If both code and filename are provided, code will be ignored.")
});
const runCode = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_run_code",
title: "Run Playwright code",
description: "Run Playwright code snippet",
inputSchema: codeSchema,
type: "action"
},
handle: async (tab, params, response) => {
let code = params.code;
if (params.filename) {
const resolvedPath = await response.resolveClientFilename(params.filename);
code = await import_fs.default.promises.readFile(resolvedPath, "utf-8");
}
response.addCode(`await (${code})(page);`);
const __end__ = new import_manualPromise.ManualPromise();
const context = {
page: tab.page,
__end__
};
import_vm.default.createContext(context);
await tab.waitForCompletion(async () => {
context.__fn__ = import_vm.default.runInContext("(" + code + ")", context);
const snippet = "(async () => {\n try {\n const result = await __fn__(page);\n __end__.resolve(JSON.stringify(result));\n } catch (e) {\n __end__.reject(e);\n }\n})()";
await import_vm.default.runInContext(snippet, context);
const result = await __end__;
if (typeof result === "string")
response.addTextResult(result);
});
}
});
var runCode_default = [
runCode
];
+88
View File
@@ -0,0 +1,88 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var screenshot_exports = {};
__export(screenshot_exports, {
default: () => screenshot_default,
scaleImageToFitMessage: () => scaleImageToFitMessage
});
module.exports = __toCommonJS(screenshot_exports);
var import_imageUtils = require("../../utils/isomorphic/imageUtils");
var import_utilsBundle = require("../../utilsBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const screenshotSchema = import_zodBundle.z.object({
type: import_zodBundle.z.enum(["png", "jpeg"]).default("png").describe("Image format for the screenshot. Default is png."),
filename: import_zodBundle.z.string().optional().describe("File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified. Prefer relative file names to stay within the output directory."),
element: import_zodBundle.z.string().optional().describe("Human-readable element description used to obtain permission to screenshot the element. If not provided, the screenshot will be taken of viewport. If element is provided, ref must be provided too."),
ref: import_zodBundle.z.string().optional().describe("Exact target element reference from the page snapshot. If not provided, the screenshot will be taken of viewport. If ref is provided, element must be provided too."),
selector: import_zodBundle.z.string().optional().describe('CSS or role selector for the target element, when "ref" is not available.'),
fullPage: import_zodBundle.z.boolean().optional().describe("When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport. Cannot be used with element screenshots.")
});
const screenshot = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_take_screenshot",
title: "Take a screenshot",
description: `Take a screenshot of the current page. You can't perform actions based on the screenshot, use browser_snapshot for actions.`,
inputSchema: screenshotSchema,
type: "readOnly"
},
handle: async (tab, params, response) => {
if (params.fullPage && params.ref)
throw new Error("fullPage cannot be used with element screenshots.");
const fileType = params.type || "png";
const options = {
type: fileType,
quality: fileType === "png" ? void 0 : 90,
scale: "css",
...tab.actionTimeoutOptions,
...params.fullPage !== void 0 && { fullPage: params.fullPage }
};
const screenshotTarget = params.ref ? params.element || "element" : params.fullPage ? "full page" : "viewport";
const ref = params.ref || params.selector ? await tab.refLocator({ element: params.element || "", ref: params.ref || "", selector: params.selector }) : null;
const data = ref ? await ref.locator.screenshot(options) : await tab.page.screenshot(options);
const resolvedFile = await response.resolveClientFile({ prefix: ref ? "element" : "page", ext: fileType, suggestedFilename: params.filename }, `Screenshot of ${screenshotTarget}`);
response.addCode(`// Screenshot ${screenshotTarget} and save it as ${resolvedFile.relativeName}`);
if (ref)
response.addCode(`await page.${ref.resolved}.screenshot(${(0, import_stringUtils.formatObject)({ ...options, path: resolvedFile.relativeName })});`);
else
response.addCode(`await page.screenshot(${(0, import_stringUtils.formatObject)({ ...options, path: resolvedFile.relativeName })});`);
await response.addFileResult(resolvedFile, data);
await response.registerImageResult(data, fileType);
}
});
function scaleImageToFitMessage(buffer, imageType) {
const image = imageType === "png" ? import_utilsBundle.PNG.sync.read(buffer) : import_utilsBundle.jpegjs.decode(buffer, { maxMemoryUsageInMB: 512 });
const pixels = image.width * image.height;
const shrink = Math.min(1568 / image.width, 1568 / image.height, Math.sqrt(1.15 * 1024 * 1024 / pixels));
if (shrink > 1)
return buffer;
const width = image.width * shrink | 0;
const height = image.height * shrink | 0;
const scaledImage = (0, import_imageUtils.scaleImageToSize)(image, { width, height });
return imageType === "png" ? import_utilsBundle.PNG.sync.write(scaledImage) : import_utilsBundle.jpegjs.encode(scaledImage, 80).data;
}
var screenshot_default = [
screenshot
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
scaleImageToFitMessage
});
+74
View File
@@ -0,0 +1,74 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var sessionLog_exports = {};
__export(sessionLog_exports, {
SessionLog: () => SessionLog
});
module.exports = __toCommonJS(sessionLog_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_context = require("./context");
var import_response = require("./response");
class SessionLog {
constructor(sessionFolder, cwd) {
this._sessionFileQueue = Promise.resolve();
this._folder = sessionFolder;
this._file = import_path.default.join(this._folder, "session.md");
this._cwd = cwd;
}
static async create(config, cwd) {
const sessionFolder = await (0, import_context.outputFile)({ config, cwd }, `session-${Date.now()}`, { origin: "code" });
await import_fs.default.promises.mkdir(sessionFolder, { recursive: true });
console.error(`Session: ${sessionFolder}`);
return new SessionLog(sessionFolder, cwd);
}
logResponse(toolName, toolArgs, responseObject) {
const parsed = { ...(0, import_response.parseResponse)(responseObject, this._cwd), text: void 0 };
const lines = [""];
lines.push(
`### Tool call: ${toolName}`,
`- Args`,
"```json",
JSON.stringify(toolArgs, null, 2),
"```"
);
if (parsed) {
lines.push(`- Result`);
lines.push("```json");
lines.push(JSON.stringify(parsed, null, 2));
lines.push("```");
}
lines.push("");
this._sessionFileQueue = this._sessionFileQueue.then(() => import_fs.default.promises.appendFile(this._file, lines.join("\n")));
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SessionLog
});
+208
View File
@@ -0,0 +1,208 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var snapshot_exports = {};
__export(snapshot_exports, {
default: () => snapshot_default,
elementSchema: () => elementSchema
});
module.exports = __toCommonJS(snapshot_exports);
var import_zodBundle = require("../../zodBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_tool = require("./tool");
const snapshot = (0, import_tool.defineTool)({
capability: "core",
schema: {
name: "browser_snapshot",
title: "Page snapshot",
description: "Capture accessibility snapshot of the current page, this is better than screenshot",
inputSchema: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("Save snapshot to markdown file instead of returning it in the response."),
selector: import_zodBundle.z.string().optional().describe("Element selector of the root element to capture a partial snapshot instead of the whole page"),
depth: import_zodBundle.z.number().optional().describe("Limit the depth of the snapshot tree")
}),
type: "readOnly"
},
handle: async (context, params, response) => {
await context.ensureTab();
response.setIncludeFullSnapshot(params.filename, params.selector, params.depth);
}
});
const elementSchema = import_zodBundle.z.object({
element: import_zodBundle.z.string().optional().describe("Human-readable element description used to obtain permission to interact with the element"),
ref: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot"),
selector: import_zodBundle.z.string().optional().describe('CSS or role selector for the target element, when "ref" is not available')
});
const clickSchema = elementSchema.extend({
doubleClick: import_zodBundle.z.boolean().optional().describe("Whether to perform a double click instead of a single click"),
button: import_zodBundle.z.enum(["left", "right", "middle"]).optional().describe("Button to click, defaults to left"),
modifiers: import_zodBundle.z.array(import_zodBundle.z.enum(["Alt", "Control", "ControlOrMeta", "Meta", "Shift"])).optional().describe("Modifier keys to press")
});
const click = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_click",
title: "Click",
description: "Perform click on a web page",
inputSchema: clickSchema,
type: "input"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
const { locator, resolved } = await tab.refLocator(params);
const options = {
button: params.button,
modifiers: params.modifiers,
...tab.actionTimeoutOptions
};
const optionsArg = (0, import_stringUtils.formatObjectOrVoid)(options);
if (params.doubleClick)
response.addCode(`await page.${resolved}.dblclick(${optionsArg});`);
else
response.addCode(`await page.${resolved}.click(${optionsArg});`);
await tab.waitForCompletion(async () => {
if (params.doubleClick)
await locator.dblclick(options);
else
await locator.click(options);
});
}
});
const drag = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_drag",
title: "Drag mouse",
description: "Perform drag and drop between two elements",
inputSchema: import_zodBundle.z.object({
startElement: import_zodBundle.z.string().describe("Human-readable source element description used to obtain the permission to interact with the element"),
startRef: import_zodBundle.z.string().describe("Exact source element reference from the page snapshot"),
startSelector: import_zodBundle.z.string().optional().describe("CSS or role selector for the source element, when ref is not available"),
endElement: import_zodBundle.z.string().describe("Human-readable target element description used to obtain the permission to interact with the element"),
endRef: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot"),
endSelector: import_zodBundle.z.string().optional().describe("CSS or role selector for the target element, when ref is not available")
}),
type: "input"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
const [start, end] = await tab.refLocators([
{ ref: params.startRef, selector: params.startSelector, element: params.startElement },
{ ref: params.endRef, selector: params.endSelector, element: params.endElement }
]);
await tab.waitForCompletion(async () => {
await start.locator.dragTo(end.locator, tab.actionTimeoutOptions);
});
response.addCode(`await page.${start.resolved}.dragTo(page.${end.resolved});`);
}
});
const hover = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_hover",
title: "Hover mouse",
description: "Hover over element on page",
inputSchema: elementSchema,
type: "input"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
const { locator, resolved } = await tab.refLocator(params);
response.addCode(`await page.${resolved}.hover();`);
await locator.hover(tab.actionTimeoutOptions);
}
});
const selectOptionSchema = elementSchema.extend({
values: import_zodBundle.z.array(import_zodBundle.z.string()).describe("Array of values to select in the dropdown. This can be a single value or multiple values.")
});
const selectOption = (0, import_tool.defineTabTool)({
capability: "core",
schema: {
name: "browser_select_option",
title: "Select option",
description: "Select an option in a dropdown",
inputSchema: selectOptionSchema,
type: "input"
},
handle: async (tab, params, response) => {
response.setIncludeSnapshot();
const { locator, resolved } = await tab.refLocator(params);
response.addCode(`await page.${resolved}.selectOption(${(0, import_stringUtils.formatObject)(params.values)});`);
await locator.selectOption(params.values, tab.actionTimeoutOptions);
}
});
const pickLocator = (0, import_tool.defineTabTool)({
capability: "testing",
schema: {
name: "browser_generate_locator",
title: "Create locator for element",
description: "Generate locator for the given element to use in tests",
inputSchema: elementSchema,
type: "readOnly"
},
handle: async (tab, params, response) => {
const { resolved } = await tab.refLocator(params);
response.addTextResult(resolved);
}
});
const check = (0, import_tool.defineTabTool)({
capability: "core-input",
skillOnly: true,
schema: {
name: "browser_check",
title: "Check",
description: "Check a checkbox or radio button",
inputSchema: elementSchema,
type: "input"
},
handle: async (tab, params, response) => {
const { locator, resolved } = await tab.refLocator(params);
response.addCode(`await page.${resolved}.check();`);
await locator.check(tab.actionTimeoutOptions);
}
});
const uncheck = (0, import_tool.defineTabTool)({
capability: "core-input",
skillOnly: true,
schema: {
name: "browser_uncheck",
title: "Uncheck",
description: "Uncheck a checkbox or radio button",
inputSchema: elementSchema,
type: "input"
},
handle: async (tab, params, response) => {
const { locator, resolved } = await tab.refLocator(params);
response.addCode(`await page.${resolved}.uncheck();`);
await locator.uncheck(tab.actionTimeoutOptions);
}
});
var snapshot_default = [
snapshot,
click,
drag,
hover,
selectOption,
pickLocator,
check,
uncheck
];
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
elementSchema
});
+68
View File
@@ -0,0 +1,68 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var storage_exports = {};
__export(storage_exports, {
default: () => storage_default
});
module.exports = __toCommonJS(storage_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const storageState = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_storage_state",
title: "Save storage state",
description: "Save storage state (cookies, local storage) to a file for later reuse",
inputSchema: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("File name to save the storage state to. Defaults to `storage-state-{timestamp}.json` if not specified.")
}),
type: "readOnly"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const state = await browserContext.storageState();
const serializedState = JSON.stringify(state, null, 2);
const resolvedFile = await response.resolveClientFile({ prefix: "storage-state", ext: "json", suggestedFilename: params.filename }, "Storage state");
response.addCode(`await page.context().storageState({ path: '${resolvedFile.relativeName}' });`);
await response.addFileResult(resolvedFile, serializedState);
}
});
const setStorageState = (0, import_tool.defineTool)({
capability: "storage",
schema: {
name: "browser_set_storage_state",
title: "Restore storage state",
description: "Restore storage state (cookies, local storage) from a file. This clears existing cookies and local storage before restoring.",
inputSchema: import_zodBundle.z.object({
filename: import_zodBundle.z.string().describe("Path to the storage state file to restore from")
}),
type: "action"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const resolvedFilename = await response.resolveClientFilename(params.filename);
await browserContext.setStorageState(resolvedFilename);
response.addTextResult(`Storage state restored from ${params.filename}`);
response.addCode(`await page.context().setStorageState('${params.filename}');`);
}
});
var storage_default = [
storageState,
setStorageState
];
+445
View File
@@ -0,0 +1,445 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tab_exports = {};
__export(tab_exports, {
Tab: () => Tab,
renderModalStates: () => renderModalStates,
shouldIncludeMessage: () => shouldIncludeMessage
});
module.exports = __toCommonJS(tab_exports);
var import_url = __toESM(require("url"));
var import_events = require("events");
var import_locatorGenerators = require("../../utils/isomorphic/locatorGenerators");
var import_locatorParser = require("../../utils/isomorphic/locatorParser");
var import_manualPromise = require("../../utils/isomorphic/manualPromise");
var import_utilsBundle = require("../../utilsBundle");
var import_eventsHelper = require("../../server/utils/eventsHelper");
var import_disposable = require("../../server/utils/disposable");
var import_utils = require("./utils");
var import_logFile = require("./logFile");
var import_dialogs = require("./dialogs");
var import_files = require("./files");
const TabEvents = {
modalState: "modalState"
};
class Tab extends import_events.EventEmitter {
constructor(context, page, onPageClose) {
super();
this._lastHeader = { title: "about:blank", url: "about:blank", current: false, console: { total: 0, warnings: 0, errors: 0 } };
this._downloads = [];
this._requests = [];
this._modalStates = [];
this._recentEventEntries = [];
this.context = context;
this.page = page;
this._onPageClose = onPageClose;
const p = page;
this._disposables = [
import_eventsHelper.eventsHelper.addEventListener(p, "console", (event) => this._handleConsoleMessage(messageToConsoleMessage(event))),
import_eventsHelper.eventsHelper.addEventListener(p, "pageerror", (error) => this._handleConsoleMessage(pageErrorToConsoleMessage(error))),
import_eventsHelper.eventsHelper.addEventListener(p, "request", (request) => this._handleRequest(request)),
import_eventsHelper.eventsHelper.addEventListener(p, "response", (response) => this._handleResponse(response)),
import_eventsHelper.eventsHelper.addEventListener(p, "requestfailed", (request) => this._handleRequestFailed(request)),
import_eventsHelper.eventsHelper.addEventListener(p, "close", () => this._onClose()),
import_eventsHelper.eventsHelper.addEventListener(p, "filechooser", (chooser) => {
this.setModalState({
type: "fileChooser",
description: "File chooser",
fileChooser: chooser,
clearedBy: { tool: import_files.uploadFile.schema.name, skill: "upload" }
});
}),
import_eventsHelper.eventsHelper.addEventListener(p, "dialog", (dialog) => this._dialogShown(dialog)),
import_eventsHelper.eventsHelper.addEventListener(p, "download", (download) => {
void this._downloadStarted(download);
})
];
page[tabSymbol] = this;
const wallTime = Date.now();
this._consoleLog = new import_logFile.LogFile(this.context, wallTime, "console", "Console");
this._initializedPromise = this._initialize();
this.actionTimeoutOptions = { timeout: context.config.timeouts?.action };
this.navigationTimeoutOptions = { timeout: context.config.timeouts?.navigation };
this.expectTimeoutOptions = { timeout: context.config.timeouts?.expect };
}
async dispose() {
await (0, import_disposable.disposeAll)(this._disposables);
this._consoleLog.stop();
}
static forPage(page) {
return page[tabSymbol];
}
static async collectConsoleMessages(page) {
const result = [];
const messages = await page.consoleMessages().catch(() => []);
for (const message of messages)
result.push(messageToConsoleMessage(message));
const errors = await page.pageErrors().catch(() => []);
for (const error of errors)
result.push(pageErrorToConsoleMessage(error));
return result;
}
async _initialize() {
for (const message of await Tab.collectConsoleMessages(this.page))
this._handleConsoleMessage(message);
const requests = await this.page.requests().catch(() => []);
for (const request of requests.filter((r) => r.existingResponse() || r.failure()))
this._requests.push(request);
for (const initPage of this.context.config.browser?.initPage || []) {
try {
const { default: func } = await import(import_url.default.pathToFileURL(initPage).href);
await func({ page: this.page });
} catch (e) {
(0, import_utilsBundle.debug)("pw:tools:error")(e);
}
}
}
modalStates() {
return this._modalStates;
}
setModalState(modalState) {
this._modalStates.push(modalState);
this.emit(TabEvents.modalState, modalState);
}
clearModalState(modalState) {
this._modalStates = this._modalStates.filter((state) => state !== modalState);
}
_dialogShown(dialog) {
this.setModalState({
type: "dialog",
description: `"${dialog.type()}" dialog with message "${dialog.message()}"`,
dialog,
clearedBy: { tool: import_dialogs.handleDialog.schema.name, skill: "dialog-accept or dialog-dismiss" }
});
}
async _downloadStarted(download) {
const outputFile = await this.context.outputFile({ suggestedFilename: sanitizeForFilePath(download.suggestedFilename()), prefix: "download", ext: "bin" }, { origin: "code" });
const entry = {
download,
finished: false,
outputFile
};
this._downloads.push(entry);
this._addLogEntry({ type: "download-start", wallTime: Date.now(), download: entry });
await download.saveAs(entry.outputFile);
entry.finished = true;
this._addLogEntry({ type: "download-finish", wallTime: Date.now(), download: entry });
}
_clearCollectedArtifacts() {
this._downloads.length = 0;
this._requests.length = 0;
this._recentEventEntries.length = 0;
this._resetLogs();
}
_resetLogs() {
const wallTime = Date.now();
this._consoleLog.stop();
this._consoleLog = new import_logFile.LogFile(this.context, wallTime, "console", "Console");
}
_handleRequest(request) {
this._requests.push(request);
const wallTime = request.timing().startTime || Date.now();
this._addLogEntry({ type: "request", wallTime, request });
}
_handleResponse(response) {
const timing = response.request().timing();
const wallTime = timing.responseStart + timing.startTime;
this._addLogEntry({ type: "request", wallTime, request: response.request() });
}
_handleRequestFailed(request) {
this._requests.push(request);
const timing = request.timing();
const wallTime = timing.responseEnd + timing.startTime;
this._addLogEntry({ type: "request", wallTime, request });
}
_handleConsoleMessage(message) {
const wallTime = message.timestamp;
this._addLogEntry({ type: "console", wallTime, message });
if (shouldIncludeMessage(this.context.config.console?.level, message.type))
this._consoleLog.appendLine(wallTime, message.toString());
}
_addLogEntry(entry) {
this._recentEventEntries.push(entry);
}
_onClose() {
this._clearCollectedArtifacts();
this._onPageClose(this);
}
async headerSnapshot() {
let title;
await this._raceAgainstModalStates(async () => {
title = await this.page.title();
});
const newHeader = {
title: title ?? "",
url: this.page.url(),
current: this.isCurrentTab(),
console: await this.consoleMessageCount()
};
if (!tabHeaderEquals(this._lastHeader, newHeader)) {
this._lastHeader = newHeader;
return { ...this._lastHeader, changed: true };
}
return { ...this._lastHeader, changed: false };
}
isCurrentTab() {
return this === this.context.currentTab();
}
async waitForLoadState(state, options) {
await this._initializedPromise;
await this.page.waitForLoadState(state, options).catch((e) => (0, import_utilsBundle.debug)("pw:tools:error")(e));
}
async navigate(url2) {
await this._initializedPromise;
this._clearCollectedArtifacts();
const { promise: downloadEvent, abort: abortDownloadEvent } = (0, import_utils.eventWaiter)(this.page, "download", 3e3);
try {
await this.page.goto(url2, { waitUntil: "domcontentloaded", ...this.navigationTimeoutOptions });
abortDownloadEvent();
} catch (_e) {
const e = _e;
const mightBeDownload = e.message.includes("net::ERR_ABORTED") || e.message.includes("Download is starting");
if (!mightBeDownload)
throw e;
const download = await downloadEvent;
if (!download)
throw e;
await new Promise((resolve) => setTimeout(resolve, 500));
return;
}
await this.waitForLoadState("load", { timeout: 5e3 });
}
async consoleMessageCount() {
await this._initializedPromise;
const messages = await this.page.consoleMessages({ filter: "since-navigation" });
const pageErrors = await this.page.pageErrors({ filter: "since-navigation" });
let errors = pageErrors.length;
let warnings = 0;
for (const message of messages) {
if (message.type() === "error")
errors++;
else if (message.type() === "warning")
warnings++;
}
return { total: messages.length + pageErrors.length, errors, warnings };
}
async consoleMessages(level, all) {
await this._initializedPromise;
const result = [];
const messages = await this.page.consoleMessages({ filter: all ? "all" : "since-navigation" });
for (const message of messages) {
const cm = messageToConsoleMessage(message);
if (shouldIncludeMessage(level, cm.type))
result.push(cm);
}
if (shouldIncludeMessage(level, "error")) {
const errors = await this.page.pageErrors({ filter: all ? "all" : "since-navigation" });
for (const error of errors)
result.push(pageErrorToConsoleMessage(error));
}
return result;
}
async clearConsoleMessages() {
await this._initializedPromise;
await Promise.all([
this.page.clearConsoleMessages(),
this.page.clearPageErrors()
]);
}
async requests() {
await this._initializedPromise;
return this._requests;
}
async clearRequests() {
await this._initializedPromise;
this._requests.length = 0;
}
async captureSnapshot(selector, depth, relativeTo) {
await this._initializedPromise;
let tabSnapshot;
const modalStates = await this._raceAgainstModalStates(async () => {
const ariaSnapshot = selector ? await this.page.locator(selector).ariaSnapshot({ mode: "ai", depth }) : await this.page.ariaSnapshot({ mode: "ai", depth });
tabSnapshot = {
ariaSnapshot,
modalStates: [],
events: []
};
});
if (tabSnapshot) {
tabSnapshot.consoleLink = await this._consoleLog.take(relativeTo);
tabSnapshot.events = this._recentEventEntries;
this._recentEventEntries = [];
}
return tabSnapshot ?? {
ariaSnapshot: "",
modalStates,
events: []
};
}
_javaScriptBlocked() {
return this._modalStates.some((state) => state.type === "dialog");
}
async _raceAgainstModalStates(action) {
if (this.modalStates().length)
return this.modalStates();
const promise = new import_manualPromise.ManualPromise();
const listener = (modalState) => promise.resolve([modalState]);
this.once(TabEvents.modalState, listener);
return await Promise.race([
action().then(() => {
this.off(TabEvents.modalState, listener);
return [];
}),
promise
]);
}
async waitForCompletion(callback) {
await this._initializedPromise;
await this._raceAgainstModalStates(() => (0, import_utils.waitForCompletion)(this, callback));
}
async refLocator(params) {
await this._initializedPromise;
return (await this.refLocators([params]))[0];
}
async refLocators(params) {
await this._initializedPromise;
return Promise.all(params.map(async (param) => {
if (param.selector) {
const selector = (0, import_locatorParser.locatorOrSelectorAsSelector)("javascript", param.selector, this.context.config.testIdAttribute || "data-testid");
const handle = await this.page.$(selector);
if (!handle)
throw new Error(`"${param.selector}" does not match any elements.`);
handle.dispose().catch(() => {
});
return { locator: this.page.locator(selector), resolved: (0, import_locatorGenerators.asLocator)("javascript", selector) };
} else {
try {
let locator = this.page.locator(`aria-ref=${param.ref}`);
if (param.element)
locator = locator.describe(param.element);
const resolved = await locator.normalize();
return { locator, resolved: resolved.toString() };
} catch (e) {
throw new Error(`Ref ${param.ref} not found in the current page snapshot. Try capturing new snapshot.`);
}
}
}));
}
async waitForTimeout(time) {
if (this._javaScriptBlocked()) {
await new Promise((f) => setTimeout(f, time));
return;
}
await this.page.evaluate(() => new Promise((f) => setTimeout(f, 1e3))).catch(() => {
});
}
}
function messageToConsoleMessage(message) {
return {
type: message.type(),
timestamp: message.timestamp(),
text: message.text(),
toString: () => `[${message.type().toUpperCase()}] ${message.text()} @ ${message.location().url}:${message.location().lineNumber}`
};
}
function pageErrorToConsoleMessage(errorOrValue) {
if (errorOrValue instanceof Error) {
return {
type: "error",
timestamp: Date.now(),
text: errorOrValue.message,
toString: () => errorOrValue.stack || errorOrValue.message
};
}
return {
type: "error",
timestamp: Date.now(),
text: String(errorOrValue),
toString: () => String(errorOrValue)
};
}
function renderModalStates(config, modalStates) {
const result = [];
if (modalStates.length === 0)
result.push("- There is no modal state present");
for (const state of modalStates)
result.push(`- [${state.description}]: can be handled by ${config.skillMode ? state.clearedBy.skill : state.clearedBy.tool}`);
return result;
}
const consoleMessageLevels = ["error", "warning", "info", "debug"];
function shouldIncludeMessage(thresholdLevel, type) {
const messageLevel = consoleLevelForMessageType(type);
return consoleMessageLevels.indexOf(messageLevel) <= consoleMessageLevels.indexOf(thresholdLevel || "info");
}
function consoleLevelForMessageType(type) {
switch (type) {
case "assert":
case "error":
return "error";
case "warning":
return "warning";
case "count":
case "dir":
case "dirxml":
case "info":
case "log":
case "table":
case "time":
case "timeEnd":
return "info";
case "clear":
case "debug":
case "endGroup":
case "profile":
case "profileEnd":
case "startGroup":
case "startGroupCollapsed":
case "trace":
return "debug";
default:
return "info";
}
}
const tabSymbol = Symbol("tabSymbol");
function sanitizeForFilePath(s) {
const sanitize = (s2) => s2.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, "-");
const separator = s.lastIndexOf(".");
if (separator === -1)
return sanitize(s);
return sanitize(s.substring(0, separator)) + "." + sanitize(s.substring(separator + 1));
}
function tabHeaderEquals(a, b) {
return a.title === b.title && a.url === b.url && a.current === b.current && a.console.errors === b.console.errors && a.console.warnings === b.console.warnings && a.console.total === b.console.total;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Tab,
renderModalStates,
shouldIncludeMessage
});
+67
View File
@@ -0,0 +1,67 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tabs_exports = {};
__export(tabs_exports, {
default: () => tabs_default
});
module.exports = __toCommonJS(tabs_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
var import_response = require("./response");
const browserTabs = (0, import_tool.defineTool)({
capability: "core-tabs",
schema: {
name: "browser_tabs",
title: "Manage tabs",
description: "List, create, close, or select a browser tab.",
inputSchema: import_zodBundle.z.object({
action: import_zodBundle.z.enum(["list", "new", "close", "select"]).describe("Operation to perform"),
index: import_zodBundle.z.number().optional().describe("Tab index, used for close/select. If omitted for close, current tab is closed.")
}),
type: "action"
},
handle: async (context, params, response) => {
switch (params.action) {
case "list": {
await context.ensureTab();
break;
}
case "new": {
await context.newTab();
break;
}
case "close": {
await context.closeTab(params.index);
break;
}
case "select": {
if (params.index === void 0)
throw new Error("Tab index is required");
await context.selectTab(params.index);
break;
}
}
const tabHeaders = await Promise.all(context.tabs().map((tab) => tab.headerSnapshot()));
const result = (0, import_response.renderTabsMarkdown)(tabHeaders);
response.addTextResult(result.join("\n"));
}
});
var tabs_default = [
browserTabs
];
+47
View File
@@ -0,0 +1,47 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tool_exports = {};
__export(tool_exports, {
defineTabTool: () => defineTabTool,
defineTool: () => defineTool
});
module.exports = __toCommonJS(tool_exports);
function defineTool(tool) {
return tool;
}
function defineTabTool(tool) {
return {
...tool,
handle: async (context, params, response) => {
const tab = await context.ensureTab();
const modalStates = tab.modalStates().map((state) => state.type);
if (tool.clearsModalState && !modalStates.includes(tool.clearsModalState))
response.addError(`Error: The tool "${tool.schema.name}" can only be used when there is related modal state present.`);
else if (!tool.clearsModalState && modalStates.length)
response.addError(`Error: Tool "${tool.schema.name}" does not handle the modal state.`);
else
return tool.handle(tab, params, response);
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
defineTabTool,
defineTool
});
+102
View File
@@ -0,0 +1,102 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tools_exports = {};
__export(tools_exports, {
browserTools: () => browserTools,
filteredTools: () => filteredTools
});
module.exports = __toCommonJS(tools_exports);
var import_zodBundle = require("../../zodBundle");
var import_common = __toESM(require("./common"));
var import_config = __toESM(require("./config"));
var import_console = __toESM(require("./console"));
var import_cookies = __toESM(require("./cookies"));
var import_devtools = __toESM(require("./devtools"));
var import_dialogs = __toESM(require("./dialogs"));
var import_evaluate = __toESM(require("./evaluate"));
var import_files = __toESM(require("./files"));
var import_form = __toESM(require("./form"));
var import_keyboard = __toESM(require("./keyboard"));
var import_mouse = __toESM(require("./mouse"));
var import_navigate = __toESM(require("./navigate"));
var import_network = __toESM(require("./network"));
var import_pdf = __toESM(require("./pdf"));
var import_route = __toESM(require("./route"));
var import_runCode = __toESM(require("./runCode"));
var import_snapshot = __toESM(require("./snapshot"));
var import_screenshot = __toESM(require("./screenshot"));
var import_storage = __toESM(require("./storage"));
var import_tabs = __toESM(require("./tabs"));
var import_tracing = __toESM(require("./tracing"));
var import_verify = __toESM(require("./verify"));
var import_video = __toESM(require("./video"));
var import_wait = __toESM(require("./wait"));
var import_webstorage = __toESM(require("./webstorage"));
const browserTools = [
...import_common.default,
...import_config.default,
...import_console.default,
...import_cookies.default,
...import_devtools.default,
...import_dialogs.default,
...import_evaluate.default,
...import_files.default,
...import_form.default,
...import_keyboard.default,
...import_mouse.default,
...import_navigate.default,
...import_network.default,
...import_pdf.default,
...import_route.default,
...import_runCode.default,
...import_screenshot.default,
...import_snapshot.default,
...import_storage.default,
...import_tabs.default,
...import_tracing.default,
...import_verify.default,
...import_video.default,
...import_wait.default,
...import_webstorage.default
];
function filteredTools(config2) {
return browserTools.filter((tool) => tool.capability.startsWith("core") || config2.capabilities?.includes(tool.capability)).filter((tool) => !tool.skillOnly).map((tool) => ({
...tool,
schema: {
...tool.schema,
// Note: we first ensure that "selector" property is present, so that we can omit() it without an error.
inputSchema: tool.schema.inputSchema.extend({ selector: import_zodBundle.z.string(), startSelector: import_zodBundle.z.string(), endSelector: import_zodBundle.z.string() }).omit({ selector: true, startSelector: true, endSelector: true })
}
}));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
browserTools,
filteredTools
});
+78
View File
@@ -0,0 +1,78 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tracing_exports = {};
__export(tracing_exports, {
default: () => tracing_default
});
module.exports = __toCommonJS(tracing_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const tracingStart = (0, import_tool.defineTool)({
capability: "devtools",
schema: {
name: "browser_start_tracing",
title: "Start tracing",
description: "Start trace recording",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
const tracesDir = await context.outputFile({ prefix: "", suggestedFilename: `traces`, ext: "" }, { origin: "code" });
const name = "trace-" + Date.now();
await browserContext.tracing.start({
name,
screenshots: true,
snapshots: true,
live: true
});
response.addTextResult(`Trace recording started`);
response.addFileLink("Action log", `${tracesDir}/${name}.trace`);
response.addFileLink("Network log", `${tracesDir}/${name}.network`);
response.addFileLink("Resources", `${tracesDir}/resources`);
browserContext.tracing[traceLegendSymbol] = { tracesDir, name };
}
});
const tracingStop = (0, import_tool.defineTool)({
capability: "devtools",
schema: {
name: "browser_stop_tracing",
title: "Stop tracing",
description: "Stop trace recording",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (context, params, response) => {
const browserContext = await context.ensureBrowserContext();
await browserContext.tracing.stop();
const traceLegend = browserContext.tracing[traceLegendSymbol];
if (!traceLegend)
throw new Error("Tracing is not started");
delete browserContext.tracing[traceLegendSymbol];
response.addTextResult(`Trace recording stopped.`);
response.addFileLink("Trace", `${traceLegend.tracesDir}/${traceLegend.name}.trace`);
response.addFileLink("Network log", `${traceLegend.tracesDir}/${traceLegend.name}.network`);
response.addFileLink("Resources", `${traceLegend.tracesDir}/resources`);
}
});
var tracing_default = [
tracingStart,
tracingStop
];
const traceLegendSymbol = Symbol("tracesDir");
+83
View File
@@ -0,0 +1,83 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var utils_exports = {};
__export(utils_exports, {
eventWaiter: () => eventWaiter,
waitForCompletion: () => waitForCompletion
});
module.exports = __toCommonJS(utils_exports);
async function waitForCompletion(tab, callback) {
const requests = [];
const requestListener = (request) => requests.push(request);
const disposeListeners = () => {
tab.page.off("request", requestListener);
};
tab.page.on("request", requestListener);
let result;
try {
result = await callback();
await tab.waitForTimeout(500);
} finally {
disposeListeners();
}
const requestedNavigation = requests.some((request) => request.isNavigationRequest());
if (requestedNavigation) {
await tab.page.mainFrame().waitForLoadState("load", { timeout: 1e4 }).catch(() => {
});
return result;
}
const promises = [];
for (const request of requests) {
if (["document", "stylesheet", "script", "xhr", "fetch"].includes(request.resourceType()))
promises.push(request.response().then((r) => r?.finished()).catch(() => {
}));
else
promises.push(request.response().catch(() => {
}));
}
const timeout = new Promise((resolve) => setTimeout(resolve, 5e3));
await Promise.race([Promise.all(promises), timeout]);
if (requests.length)
await tab.waitForTimeout(500);
return result;
}
function eventWaiter(page, event, timeout) {
const disposables = [];
const eventPromise = new Promise((resolve, reject) => {
page.on(event, resolve);
disposables.push(() => page.off(event, resolve));
});
let abort;
const abortPromise = new Promise((resolve, reject) => {
abort = () => resolve(void 0);
});
const timeoutPromise = new Promise((f) => {
const timeoutId = setTimeout(() => f(void 0), timeout);
disposables.push(() => clearTimeout(timeoutId));
});
return {
promise: Promise.race([eventPromise, abortPromise, timeoutPromise]).finally(() => disposables.forEach((dispose) => dispose())),
abort
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
eventWaiter,
waitForCompletion
});
+151
View File
@@ -0,0 +1,151 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var verify_exports = {};
__export(verify_exports, {
default: () => verify_default
});
module.exports = __toCommonJS(verify_exports);
var import_zodBundle = require("../../zodBundle");
var import_stringUtils = require("../../utils/isomorphic/stringUtils");
var import_tool = require("./tool");
const verifyElement = (0, import_tool.defineTabTool)({
capability: "testing",
schema: {
name: "browser_verify_element_visible",
title: "Verify element visible",
description: "Verify element is visible on the page",
inputSchema: import_zodBundle.z.object({
role: import_zodBundle.z.string().describe('ROLE of the element. Can be found in the snapshot like this: `- {ROLE} "Accessible Name":`'),
accessibleName: import_zodBundle.z.string().describe('ACCESSIBLE_NAME of the element. Can be found in the snapshot like this: `- role "{ACCESSIBLE_NAME}"`')
}),
type: "assertion"
},
handle: async (tab, params, response) => {
for (const frame of tab.page.frames()) {
const locator = frame.getByRole(params.role, { name: params.accessibleName });
if (await locator.count() > 0) {
const resolved = await locator.normalize();
response.addCode(`await expect(page.${resolved}).toBeVisible();`);
response.addTextResult("Done");
return;
}
}
response.addError(`Element with role "${params.role}" and accessible name "${params.accessibleName}" not found`);
}
});
const verifyText = (0, import_tool.defineTabTool)({
capability: "testing",
schema: {
name: "browser_verify_text_visible",
title: "Verify text visible",
description: `Verify text is visible on the page. Prefer ${verifyElement.schema.name} if possible.`,
inputSchema: import_zodBundle.z.object({
text: import_zodBundle.z.string().describe('TEXT to verify. Can be found in the snapshot like this: `- role "Accessible Name": {TEXT}` or like this: `- text: {TEXT}`')
}),
type: "assertion"
},
handle: async (tab, params, response) => {
for (const frame of tab.page.frames()) {
const locator = frame.getByText(params.text).filter({ visible: true });
if (await locator.count() > 0) {
const resolved = await locator.normalize();
response.addCode(`await expect(page.${resolved}).toBeVisible();`);
response.addTextResult("Done");
return;
}
}
response.addError("Text not found");
}
});
const verifyList = (0, import_tool.defineTabTool)({
capability: "testing",
schema: {
name: "browser_verify_list_visible",
title: "Verify list visible",
description: "Verify list is visible on the page",
inputSchema: import_zodBundle.z.object({
element: import_zodBundle.z.string().describe("Human-readable list description"),
ref: import_zodBundle.z.string().describe("Exact target element reference that points to the list"),
selector: import_zodBundle.z.string().optional().describe('CSS or role selector for the target list, when "ref" is not available.'),
items: import_zodBundle.z.array(import_zodBundle.z.string()).describe("Items to verify")
}),
type: "assertion"
},
handle: async (tab, params, response) => {
const { locator } = await tab.refLocator({ ref: params.ref, selector: params.selector, element: params.element });
const itemTexts = [];
for (const item of params.items) {
const itemLocator = locator.getByText(item);
if (await itemLocator.count() === 0) {
response.addError(`Item "${item}" not found`);
return;
}
itemTexts.push(await itemLocator.textContent(tab.expectTimeoutOptions));
}
const ariaSnapshot = `\`
- list:
${itemTexts.map((t) => ` - listitem: ${(0, import_stringUtils.escapeWithQuotes)(t, '"')}`).join("\n")}
\``;
response.addCode(`await expect(page.locator('body')).toMatchAriaSnapshot(${ariaSnapshot});`);
response.addTextResult("Done");
}
});
const verifyValue = (0, import_tool.defineTabTool)({
capability: "testing",
schema: {
name: "browser_verify_value",
title: "Verify value",
description: "Verify element value",
inputSchema: import_zodBundle.z.object({
type: import_zodBundle.z.enum(["textbox", "checkbox", "radio", "combobox", "slider"]).describe("Type of the element"),
element: import_zodBundle.z.string().describe("Human-readable element description"),
ref: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot"),
selector: import_zodBundle.z.string().optional().describe('CSS or role selector for the target element, when "ref" is not available'),
value: import_zodBundle.z.string().describe('Value to verify. For checkbox, use "true" or "false".')
}),
type: "assertion"
},
handle: async (tab, params, response) => {
const { locator, resolved } = await tab.refLocator({ ref: params.ref, selector: params.selector, element: params.element });
const locatorSource = `page.${resolved}`;
if (params.type === "textbox" || params.type === "slider" || params.type === "combobox") {
const value = await locator.inputValue(tab.expectTimeoutOptions);
if (value !== params.value) {
response.addError(`Expected value "${params.value}", but got "${value}"`);
return;
}
response.addCode(`await expect(${locatorSource}).toHaveValue(${(0, import_stringUtils.escapeWithQuotes)(params.value)});`);
} else if (params.type === "checkbox" || params.type === "radio") {
const value = await locator.isChecked(tab.expectTimeoutOptions);
if (value !== (params.value === "true")) {
response.addError(`Expected value "${params.value}", but got "${value}"`);
return;
}
const matcher = value ? "toBeChecked" : "not.toBeChecked";
response.addCode(`await expect(${locatorSource}).${matcher}();`);
}
response.addTextResult("Done");
}
});
var verify_default = [
verifyElement,
verifyText,
verifyList,
verifyValue
];
+98
View File
@@ -0,0 +1,98 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var video_exports = {};
__export(video_exports, {
default: () => video_default
});
module.exports = __toCommonJS(video_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const videoStart = (0, import_tool.defineTool)({
capability: "devtools",
schema: {
name: "browser_start_video",
title: "Start video",
description: "Start video recording",
inputSchema: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("Filename to save the video."),
size: import_zodBundle.z.object({
width: import_zodBundle.z.number().describe("Video width"),
height: import_zodBundle.z.number().describe("Video height")
}).optional().describe("Video size")
}),
type: "readOnly"
},
handle: async (context, params, response) => {
const resolvedFile = await response.resolveClientFile({ prefix: "video", ext: "webm", suggestedFilename: params.filename }, "Video");
await context.startVideoRecording(resolvedFile.fileName, { size: params.size });
response.addTextResult("Video recording started.");
}
});
const videoStop = (0, import_tool.defineTool)({
capability: "devtools",
schema: {
name: "browser_stop_video",
title: "Stop video",
description: "Stop video recording",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (context, params, response) => {
const fileNames = await context.stopVideoRecording();
if (!fileNames.length) {
response.addTextResult("No videos were recorded.");
return;
}
for (const fileName of fileNames) {
const resolvedFile = await response.resolveClientFile({
prefix: "video",
ext: "webm",
suggestedFilename: fileName
}, "Video");
await response.addFileResult(resolvedFile, null);
}
}
});
const videoChapter = (0, import_tool.defineTool)({
capability: "devtools",
schema: {
name: "browser_video_chapter",
title: "Video chapter",
description: "Add a chapter marker to the video recording. Shows a full-screen chapter card with blurred backdrop.",
inputSchema: import_zodBundle.z.object({
title: import_zodBundle.z.string().describe("Chapter title"),
description: import_zodBundle.z.string().optional().describe("Chapter description"),
duration: import_zodBundle.z.number().optional().describe("Duration in milliseconds to show the chapter card")
}),
type: "readOnly"
},
handle: async (context, params, response) => {
const tab = context.currentTabOrDie();
await tab.page.screencast.showChapter(params.title, {
description: params.description,
duration: params.duration
});
response.addTextResult(`Chapter '${params.title}' added.`);
}
});
var video_default = [
videoStart,
videoStop,
videoChapter
];
+63
View File
@@ -0,0 +1,63 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var wait_exports = {};
__export(wait_exports, {
default: () => wait_default
});
module.exports = __toCommonJS(wait_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const wait = (0, import_tool.defineTool)({
capability: "core",
schema: {
name: "browser_wait_for",
title: "Wait for",
description: "Wait for text to appear or disappear or a specified time to pass",
inputSchema: import_zodBundle.z.object({
time: import_zodBundle.z.number().optional().describe("The time to wait in seconds"),
text: import_zodBundle.z.string().optional().describe("The text to wait for"),
textGone: import_zodBundle.z.string().optional().describe("The text to wait for to disappear")
}),
type: "assertion"
},
handle: async (context, params, response) => {
if (!params.text && !params.textGone && !params.time)
throw new Error("Either time, text or textGone must be provided");
if (params.time) {
response.addCode(`await new Promise(f => setTimeout(f, ${params.time} * 1000));`);
await new Promise((f) => setTimeout(f, Math.min(3e4, params.time * 1e3)));
}
const tab = context.currentTabOrDie();
const locator = params.text ? tab.page.getByText(params.text).first() : void 0;
const goneLocator = params.textGone ? tab.page.getByText(params.textGone).first() : void 0;
if (goneLocator) {
response.addCode(`await page.getByText(${JSON.stringify(params.textGone)}).first().waitFor({ state: 'hidden' });`);
await goneLocator.waitFor({ state: "hidden" });
}
if (locator) {
response.addCode(`await page.getByText(${JSON.stringify(params.text)}).first().waitFor({ state: 'visible' });`);
await locator.waitFor({ state: "visible" });
}
response.addTextResult(`Waited for ${params.text || params.textGone || params.time}`);
response.setIncludeSnapshot();
}
});
var wait_default = [
wait
];
+223
View File
@@ -0,0 +1,223 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var webstorage_exports = {};
__export(webstorage_exports, {
default: () => webstorage_default
});
module.exports = __toCommonJS(webstorage_exports);
var import_zodBundle = require("../../zodBundle");
var import_tool = require("./tool");
const localStorageList = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_localstorage_list",
title: "List localStorage",
description: "List all localStorage key-value pairs",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (tab, params, response) => {
const items = await tab.page.evaluate(() => {
const result = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key !== null)
result.push({ key, value: localStorage.getItem(key) || "" });
}
return result;
});
if (items.length === 0)
response.addTextResult("No localStorage items found");
else
response.addTextResult(items.map((item) => `${item.key}=${item.value}`).join("\n"));
response.addCode(`await page.evaluate(() => ({ ...localStorage }));`);
}
});
const localStorageGet = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_localstorage_get",
title: "Get localStorage item",
description: "Get a localStorage item by key",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to get")
}),
type: "readOnly"
},
handle: async (tab, params, response) => {
const value = await tab.page.evaluate((key) => localStorage.getItem(key), params.key);
if (value === null)
response.addTextResult(`localStorage key '${params.key}' not found`);
else
response.addTextResult(`${params.key}=${value}`);
response.addCode(`await page.evaluate(() => localStorage.getItem('${params.key}'));`);
}
});
const localStorageSet = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_localstorage_set",
title: "Set localStorage item",
description: "Set a localStorage item",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to set"),
value: import_zodBundle.z.string().describe("Value to set")
}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.evaluate(({ key, value }) => localStorage.setItem(key, value), params);
response.addCode(`await page.evaluate(() => localStorage.setItem('${params.key}', '${params.value}'));`);
}
});
const localStorageDelete = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_localstorage_delete",
title: "Delete localStorage item",
description: "Delete a localStorage item",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to delete")
}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.evaluate((key) => localStorage.removeItem(key), params.key);
response.addCode(`await page.evaluate(() => localStorage.removeItem('${params.key}'));`);
}
});
const localStorageClear = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_localstorage_clear",
title: "Clear localStorage",
description: "Clear all localStorage",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.evaluate(() => localStorage.clear());
response.addCode(`await page.evaluate(() => localStorage.clear());`);
}
});
const sessionStorageList = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_sessionstorage_list",
title: "List sessionStorage",
description: "List all sessionStorage key-value pairs",
inputSchema: import_zodBundle.z.object({}),
type: "readOnly"
},
handle: async (tab, params, response) => {
const items = await tab.page.evaluate(() => {
const result = [];
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
if (key !== null)
result.push({ key, value: sessionStorage.getItem(key) || "" });
}
return result;
});
if (items.length === 0)
response.addTextResult("No sessionStorage items found");
else
response.addTextResult(items.map((item) => `${item.key}=${item.value}`).join("\n"));
response.addCode(`await page.evaluate(() => ({ ...sessionStorage }));`);
}
});
const sessionStorageGet = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_sessionstorage_get",
title: "Get sessionStorage item",
description: "Get a sessionStorage item by key",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to get")
}),
type: "readOnly"
},
handle: async (tab, params, response) => {
const value = await tab.page.evaluate((key) => sessionStorage.getItem(key), params.key);
if (value === null)
response.addTextResult(`sessionStorage key '${params.key}' not found`);
else
response.addTextResult(`${params.key}=${value}`);
response.addCode(`await page.evaluate(() => sessionStorage.getItem('${params.key}'));`);
}
});
const sessionStorageSet = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_sessionstorage_set",
title: "Set sessionStorage item",
description: "Set a sessionStorage item",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to set"),
value: import_zodBundle.z.string().describe("Value to set")
}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.evaluate(({ key, value }) => sessionStorage.setItem(key, value), params);
response.addCode(`await page.evaluate(() => sessionStorage.setItem('${params.key}', '${params.value}'));`);
}
});
const sessionStorageDelete = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_sessionstorage_delete",
title: "Delete sessionStorage item",
description: "Delete a sessionStorage item",
inputSchema: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to delete")
}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.evaluate((key) => sessionStorage.removeItem(key), params.key);
response.addCode(`await page.evaluate(() => sessionStorage.removeItem('${params.key}'));`);
}
});
const sessionStorageClear = (0, import_tool.defineTabTool)({
capability: "storage",
schema: {
name: "browser_sessionstorage_clear",
title: "Clear sessionStorage",
description: "Clear all sessionStorage",
inputSchema: import_zodBundle.z.object({}),
type: "action"
},
handle: async (tab, params, response) => {
await tab.page.evaluate(() => sessionStorage.clear());
response.addCode(`await page.evaluate(() => sessionStorage.clear());`);
}
});
var webstorage_default = [
localStorageList,
localStorageGet,
localStorageSet,
localStorageDelete,
localStorageClear,
sessionStorageList,
sessionStorageGet,
sessionStorageSet,
sessionStorageDelete,
sessionStorageClear
];
+6
View File
@@ -0,0 +1,6 @@
"use strict";
var import_program = require("./program");
(0, import_program.program)().catch((e) => {
console.error(e.message);
process.exit(1);
});
+399
View File
@@ -0,0 +1,399 @@
{
"global": "Usage: playwright-cli <command> [args] [options]\nUsage: playwright-cli -s=<session> <command> [args] [options]\n\nCore:\n open [url] open the browser\n attach <name> attach to a running playwright browser\n close close the browser\n goto <url> navigate to a url\n type <text> type text into editable element\n click <target> [button] perform click on a web page\n dblclick <target> [button] perform double click on a web page\n fill <target> <text> fill text into editable element\n drag <startElement> <endElement> perform drag and drop between two elements\n hover <target> hover over element on page\n select <target> <val> select an option in a dropdown\n upload <file> upload one or multiple files\n check <target> check a checkbox or radio button\n uncheck <target> uncheck a checkbox or radio button\n snapshot [element] capture page snapshot to obtain element ref\n eval <func> [element] evaluate javascript expression on page or element\n dialog-accept [prompt] accept a dialog\n dialog-dismiss dismiss a dialog\n resize <w> <h> resize the browser window\n delete-data delete session data\n\nNavigation:\n go-back go back to the previous page\n go-forward go forward to the next page\n reload reload the current page\n\nKeyboard:\n press <key> press a key on the keyboard, `a`, `arrowleft`\n keydown <key> press a key down on the keyboard\n keyup <key> press a key up on the keyboard\n\nMouse:\n mousemove <x> <y> move mouse to a given position\n mousedown [button] press mouse down\n mouseup [button] press mouse up\n mousewheel <dx> <dy> scroll mouse wheel\n\nSave as:\n screenshot [target] screenshot of the current page or element\n pdf save page as pdf\n\nTabs:\n tab-list list all tabs\n tab-new [url] create a new tab\n tab-close [index] close a browser tab\n tab-select <index> select a browser tab\n\nStorage:\n state-load <filename> loads browser storage (authentication) state from a file\n state-save [filename] saves the current storage (authentication) state to a file\n cookie-list list all cookies (optionally filtered by domain/path)\n cookie-get <name> get a specific cookie by name\n cookie-set <name> <value> set a cookie with optional flags\n cookie-delete <name> delete a specific cookie\n cookie-clear clear all cookies\n localstorage-list list all localstorage key-value pairs\n localstorage-get <key> get a localstorage item by key\n localstorage-set <key> <value> set a localstorage item\n localstorage-delete <key> delete a localstorage item\n localstorage-clear clear all localstorage\n sessionstorage-list list all sessionstorage key-value pairs\n sessionstorage-get <key> get a sessionstorage item by key\n sessionstorage-set <key> <value> set a sessionstorage item\n sessionstorage-delete <key> delete a sessionstorage item\n sessionstorage-clear clear all sessionstorage\n\nNetwork:\n route <pattern> mock network requests matching a url pattern\n route-list list all active network routes\n unroute [pattern] remove routes matching a pattern (or all routes)\n network-state-set <state> set the browser network state to online or offline\n\nDevTools:\n console [min-level] list console messages\n run-code [code] run playwright code snippet\n network list all network requests since loading the page\n tracing-start start trace recording\n tracing-stop stop trace recording\n video-start [filename] start video recording\n video-stop stop video recording\n video-chapter <title> add a chapter marker to the video recording\n show show browser devtools\n pause-at <location> run the test up to a specific location and pause there\n resume resume the test execution\n step-over step over the next call in the test\n\nInstall:\n install initialize workspace\n install-browser [browser] install browser\n\nBrowser sessions:\n list list browser sessions\n close-all close all browser sessions\n kill-all forcefully kill all browser sessions (for stale/zombie processes)\n\nGlobal options:\n --help [command] print help\n --version print version",
"commands": {
"open": {
"help": "playwright-cli open [url]\n\nOpen the browser\n\nArguments:\n [url] the url to navigate to\nOptions:\n --browser browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.\n --config path to the configuration file, defaults to .playwright/cli.config.json\n --extension connect to browser extension\n --headed run browser in headed mode\n --persistent use persistent browser profile\n --profile use persistent browser profile, store profile in specified directory.",
"flags": {
"browser": "string",
"config": "string",
"extension": "boolean",
"headed": "boolean",
"persistent": "boolean",
"profile": "string"
}
},
"attach": {
"help": "playwright-cli attach <name>\n\nAttach to a running Playwright browser\n\nArguments:\n <name> name or endpoint of the browser to attach to\nOptions:\n --config path to the configuration file, defaults to .playwright/cli.config.json\n --session session name alias (defaults to the attach target name)",
"flags": {
"config": "string",
"session": "string"
}
},
"close": {
"help": "playwright-cli close \n\nClose the browser\n",
"flags": {}
},
"goto": {
"help": "playwright-cli goto <url>\n\nNavigate to a URL\n\nArguments:\n <url> the url to navigate to",
"flags": {}
},
"type": {
"help": "playwright-cli type <text>\n\nType text into editable element\n\nArguments:\n <text> text to type into the element\nOptions:\n --submit whether to submit entered text (press enter after)",
"flags": {
"submit": "boolean"
}
},
"click": {
"help": "playwright-cli click <target> [button]\n\nPerform click on a web page\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector\n [button] button to click, defaults to left\nOptions:\n --modifiers modifier keys to press",
"flags": {
"modifiers": "string"
}
},
"dblclick": {
"help": "playwright-cli dblclick <target> [button]\n\nPerform double click on a web page\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector\n [button] button to click, defaults to left\nOptions:\n --modifiers modifier keys to press",
"flags": {
"modifiers": "string"
}
},
"fill": {
"help": "playwright-cli fill <target> <text>\n\nFill text into editable element\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector\n <text> text to fill into the element\nOptions:\n --submit whether to submit entered text (press enter after)",
"flags": {
"submit": "boolean"
}
},
"drag": {
"help": "playwright-cli drag <startElement> <endElement>\n\nPerform drag and drop between two elements\n\nArguments:\n <startElement> exact source element reference from the page snapshot, or a unique element selector\n <endElement> exact target element reference from the page snapshot, or a unique element selector",
"flags": {}
},
"hover": {
"help": "playwright-cli hover <target>\n\nHover over element on page\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector",
"flags": {}
},
"select": {
"help": "playwright-cli select <target> <val>\n\nSelect an option in a dropdown\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector\n <val> value to select in the dropdown",
"flags": {}
},
"upload": {
"help": "playwright-cli upload <file>\n\nUpload one or multiple files\n\nArguments:\n <file> the absolute paths to the files to upload",
"flags": {}
},
"check": {
"help": "playwright-cli check <target>\n\nCheck a checkbox or radio button\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector",
"flags": {}
},
"uncheck": {
"help": "playwright-cli uncheck <target>\n\nUncheck a checkbox or radio button\n\nArguments:\n <target> exact target element reference from the page snapshot, or a unique element selector",
"flags": {}
},
"snapshot": {
"help": "playwright-cli snapshot [element]\n\nCapture page snapshot to obtain element ref\n\nArguments:\n [element] element selector of the root element to capture a partial snapshot instead of the whole page\nOptions:\n --filename save snapshot to markdown file instead of returning it in the response.\n --depth limit snapshot depth, unlimited by default.",
"flags": {
"filename": "string",
"depth": "string"
}
},
"eval": {
"help": "playwright-cli eval <func> [element]\n\nEvaluate JavaScript expression on page or element\n\nArguments:\n <func> () => { /* code */ } or (element) => { /* code */ } when element is provided\n [element] exact target element reference from the page snapshot, or a unique element selector\nOptions:\n --filename save evaluation result to a file instead of returning it in the response.",
"flags": {
"filename": "string"
}
},
"console": {
"help": "playwright-cli console [min-level]\n\nList console messages\n\nArguments:\n [min-level] level of the console messages to return. each level includes the messages of more severe levels. defaults to \"info\".\nOptions:\n --clear whether to clear the console list",
"flags": {
"clear": "boolean"
}
},
"dialog-accept": {
"help": "playwright-cli dialog-accept [prompt]\n\nAccept a dialog\n\nArguments:\n [prompt] the text of the prompt in case of a prompt dialog.",
"flags": {}
},
"dialog-dismiss": {
"help": "playwright-cli dialog-dismiss \n\nDismiss a dialog\n",
"flags": {}
},
"resize": {
"help": "playwright-cli resize <w> <h>\n\nResize the browser window\n\nArguments:\n <w> width of the browser window\n <h> height of the browser window",
"flags": {}
},
"run-code": {
"help": "playwright-cli run-code [code]\n\nRun Playwright code snippet\n\nArguments:\n [code] a javascript function containing playwright code to execute. it will be invoked with a single argument, page, which you can use for any page interaction.\nOptions:\n --filename load code from the specified file.",
"flags": {
"filename": "string"
}
},
"delete-data": {
"help": "playwright-cli delete-data \n\nDelete session data\n",
"flags": {}
},
"go-back": {
"help": "playwright-cli go-back \n\nGo back to the previous page\n",
"flags": {}
},
"go-forward": {
"help": "playwright-cli go-forward \n\nGo forward to the next page\n",
"flags": {}
},
"reload": {
"help": "playwright-cli reload \n\nReload the current page\n",
"flags": {}
},
"press": {
"help": "playwright-cli press <key>\n\nPress a key on the keyboard, `a`, `ArrowLeft`\n\nArguments:\n <key> name of the key to press or a character to generate, such as `arrowleft` or `a`",
"flags": {}
},
"keydown": {
"help": "playwright-cli keydown <key>\n\nPress a key down on the keyboard\n\nArguments:\n <key> name of the key to press or a character to generate, such as `arrowleft` or `a`",
"flags": {}
},
"keyup": {
"help": "playwright-cli keyup <key>\n\nPress a key up on the keyboard\n\nArguments:\n <key> name of the key to press or a character to generate, such as `arrowleft` or `a`",
"flags": {}
},
"mousemove": {
"help": "playwright-cli mousemove <x> <y>\n\nMove mouse to a given position\n\nArguments:\n <x> x coordinate\n <y> y coordinate",
"flags": {}
},
"mousedown": {
"help": "playwright-cli mousedown [button]\n\nPress mouse down\n\nArguments:\n [button] button to press, defaults to left",
"flags": {}
},
"mouseup": {
"help": "playwright-cli mouseup [button]\n\nPress mouse up\n\nArguments:\n [button] button to press, defaults to left",
"flags": {}
},
"mousewheel": {
"help": "playwright-cli mousewheel <dx> <dy>\n\nScroll mouse wheel\n\nArguments:\n <dx> x delta\n <dy> y delta",
"flags": {}
},
"screenshot": {
"help": "playwright-cli screenshot [target]\n\nscreenshot of the current page or element\n\nArguments:\n [target] exact target element reference from the page snapshot, or a unique element selector.\nOptions:\n --filename file name to save the screenshot to. defaults to `page-{timestamp}.{png|jpeg}` if not specified.\n --full-page when true, takes a screenshot of the full scrollable page, instead of the currently visible viewport.",
"flags": {
"filename": "string",
"full-page": "boolean"
}
},
"pdf": {
"help": "playwright-cli pdf \n\nSave page as PDF\n\nOptions:\n --filename file name to save the pdf to. defaults to `page-{timestamp}.pdf` if not specified.",
"flags": {
"filename": "string"
}
},
"tab-list": {
"help": "playwright-cli tab-list \n\nList all tabs\n",
"flags": {}
},
"tab-new": {
"help": "playwright-cli tab-new [url]\n\nCreate a new tab\n\nArguments:\n [url] the url to navigate to in the new tab. if omitted, the new tab will be blank.",
"flags": {}
},
"tab-close": {
"help": "playwright-cli tab-close [index]\n\nClose a browser tab\n\nArguments:\n [index] tab index. if omitted, current tab is closed.",
"flags": {}
},
"tab-select": {
"help": "playwright-cli tab-select <index>\n\nSelect a browser tab\n\nArguments:\n <index> tab index",
"flags": {}
},
"state-load": {
"help": "playwright-cli state-load <filename>\n\nLoads browser storage (authentication) state from a file\n\nArguments:\n <filename> file name to load the storage state from.",
"flags": {}
},
"state-save": {
"help": "playwright-cli state-save [filename]\n\nSaves the current storage (authentication) state to a file\n\nArguments:\n [filename] file name to save the storage state to.",
"flags": {}
},
"cookie-list": {
"help": "playwright-cli cookie-list \n\nList all cookies (optionally filtered by domain/path)\n\nOptions:\n --domain filter cookies by domain\n --path filter cookies by path",
"flags": {
"domain": "string",
"path": "string"
}
},
"cookie-get": {
"help": "playwright-cli cookie-get <name>\n\nGet a specific cookie by name\n\nArguments:\n <name> cookie name",
"flags": {}
},
"cookie-set": {
"help": "playwright-cli cookie-set <name> <value>\n\nSet a cookie with optional flags\n\nArguments:\n <name> cookie name\n <value> cookie value\nOptions:\n --domain cookie domain\n --path cookie path\n --expires cookie expiration as unix timestamp\n --httpOnly whether the cookie is http only\n --secure whether the cookie is secure\n --sameSite cookie samesite attribute",
"flags": {
"domain": "string",
"path": "string",
"expires": "string",
"httpOnly": "boolean",
"secure": "boolean",
"sameSite": "string"
}
},
"cookie-delete": {
"help": "playwright-cli cookie-delete <name>\n\nDelete a specific cookie\n\nArguments:\n <name> cookie name",
"flags": {}
},
"cookie-clear": {
"help": "playwright-cli cookie-clear \n\nClear all cookies\n",
"flags": {}
},
"localstorage-list": {
"help": "playwright-cli localstorage-list \n\nList all localStorage key-value pairs\n",
"flags": {}
},
"localstorage-get": {
"help": "playwright-cli localstorage-get <key>\n\nGet a localStorage item by key\n\nArguments:\n <key> key to get",
"flags": {}
},
"localstorage-set": {
"help": "playwright-cli localstorage-set <key> <value>\n\nSet a localStorage item\n\nArguments:\n <key> key to set\n <value> value to set",
"flags": {}
},
"localstorage-delete": {
"help": "playwright-cli localstorage-delete <key>\n\nDelete a localStorage item\n\nArguments:\n <key> key to delete",
"flags": {}
},
"localstorage-clear": {
"help": "playwright-cli localstorage-clear \n\nClear all localStorage\n",
"flags": {}
},
"sessionstorage-list": {
"help": "playwright-cli sessionstorage-list \n\nList all sessionStorage key-value pairs\n",
"flags": {}
},
"sessionstorage-get": {
"help": "playwright-cli sessionstorage-get <key>\n\nGet a sessionStorage item by key\n\nArguments:\n <key> key to get",
"flags": {}
},
"sessionstorage-set": {
"help": "playwright-cli sessionstorage-set <key> <value>\n\nSet a sessionStorage item\n\nArguments:\n <key> key to set\n <value> value to set",
"flags": {}
},
"sessionstorage-delete": {
"help": "playwright-cli sessionstorage-delete <key>\n\nDelete a sessionStorage item\n\nArguments:\n <key> key to delete",
"flags": {}
},
"sessionstorage-clear": {
"help": "playwright-cli sessionstorage-clear \n\nClear all sessionStorage\n",
"flags": {}
},
"route": {
"help": "playwright-cli route <pattern>\n\nMock network requests matching a URL pattern\n\nArguments:\n <pattern> url pattern to match (e.g., \"**/api/users\")\nOptions:\n --status http status code (default: 200)\n --body response body (text or json string)\n --content-type content-type header\n --header header to add in \"name: value\" format (repeatable)\n --remove-header comma-separated header names to remove",
"flags": {
"status": "string",
"body": "string",
"content-type": "string",
"header": "string",
"remove-header": "string"
}
},
"route-list": {
"help": "playwright-cli route-list \n\nList all active network routes\n",
"flags": {}
},
"unroute": {
"help": "playwright-cli unroute [pattern]\n\nRemove routes matching a pattern (or all routes)\n\nArguments:\n [pattern] url pattern to unroute (omit to remove all)",
"flags": {}
},
"network-state-set": {
"help": "playwright-cli network-state-set <state>\n\nSet the browser network state to online or offline\n\nArguments:\n <state> set to \"offline\" to simulate offline mode, \"online\" to restore network connectivity",
"flags": {}
},
"config-print": {
"help": "playwright-cli config-print \n\nPrint the final resolved config after merging CLI options, environment variables and config file.\n",
"flags": {}
},
"install": {
"help": "playwright-cli install \n\nInitialize workspace\n\nOptions:\n --skills install skills to \".claude\" (default) or \".agents\" dir",
"flags": {
"skills": "string"
}
},
"install-browser": {
"help": "playwright-cli install-browser [browser]\n\nInstall browser\n\nArguments:\n [browser] browser to install\nOptions:\n --with-deps install system dependencies for browsers\n --dry-run do not execute installation, only print information\n --list prints list of browsers from all playwright installations\n --force force reinstall of already installed browsers\n --only-shell only install headless shell when installing chromium\n --no-shell do not install chromium headless shell",
"flags": {
"with-deps": "boolean",
"dry-run": "boolean",
"list": "boolean",
"force": "boolean",
"only-shell": "boolean",
"no-shell": "boolean"
}
},
"network": {
"help": "playwright-cli network \n\nList all network requests since loading the page\n\nOptions:\n --static whether to include successful static resources like images, fonts, scripts, etc. defaults to false.\n --request-body whether to include request body. defaults to false.\n --request-headers whether to include request headers. defaults to false.\n --filter only return requests whose url matches this regexp (e.g. \"/api/.*user\").\n --clear whether to clear the network list",
"flags": {
"static": "boolean",
"request-body": "boolean",
"request-headers": "boolean",
"filter": "string",
"clear": "boolean"
}
},
"tracing-start": {
"help": "playwright-cli tracing-start \n\nStart trace recording\n",
"flags": {}
},
"tracing-stop": {
"help": "playwright-cli tracing-stop \n\nStop trace recording\n",
"flags": {}
},
"video-start": {
"help": "playwright-cli video-start [filename]\n\nStart video recording\n\nArguments:\n [filename] filename to save the video.\nOptions:\n --size video frame size, e.g. \"800x600\". if not specified, the size of the recorded video will fit 800x800.",
"flags": {
"size": "string"
}
},
"video-stop": {
"help": "playwright-cli video-stop \n\nStop video recording\n",
"flags": {}
},
"video-chapter": {
"help": "playwright-cli video-chapter <title>\n\nAdd a chapter marker to the video recording\n\nArguments:\n <title> chapter title.\nOptions:\n --description chapter description.\n --duration duration in milliseconds to show the chapter card.",
"flags": {
"description": "string",
"duration": "string"
}
},
"show": {
"help": "playwright-cli show \n\nShow browser DevTools\n",
"flags": {}
},
"pause-at": {
"help": "playwright-cli pause-at <location>\n\nRun the test up to a specific location and pause there\n\nArguments:\n <location> location to pause at. format is <file>:<line>, e.g. \"example.spec.ts:42\".",
"flags": {}
},
"resume": {
"help": "playwright-cli resume \n\nResume the test execution\n",
"flags": {}
},
"step-over": {
"help": "playwright-cli step-over \n\nStep over the next call in the test\n",
"flags": {}
},
"list": {
"help": "playwright-cli list \n\nList browser sessions\n\nOptions:\n --all list all browser sessions across all workspaces",
"flags": {
"all": "boolean"
}
},
"close-all": {
"help": "playwright-cli close-all \n\nClose all browser sessions\n",
"flags": {}
},
"kill-all": {
"help": "playwright-cli kill-all \n\nForcefully kill all browser sessions (for stale/zombie processes)\n",
"flags": {}
},
"tray": {
"help": "playwright-cli tray \n\nRun tray\n",
"flags": {}
}
},
"booleanOptions": [
"extension",
"headed",
"persistent",
"submit",
"clear",
"full-page",
"httpOnly",
"secure",
"with-deps",
"dry-run",
"list",
"force",
"only-shell",
"no-shell",
"static",
"request-body",
"request-headers",
"all"
]
}
+128
View File
@@ -0,0 +1,128 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var minimist_exports = {};
__export(minimist_exports, {
minimist: () => minimist
});
module.exports = __toCommonJS(minimist_exports);
function minimist(args, opts) {
if (!opts)
opts = {};
const bools = {};
const strings = {};
for (const key of toArray(opts.boolean))
bools[key] = true;
for (const key of toArray(opts.string))
strings[key] = true;
const argv = { _: [] };
function setArg(key, val) {
if (argv[key] === void 0 || bools[key] || typeof argv[key] === "boolean")
argv[key] = val;
else if (Array.isArray(argv[key]))
argv[key].push(val);
else
argv[key] = [argv[key], val];
}
let notFlags = [];
const doubleDashIndex = args.indexOf("--");
if (doubleDashIndex !== -1) {
notFlags = args.slice(doubleDashIndex + 1);
args = args.slice(0, doubleDashIndex);
}
for (let i = 0; i < args.length; i++) {
const arg = args[i];
let key;
let next;
if (/^--.+=/.test(arg)) {
const m = arg.match(/^--([^=]+)=([\s\S]*)$/);
key = m[1];
if (bools[key])
throw new Error(`boolean option '--${key}' should not be passed with '=value', use '--${key}' or '--no-${key}' instead`);
setArg(key, m[2]);
} else if (/^--no-.+/.test(arg)) {
key = arg.match(/^--no-(.+)/)[1];
setArg(key, false);
} else if (/^--.+/.test(arg)) {
key = arg.match(/^--(.+)/)[1];
next = args[i + 1];
if (next !== void 0 && !/^(-|--)[^-]/.test(next) && !bools[key]) {
setArg(key, next);
i += 1;
} else if (/^(true|false)$/.test(next)) {
setArg(key, next === "true");
i += 1;
} else {
setArg(key, strings[key] ? "" : true);
}
} else if (/^-[^-]+/.test(arg)) {
const letters = arg.slice(1, -1).split("");
let broken = false;
for (let j = 0; j < letters.length; j++) {
next = arg.slice(j + 2);
if (next === "-") {
setArg(letters[j], next);
continue;
}
if (/[A-Za-z]/.test(letters[j]) && next[0] === "=") {
setArg(letters[j], next.slice(1));
broken = true;
break;
}
if (/[A-Za-z]/.test(letters[j]) && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) {
setArg(letters[j], next);
broken = true;
break;
}
if (letters[j + 1] && letters[j + 1].match(/\W/)) {
setArg(letters[j], arg.slice(j + 2));
broken = true;
break;
} else {
setArg(letters[j], strings[letters[j]] ? "" : true);
}
}
key = arg.slice(-1)[0];
if (!broken && key !== "-") {
if (args[i + 1] && !/^(-|--)[^-]/.test(args[i + 1]) && !bools[key]) {
setArg(key, args[i + 1]);
i += 1;
} else if (args[i + 1] && /^(true|false)$/.test(args[i + 1])) {
setArg(key, args[i + 1] === "true");
i += 1;
} else {
setArg(key, strings[key] ? "" : true);
}
}
} else {
argv._.push(arg);
}
}
for (const k of notFlags)
argv._.push(k);
return argv;
}
function toArray(value) {
if (!value)
return [];
return Array.isArray(value) ? value : [value];
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
minimist
});
+350
View File
@@ -0,0 +1,350 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var program_exports = {};
__export(program_exports, {
calculateSha1: () => calculateSha1,
program: () => program
});
module.exports = __toCommonJS(program_exports);
var import_child_process = require("child_process");
var import_crypto = __toESM(require("crypto"));
var import_os = __toESM(require("os"));
var import_path = __toESM(require("path"));
var import_registry = require("./registry");
var import_session = require("./session");
var import_serverRegistry = require("../../serverRegistry");
var import_minimist = require("./minimist");
const globalOptions = [
"endpoint",
"browser",
"config",
"extension",
"headed",
"help",
"persistent",
"profile",
"session",
"version"
];
const booleanOptions = [
"all",
"help",
"version"
];
async function program(options) {
const clientInfo = (0, import_registry.createClientInfo)();
const help = require("./help.json");
const argv = process.argv.slice(2);
const boolean = [...help.booleanOptions, ...booleanOptions];
const args = (0, import_minimist.minimist)(argv, { boolean, string: ["_"] });
if (args.s) {
args.session = args.s;
delete args.s;
}
const commandName = args._?.[0];
if (args.version || args.v) {
console.log(options?.embedderVersion ?? clientInfo.version);
process.exit(0);
}
const command = commandName && help.commands[commandName];
if (args.help || args.h) {
if (command) {
console.log(command.help);
} else {
console.log("playwright-cli - run playwright mcp commands from terminal\n");
console.log(help.global);
}
process.exit(0);
}
if (!command) {
console.error(`Unknown command: ${commandName}
`);
console.log(help.global);
process.exit(1);
}
validateFlags(args, command);
const registry = await import_registry.Registry.load();
const sessionName = (0, import_registry.resolveSessionName)(args.session);
switch (commandName) {
case "list": {
await listSessions(registry, clientInfo, !!args.all);
return;
}
case "close-all": {
const entries = registry.entries(clientInfo);
for (const entry of entries)
await new import_session.Session(entry).stop(true);
return;
}
case "delete-data": {
const entry = registry.entry(clientInfo, sessionName);
if (!entry) {
console.log(`No user data found for browser '${sessionName}'.`);
return;
}
await new import_session.Session(entry).deleteData();
return;
}
case "kill-all": {
await killAllDaemons();
return;
}
case "open": {
await startSession(sessionName, registry, clientInfo, args);
return;
}
case "attach": {
const attachTarget = args._[1];
const attachSessionName = (0, import_registry.explicitSessionName)(args.session) ?? attachTarget;
args.endpoint = attachTarget;
args.session = attachSessionName;
await startSession(attachSessionName, registry, clientInfo, args);
return;
}
case "close":
const closeEntry = registry.entry(clientInfo, sessionName);
const session = closeEntry ? new import_session.Session(closeEntry) : void 0;
if (!session || !await session.canConnect()) {
console.log(`Browser '${sessionName}' is not open.`);
return;
}
await session.stop();
return;
case "install":
await runInitWorkspace(args);
return;
case "install-browser":
await installBrowser();
return;
case "show": {
const daemonScript = require.resolve("../dashboard/dashboardApp.js");
const child = (0, import_child_process.spawn)(process.execPath, [daemonScript], {
detached: true,
stdio: "ignore"
});
child.unref();
return;
}
default: {
const entry = registry.entry(clientInfo, sessionName);
if (!entry) {
console.log(`The browser '${sessionName}' is not open, please run open first`);
console.log("");
console.log(` playwright-cli${sessionName !== "default" ? ` -s=${sessionName}` : ""} open [params]`);
process.exit(1);
}
await runInSession(entry, clientInfo, args);
}
}
}
async function startSession(sessionName, registry, clientInfo, args) {
const entry = registry.entry(clientInfo, sessionName);
if (entry)
await new import_session.Session(entry).stop(true);
await import_session.Session.startDaemon(clientInfo, args);
const newEntry = await registry.loadEntry(clientInfo, sessionName);
await runInSession(newEntry, clientInfo, args);
}
async function runInSession(entry, clientInfo, args) {
for (const globalOption of globalOptions)
delete args[globalOption];
const session = new import_session.Session(entry);
const result = await session.run(clientInfo, args);
console.log(result.text);
}
async function runInitWorkspace(args) {
const cliPath = require.resolve("../cli-daemon/program.js");
const daemonArgs = [cliPath, "--init-workspace", ...args.skills ? ["--init-skills", String(args.skills)] : []];
await new Promise((resolve, reject) => {
const child = (0, import_child_process.spawn)(process.execPath, daemonArgs, {
stdio: "inherit",
cwd: process.cwd()
});
child.on("close", (code) => {
if (code === 0)
resolve();
else
reject(new Error(`Workspace initialization failed with exit code ${code}`));
});
});
}
async function installBrowser() {
const { program: program2 } = require("../../cli/program");
const argv = process.argv.map((arg) => arg === "install-browser" ? "install" : arg);
program2.parse(argv);
}
async function killAllDaemons() {
const platform = import_os.default.platform();
let killed = 0;
try {
if (platform === "win32") {
const result = (0, import_child_process.execSync)(
`powershell -NoProfile -NonInteractive -Command "Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -like '*run-mcp-server*' -or $_.CommandLine -like '*run-cli-server*' -or $_.CommandLine -like '*cli-daemon*' -or $_.CommandLine -like '*dashboardApp.js*' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue; $_.ProcessId }"`,
{ encoding: "utf-8" }
);
const pids = result.split("\n").map((line) => line.trim()).filter((line) => /^\d+$/.test(line));
for (const pid of pids)
console.log(`Killed daemon process ${pid}`);
killed = pids.length;
} else {
const result = (0, import_child_process.execSync)("ps aux", { encoding: "utf-8" });
const lines = result.split("\n");
for (const line of lines) {
if (line.includes("run-mcp-server") || line.includes("run-cli-server") || line.includes("cli-daemon") || line.includes("dashboardApp.js")) {
const parts = line.trim().split(/\s+/);
const pid = parts[1];
if (pid && /^\d+$/.test(pid)) {
try {
process.kill(parseInt(pid, 10), "SIGKILL");
console.log(`Killed daemon process ${pid}`);
killed++;
} catch {
}
}
}
}
}
} catch (e) {
}
if (killed === 0)
console.log("No daemon processes found.");
else if (killed > 0)
console.log(`Killed ${killed} daemon process${killed === 1 ? "" : "es"}.`);
}
async function listSessions(registry, clientInfo, all) {
console.log("### Browsers");
let count = 0;
const runningSessions = /* @__PURE__ */ new Set();
const entries = registry.entryMap();
for (const [workspace, list] of entries) {
if (!all && workspace !== clientInfo.workspaceDir)
continue;
count += await gcAndPrintSessions(clientInfo, list.map((entry) => new import_session.Session(entry)), all ? `${import_path.default.relative(process.cwd(), workspace) || "/"}:` : void 0, runningSessions);
}
const serverEntries = await import_serverRegistry.serverRegistry.list();
const filteredServerEntries = /* @__PURE__ */ new Map();
for (const [workspace, list] of serverEntries) {
if (!all && workspace !== clientInfo.workspaceDir)
continue;
const unattached = list.filter((d) => !runningSessions.has(d.title));
if (unattached.length)
filteredServerEntries.set(workspace, unattached);
}
if (filteredServerEntries.size) {
if (count)
console.log("");
console.log("### Browser servers available for attach");
}
for (const [workspace, list] of filteredServerEntries)
count += await gcAndPrintBrowserSessions(workspace, list);
if (!count)
console.log(" (no browsers)");
}
async function gcAndPrintSessions(clientInfo, sessions, header, runningSessions) {
const running = [];
const stopped = [];
for (const session of sessions) {
const canConnect = await session.canConnect();
if (canConnect) {
running.push(session);
runningSessions?.add(session.name);
} else {
if (session.config.cli.persistent)
stopped.push(session);
else
await session.deleteSessionConfig();
}
}
if (header && (running.length || stopped.length))
console.log(header);
for (const session of running)
console.log(await renderSessionStatus(clientInfo, session));
for (const session of stopped)
console.log(await renderSessionStatus(clientInfo, session));
return running.length + stopped.length;
}
async function gcAndPrintBrowserSessions(workspace, list) {
if (!list.length)
return 0;
if (workspace)
console.log(`${import_path.default.relative(process.cwd(), workspace) || "/"}:`);
for (const descriptor of list) {
const text = [];
text.push(`- browser "${descriptor.title}":`);
text.push(` - browser: ${descriptor.browser.browserName}`);
text.push(` - version: v${descriptor.playwrightVersion}`);
text.push(` - status: ${descriptor.canConnect ? "open" : "closed"}`);
if (descriptor.browser.userDataDir)
text.push(` - data-dir: ${descriptor.browser.userDataDir}`);
else
text.push(` - data-dir: <in-memory>`);
text.push(` - run \`playwright-cli attach "${descriptor.title}"\` to attach`);
console.log(text.join("\n"));
}
return list.length;
}
async function renderSessionStatus(clientInfo, session) {
const text = [];
const config = session.config;
const canConnect = await session.canConnect();
text.push(`- ${session.name}:`);
text.push(` - status: ${canConnect ? "open" : "closed"}`);
if (canConnect && !session.isCompatible(clientInfo))
text.push(` - version: v${config.version} [incompatible please re-open]`);
if (config.browser)
text.push(...(0, import_session.renderResolvedConfig)(config));
return text.join("\n");
}
function validateFlags(args, command) {
const unknownFlags = [];
for (const key of Object.keys(args)) {
if (key === "_")
continue;
if (globalOptions.includes(key))
continue;
if (!(key in command.flags))
unknownFlags.push(key);
}
if (unknownFlags.length) {
console.error(`Unknown option${unknownFlags.length > 1 ? "s" : ""}: ${unknownFlags.map((f) => `--${f}`).join(", ")}`);
console.log("");
console.log(command.help);
process.exit(1);
}
}
function calculateSha1(buffer) {
const hash = import_crypto.default.createHash("sha1");
hash.update(buffer);
return hash.digest("hex");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
calculateSha1,
program
});
+176
View File
@@ -0,0 +1,176 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var registry_exports = {};
__export(registry_exports, {
Registry: () => Registry,
baseDaemonDir: () => baseDaemonDir,
createClientInfo: () => createClientInfo,
explicitSessionName: () => explicitSessionName,
resolveSessionName: () => resolveSessionName
});
module.exports = __toCommonJS(registry_exports);
var import_crypto = __toESM(require("crypto"));
var import_fs = __toESM(require("fs"));
var import_os = __toESM(require("os"));
var import_path = __toESM(require("path"));
class Registry {
constructor(files) {
this._files = files;
}
entry(clientInfo, sessionName) {
const key = clientInfo.workspaceDir || clientInfo.workspaceDirHash;
const entries = this._files.get(key) || [];
return entries.find((entry) => entry.config.name === sessionName);
}
entries(clientInfo) {
const key = clientInfo.workspaceDir || clientInfo.workspaceDirHash;
return this._files.get(key) || [];
}
entryMap() {
return this._files;
}
async loadEntry(clientInfo, sessionName) {
const entry = await Registry._loadSessionEntry(clientInfo.daemonProfilesDir, sessionName + ".session");
if (!entry)
throw new Error(`Could not start the session "${sessionName}"`);
const key = clientInfo.workspaceDir || clientInfo.workspaceDirHash;
let list = this._files.get(key);
if (!list) {
list = [];
this._files.set(key, list);
}
const oldIndex = list.findIndex((e) => e.config.name === sessionName);
if (oldIndex !== -1)
list.splice(oldIndex, 1);
list.push(entry);
return entry;
}
static async _loadSessionEntry(daemonDir, file) {
try {
const fileName = import_path.default.join(daemonDir, file);
const data = await import_fs.default.promises.readFile(fileName, "utf-8");
const config = JSON.parse(data);
if (!config.name)
config.name = import_path.default.basename(file, ".session");
if (!config.timestamp)
config.timestamp = 0;
return { file: fileName, config, daemonDir };
} catch {
return void 0;
}
}
static async load() {
const sessions = /* @__PURE__ */ new Map();
const hashDirs = await import_fs.default.promises.readdir(baseDaemonDir).catch(() => []);
for (const workspaceDirHash of hashDirs) {
const daemonDir = import_path.default.join(baseDaemonDir, workspaceDirHash);
const stat = await import_fs.default.promises.stat(daemonDir);
if (!stat.isDirectory())
continue;
const files = await import_fs.default.promises.readdir(daemonDir).catch(() => []);
for (const file of files) {
if (!file.endsWith(".session"))
continue;
const entry = await Registry._loadSessionEntry(daemonDir, file);
if (!entry)
continue;
const key = entry.config.workspaceDir || workspaceDirHash;
let list = sessions.get(key);
if (!list) {
list = [];
sessions.set(key, list);
}
list.push(entry);
}
}
return new Registry(sessions);
}
}
const baseDaemonDir = (() => {
if (process.env.PLAYWRIGHT_DAEMON_SESSION_DIR)
return process.env.PLAYWRIGHT_DAEMON_SESSION_DIR;
let localCacheDir;
if (process.platform === "linux")
localCacheDir = process.env.XDG_CACHE_HOME || import_path.default.join(import_os.default.homedir(), ".cache");
if (process.platform === "darwin")
localCacheDir = import_path.default.join(import_os.default.homedir(), "Library", "Caches");
if (process.platform === "win32")
localCacheDir = process.env.LOCALAPPDATA || import_path.default.join(import_os.default.homedir(), "AppData", "Local");
if (!localCacheDir)
throw new Error("Unsupported platform: " + process.platform);
return import_path.default.join(localCacheDir, "ms-playwright", "daemon");
})();
function createClientInfo() {
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;
const hash = import_crypto.default.createHash("sha1");
hash.update(workspaceDir || packageLocation);
const workspaceDirHash = hash.digest("hex").substring(0, 16);
return {
version,
workspaceDir,
workspaceDirHash,
daemonProfilesDir: daemonProfilesDir(workspaceDirHash)
};
}
function findWorkspaceDir(startDir) {
let dir = startDir;
for (let i = 0; i < 10; i++) {
if (import_fs.default.existsSync(import_path.default.join(dir, ".playwright")))
return dir;
const parentDir = import_path.default.dirname(dir);
if (parentDir === dir)
break;
dir = parentDir;
}
return void 0;
}
const daemonProfilesDir = (workspaceDirHash) => {
return import_path.default.join(baseDaemonDir, workspaceDirHash);
};
function explicitSessionName(sessionName) {
return sessionName || process.env.PLAYWRIGHT_CLI_SESSION;
}
function resolveSessionName(sessionName) {
if (sessionName)
return sessionName;
if (process.env.PLAYWRIGHT_CLI_SESSION)
return process.env.PLAYWRIGHT_CLI_SESSION;
return "default";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Registry,
baseDaemonDir,
createClientInfo,
explicitSessionName,
resolveSessionName
});
+289
View File
@@ -0,0 +1,289 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var session_exports = {};
__export(session_exports, {
Session: () => Session,
renderResolvedConfig: () => renderResolvedConfig
});
module.exports = __toCommonJS(session_exports);
var import_child_process = require("child_process");
var import_fs = __toESM(require("fs"));
var import_net = __toESM(require("net"));
var import_os = __toESM(require("os"));
var import_path = __toESM(require("path"));
var import_socketConnection = require("../utils/socketConnection");
var import_registry = require("./registry");
class Session {
constructor(sessionFile) {
this.config = sessionFile.config;
this.name = this.config.name;
this._sessionFile = sessionFile;
}
isCompatible(clientInfo) {
return (0, import_socketConnection.compareSemver)(clientInfo.version, this.config.version) >= 0;
}
async run(clientInfo, args) {
if (!this.isCompatible(clientInfo))
throw new Error(`Client is v${clientInfo.version}, session '${this.name}' is v${this.config.version}. Run
playwright-cli${this.name !== "default" ? ` -s=${this.name}` : ""} open
to restart the browser session.`);
const { socket } = await this._connect();
if (!socket)
throw new Error(`Browser '${this.name}' is not open. Run
playwright-cli${this.name !== "default" ? ` -s=${this.name}` : ""} open
to start the browser session.`);
return await SocketConnectionClient.sendAndClose(socket, "run", { args, cwd: process.cwd() });
}
async stop(quiet = false) {
if (!await this.canConnect()) {
if (!quiet)
console.log(`Browser '${this.name}' is not open.`);
return;
}
await this._stopDaemon();
if (!quiet)
console.log(`Browser '${this.name}' closed
`);
}
async deleteData() {
await this.stop();
const dataDirs = await import_fs.default.promises.readdir(this._sessionFile.daemonDir).catch(() => []);
const matchingEntries = dataDirs.filter((file) => file === `${this.name}.session` || file.startsWith(`ud-${this.name}-`));
if (matchingEntries.length === 0) {
console.log(`No user data found for browser '${this.name}'.`);
return;
}
for (const entry of matchingEntries) {
const userDataDir = import_path.default.resolve(this._sessionFile.daemonDir, entry);
for (let i = 0; i < 5; i++) {
try {
await import_fs.default.promises.rm(userDataDir, { recursive: true });
if (entry.startsWith("ud-"))
console.log(`Deleted user data for browser '${this.name}'.`);
break;
} catch (e) {
if (e.code === "ENOENT") {
console.log(`No user data found for browser '${this.name}'.`);
break;
}
await new Promise((resolve) => setTimeout(resolve, 1e3));
if (i === 4)
throw e;
}
}
}
}
async _connect() {
return await new Promise((resolve) => {
const socket = import_net.default.createConnection(this.config.socketPath, () => {
resolve({ socket });
});
socket.on("error", (error) => {
if (import_os.default.platform() !== "win32")
void import_fs.default.promises.unlink(this.config.socketPath).catch(() => {
}).then(() => resolve({ error }));
else
resolve({ error });
});
});
}
async canConnect() {
const { socket } = await this._connect();
if (socket) {
socket.destroy();
return true;
}
return false;
}
static async startDaemon(clientInfo, cliArgs) {
await import_fs.default.promises.mkdir(clientInfo.daemonProfilesDir, { recursive: true });
const cliPath = require.resolve("../cli-daemon/program.js");
const sessionName = (0, import_registry.resolveSessionName)(cliArgs.session);
const errLog = import_path.default.join(clientInfo.daemonProfilesDir, sessionName + ".err");
const err = import_fs.default.openSync(errLog, "w");
const args = [
cliPath,
sessionName
];
if (cliArgs.headed)
args.push("--headed");
if (cliArgs.extension)
args.push("--extension");
if (cliArgs.browser)
args.push(`--browser=${cliArgs.browser}`);
if (cliArgs.persistent)
args.push("--persistent");
if (cliArgs.profile)
args.push(`--profile=${cliArgs.profile}`);
if (cliArgs.config)
args.push(`--config=${cliArgs.config}`);
if (cliArgs.endpoint || process.env.PLAYWRIGHT_CLI_SESSION)
args.push(`--endpoint=${cliArgs.endpoint || process.env.PLAYWRIGHT_CLI_SESSION}`);
const child = (0, import_child_process.spawn)(process.execPath, args, {
detached: true,
stdio: ["ignore", "pipe", err],
cwd: process.cwd()
// Will be used as root.
});
let signalled = false;
const sigintHandler = () => {
signalled = true;
child.kill("SIGINT");
};
const sigtermHandler = () => {
signalled = true;
child.kill("SIGTERM");
};
process.on("SIGINT", sigintHandler);
process.on("SIGTERM", sigtermHandler);
let outLog = "";
await new Promise((resolve, reject) => {
child.stdout.on("data", (data) => {
outLog += data.toString();
if (!outLog.includes("<EOF>"))
return;
const errorMatch = outLog.match(/### Error\n([\s\S]*)<EOF>/);
const error = errorMatch ? errorMatch[1].trim() : void 0;
if (error) {
const errLogContent = import_fs.default.readFileSync(errLog, "utf-8");
const message = error + (errLogContent ? "\n" + errLogContent : "");
reject(new Error(message));
}
const successMatch = outLog.match(/### Success\nDaemon listening on (.*)\n<EOF>/);
if (successMatch)
resolve();
});
child.on("close", (code) => {
if (!signalled) {
const errLogContent = import_fs.default.readFileSync(errLog, "utf-8");
const message = `Daemon process exited with code ${code}` + (errLogContent ? "\n" + errLogContent : "");
reject(new Error(message));
}
});
});
process.off("SIGINT", sigintHandler);
process.off("SIGTERM", sigtermHandler);
child.stdout.destroy();
child.unref();
if (cliArgs["endpoint"]) {
console.log(`### Session \`${sessionName}\` created, attached to \`${cliArgs["endpoint"]}\`.`);
console.log(`Run commands with: playwright-cli --session=${sessionName} <command>`);
} else {
console.log(`### Browser \`${sessionName}\` opened with pid ${child.pid}.`);
}
}
async _stopDaemon() {
const { socket, error: socketError } = await this._connect();
if (!socket) {
console.log(`Browser '${this.name}' is not open.${socketError ? " Error: " + socketError.message : ""}`);
return;
}
let error;
await SocketConnectionClient.sendAndClose(socket, "stop", {}).catch((e) => error = e);
if (error && !error?.message?.includes("Session closed"))
throw error;
}
async deleteSessionConfig() {
await import_fs.default.promises.rm(this._sessionFile.file).catch(() => {
});
}
}
function renderResolvedConfig(config) {
const channel = config.browser.launchOptions.channel ?? config.browser.browserName;
const lines = [];
if (channel)
lines.push(` - browser-type: ${channel}`);
if (!config.cli.persistent)
lines.push(` - user-data-dir: <in-memory>`);
else
lines.push(` - user-data-dir: ${config.browser.userDataDir}`);
lines.push(` - headed: ${!config.browser.launchOptions.headless}`);
return lines;
}
class SocketConnectionClient {
constructor(socket) {
this._nextMessageId = 1;
this._callbacks = /* @__PURE__ */ new Map();
this._connection = new import_socketConnection.SocketConnection(socket);
this._connection.onmessage = (message) => this._onMessage(message);
this._connection.onclose = () => this._rejectCallbacks();
}
async send(method, params = {}) {
const messageId = this._nextMessageId++;
const message = {
id: messageId,
method,
params
};
const responsePromise = new Promise((resolve, reject) => {
this._callbacks.set(messageId, { resolve, reject, method, params });
});
const [result] = await Promise.all([responsePromise, this._connection.send(message)]);
return result;
}
static async sendAndClose(socket, method, params = {}) {
const connection = new SocketConnectionClient(socket);
try {
return await connection.send(method, params);
} finally {
connection.close();
}
}
close() {
this._connection.close();
}
_onMessage(object) {
if (object.id && this._callbacks.has(object.id)) {
const callback = this._callbacks.get(object.id);
this._callbacks.delete(object.id);
if (object.error)
callback.reject(new Error(object.error));
else
callback.resolve(object.result);
} else if (object.id) {
throw new Error(`Unexpected message id: ${object.id}`);
} else {
throw new Error(`Unexpected message without id: ${JSON.stringify(object)}`);
}
}
_rejectCallbacks() {
for (const callback of this._callbacks.values())
callback.reject(new Error("Session closed"));
this._callbacks.clear();
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Session,
renderResolvedConfig
});
+328
View File
@@ -0,0 +1,328 @@
---
name: playwright-cli
description: Automate browser interactions, test web pages and work with Playwright tests.
allowed-tools: Bash(playwright-cli:*) Bash(npx:*) Bash(npm:*)
---
# Browser Automation with playwright-cli
## Quick start
```bash
# open new browser
playwright-cli open
# navigate to a page
playwright-cli goto https://playwright.dev
# interact with the page using refs from the snapshot
playwright-cli click e15
playwright-cli type "page.click"
playwright-cli press Enter
# take a screenshot (rarely used, as snapshot is more common)
playwright-cli screenshot
# close the browser
playwright-cli close
```
## Commands
### Core
```bash
playwright-cli open
# open and navigate right away
playwright-cli open https://example.com/
playwright-cli goto https://playwright.dev
playwright-cli type "search query"
playwright-cli click e3
playwright-cli dblclick e7
# --submit presses Enter after filling the element
playwright-cli fill e5 "user@example.com" --submit
playwright-cli drag e2 e8
playwright-cli hover e4
playwright-cli select e9 "option-value"
playwright-cli upload ./document.pdf
playwright-cli check e12
playwright-cli uncheck e12
playwright-cli snapshot
playwright-cli eval "document.title"
playwright-cli eval "el => el.textContent" e5
# get element id, class, or any attribute not visible in the snapshot
playwright-cli eval "el => el.id" e5
playwright-cli eval "el => el.getAttribute('data-testid')" e5
playwright-cli dialog-accept
playwright-cli dialog-accept "confirmation text"
playwright-cli dialog-dismiss
playwright-cli resize 1920 1080
playwright-cli close
```
### Navigation
```bash
playwright-cli go-back
playwright-cli go-forward
playwright-cli reload
```
### Keyboard
```bash
playwright-cli press Enter
playwright-cli press ArrowDown
playwright-cli keydown Shift
playwright-cli keyup Shift
```
### Mouse
```bash
playwright-cli mousemove 150 300
playwright-cli mousedown
playwright-cli mousedown right
playwright-cli mouseup
playwright-cli mouseup right
playwright-cli mousewheel 0 100
```
### Save as
```bash
playwright-cli screenshot
playwright-cli screenshot e5
playwright-cli screenshot --filename=page.png
playwright-cli pdf --filename=page.pdf
```
### Tabs
```bash
playwright-cli tab-list
playwright-cli tab-new
playwright-cli tab-new https://example.com/page
playwright-cli tab-close
playwright-cli tab-close 2
playwright-cli tab-select 0
```
### Storage
```bash
playwright-cli state-save
playwright-cli state-save auth.json
playwright-cli state-load auth.json
# Cookies
playwright-cli cookie-list
playwright-cli cookie-list --domain=example.com
playwright-cli cookie-get session_id
playwright-cli cookie-set session_id abc123
playwright-cli cookie-set session_id abc123 --domain=example.com --httpOnly --secure
playwright-cli cookie-delete session_id
playwright-cli cookie-clear
# LocalStorage
playwright-cli localstorage-list
playwright-cli localstorage-get theme
playwright-cli localstorage-set theme dark
playwright-cli localstorage-delete theme
playwright-cli localstorage-clear
# SessionStorage
playwright-cli sessionstorage-list
playwright-cli sessionstorage-get step
playwright-cli sessionstorage-set step 3
playwright-cli sessionstorage-delete step
playwright-cli sessionstorage-clear
```
### Network
```bash
playwright-cli route "**/*.jpg" --status=404
playwright-cli route "https://api.example.com/**" --body='{"mock": true}'
playwright-cli route-list
playwright-cli unroute "**/*.jpg"
playwright-cli unroute
```
### DevTools
```bash
playwright-cli console
playwright-cli console warning
playwright-cli network
playwright-cli run-code "async page => await page.context().grantPermissions(['geolocation'])"
playwright-cli run-code --filename=script.js
playwright-cli tracing-start
playwright-cli tracing-stop
playwright-cli video-start video.webm
playwright-cli video-chapter "Chapter Title" --description="Details" --duration=2000
playwright-cli video-stop
```
## Open parameters
```bash
# Use specific browser when creating session
playwright-cli open --browser=chrome
playwright-cli open --browser=firefox
playwright-cli open --browser=webkit
playwright-cli open --browser=msedge
# Connect to browser via extension
playwright-cli open --extension
# Use persistent profile (by default profile is in-memory)
playwright-cli open --persistent
# Use persistent profile with custom directory
playwright-cli open --profile=/path/to/profile
# Start with config file
playwright-cli open --config=my-config.json
# Close the browser
playwright-cli close
# Delete user data for the default session
playwright-cli delete-data
```
## Snapshots
After each command, playwright-cli provides a snapshot of the current browser state.
```bash
> playwright-cli goto https://example.com
### Page
- Page URL: https://example.com/
- Page Title: Example Domain
### Snapshot
[Snapshot](.playwright-cli/page-2026-02-14T19-22-42-679Z.yml)
```
You can also take a snapshot on demand using `playwright-cli snapshot` command. All the options below can be combined as needed.
```bash
# default - save to a file with timestamp-based name
playwright-cli snapshot
# save to file, use when snapshot is a part of the workflow result
playwright-cli snapshot --filename=after-click.yaml
# snapshot an element instead of the whole page
playwright-cli snapshot "#main"
# limit snapshot depth for efficiency, take a partial snapshot afterwards
playwright-cli snapshot --depth=4
playwright-cli snapshot e34
```
## Targeting elements
By default, use refs from the snapshot to interact with page elements.
```bash
# get snapshot with refs
playwright-cli snapshot
# interact using a ref
playwright-cli click e15
```
You can also use css selectors or Playwright locators.
```bash
# css selector
playwright-cli click "#main > button.submit"
# role locator
playwright-cli click "getByRole('button', { name: 'Submit' })"
# test id
playwright-cli click "getByTestId('submit-button')"
```
## Browser Sessions
```bash
# create new browser session named "mysession" with persistent profile
playwright-cli -s=mysession open example.com --persistent
# same with manually specified profile directory (use when requested explicitly)
playwright-cli -s=mysession open example.com --profile=/path/to/profile
playwright-cli -s=mysession click e6
playwright-cli -s=mysession close # stop a named browser
playwright-cli -s=mysession delete-data # delete user data for persistent session
playwright-cli list
# Close all browsers
playwright-cli close-all
# Forcefully kill all browser processes
playwright-cli kill-all
```
## Installation
If global `playwright-cli` command is not available, try a local version via `npx playwright-cli`:
```bash
npx --no-install playwright-cli --version
```
When local version is available, use `npx playwright-cli` in all commands. Otherwise, install `playwright-cli` as a global command:
```bash
npm install -g @playwright/cli@latest
```
## Example: Form submission
```bash
playwright-cli open https://example.com/form
playwright-cli snapshot
playwright-cli fill e1 "user@example.com"
playwright-cli fill e2 "password123"
playwright-cli click e3
playwright-cli snapshot
playwright-cli close
```
## Example: Multi-tab workflow
```bash
playwright-cli open https://example.com
playwright-cli tab-new https://example.com/other
playwright-cli tab-list
playwright-cli tab-select 0
playwright-cli snapshot
playwright-cli close
```
## Example: Debugging with DevTools
```bash
playwright-cli open https://example.com
playwright-cli click e4
playwright-cli fill e7 "test"
playwright-cli console
playwright-cli network
playwright-cli close
```
```bash
playwright-cli open https://example.com
playwright-cli tracing-start
playwright-cli click e4
playwright-cli fill e7 "test"
playwright-cli tracing-stop
playwright-cli close
```
## Specific tasks
* **Running and Debugging Playwright tests** [references/playwright-tests.md](references/playwright-tests.md)
* **Request mocking** [references/request-mocking.md](references/request-mocking.md)
* **Running Playwright code** [references/running-code.md](references/running-code.md)
* **Browser session management** [references/session-management.md](references/session-management.md)
* **Storage state (cookies, localStorage)** [references/storage-state.md](references/storage-state.md)
* **Test generation** [references/test-generation.md](references/test-generation.md)
* **Tracing** [references/tracing.md](references/tracing.md)
* **Video recording** [references/video-recording.md](references/video-recording.md)
* **Inspecting element attributes** [references/element-attributes.md](references/element-attributes.md)
@@ -0,0 +1,23 @@
# Inspecting Element Attributes
When the snapshot doesn't show an element's `id`, `class`, `data-*` attributes, or other DOM properties, use `eval` to inspect them.
## Examples
```bash
playwright-cli snapshot
# snapshot shows a button as e7 but doesn't reveal its id or data attributes
# get the element's id
playwright-cli eval "el => el.id" e7
# get all CSS classes
playwright-cli eval "el => el.className" e7
# get a specific attribute
playwright-cli eval "el => el.getAttribute('data-testid')" e7
playwright-cli eval "el => el.getAttribute('aria-label')" e7
# get a computed style property
playwright-cli eval "el => getComputedStyle(el).display" e7
```
@@ -0,0 +1,39 @@
# Running Playwright Tests
To run Playwright tests, use the `npx playwright test` command, or a package manager script. To avoid opening the interactive html report, use `PLAYWRIGHT_HTML_OPEN=never` environment variable.
```bash
# Run all tests
PLAYWRIGHT_HTML_OPEN=never npx playwright test
# Run all tests through a custom npm script
PLAYWRIGHT_HTML_OPEN=never npm run special-test-command
```
# Debugging Playwright Tests
To debug a failing Playwright test, run it with `--debug=cli` option. This command will pause the test at the start and print the debugging instructions.
**IMPORTANT**: run the command in the background and check the output until "Debugging Instructions" is printed.
Once instructions containing a session name are printed, use `playwright-cli` to attach the session and explore the page.
```bash
# Run the test
PLAYWRIGHT_HTML_OPEN=never npx playwright test --debug=cli
# ...
# ... debugging instructions for "tw-abcdef" session ...
# ...
# Attach to the test
playwright-cli attach tw-abcdef
```
Keep the test running in the background while you explore and look for a fix.
The test is paused at the start, so you should step over or pause at a particular location
where the problem is most likely to be.
Every action you perform with `playwright-cli` generates corresponding Playwright TypeScript code.
This code appears in the output and can be copied directly into the test. Most of the time, a specific locator or an expectation should be updated, but it could also be a bug in the app. Use your judgement.
After fixing the test, stop the background test run. Rerun to check that test passes.
@@ -0,0 +1,87 @@
# Request Mocking
Intercept, mock, modify, and block network requests.
## CLI Route Commands
```bash
# Mock with custom status
playwright-cli route "**/*.jpg" --status=404
# Mock with JSON body
playwright-cli route "**/api/users" --body='[{"id":1,"name":"Alice"}]' --content-type=application/json
# Mock with custom headers
playwright-cli route "**/api/data" --body='{"ok":true}' --header="X-Custom: value"
# Remove headers from requests
playwright-cli route "**/*" --remove-header=cookie,authorization
# List active routes
playwright-cli route-list
# Remove a route or all routes
playwright-cli unroute "**/*.jpg"
playwright-cli unroute
```
## URL Patterns
```
**/api/users - Exact path match
**/api/*/details - Wildcard in path
**/*.{png,jpg,jpeg} - Match file extensions
**/search?q=* - Match query parameters
```
## Advanced Mocking with run-code
For conditional responses, request body inspection, response modification, or delays:
### Conditional Response Based on Request
```bash
playwright-cli run-code "async page => {
await page.route('**/api/login', route => {
const body = route.request().postDataJSON();
if (body.username === 'admin') {
route.fulfill({ body: JSON.stringify({ token: 'mock-token' }) });
} else {
route.fulfill({ status: 401, body: JSON.stringify({ error: 'Invalid' }) });
}
});
}"
```
### Modify Real Response
```bash
playwright-cli run-code "async page => {
await page.route('**/api/user', async route => {
const response = await route.fetch();
const json = await response.json();
json.isPremium = true;
await route.fulfill({ response, json });
});
}"
```
### Simulate Network Failures
```bash
playwright-cli run-code "async page => {
await page.route('**/api/offline', route => route.abort('internetdisconnected'));
}"
# Options: connectionrefused, timedout, connectionreset, internetdisconnected
```
### Delayed Response
```bash
playwright-cli run-code "async page => {
await page.route('**/api/slow', async route => {
await new Promise(r => setTimeout(r, 3000));
route.fulfill({ body: JSON.stringify({ data: 'loaded' }) });
});
}"
```
@@ -0,0 +1,231 @@
# Running Custom Playwright Code
Use `run-code` to execute arbitrary Playwright code for advanced scenarios not covered by CLI commands.
## Syntax
```bash
playwright-cli run-code "async page => {
// Your Playwright code here
// Access page.context() for browser context operations
}"
```
## Geolocation
```bash
# Grant geolocation permission and set location
playwright-cli run-code "async page => {
await page.context().grantPermissions(['geolocation']);
await page.context().setGeolocation({ latitude: 37.7749, longitude: -122.4194 });
}"
# Set location to London
playwright-cli run-code "async page => {
await page.context().grantPermissions(['geolocation']);
await page.context().setGeolocation({ latitude: 51.5074, longitude: -0.1278 });
}"
# Clear geolocation override
playwright-cli run-code "async page => {
await page.context().clearPermissions();
}"
```
## Permissions
```bash
# Grant multiple permissions
playwright-cli run-code "async page => {
await page.context().grantPermissions([
'geolocation',
'notifications',
'camera',
'microphone'
]);
}"
# Grant permissions for specific origin
playwright-cli run-code "async page => {
await page.context().grantPermissions(['clipboard-read'], {
origin: 'https://example.com'
});
}"
```
## Media Emulation
```bash
# Emulate dark color scheme
playwright-cli run-code "async page => {
await page.emulateMedia({ colorScheme: 'dark' });
}"
# Emulate light color scheme
playwright-cli run-code "async page => {
await page.emulateMedia({ colorScheme: 'light' });
}"
# Emulate reduced motion
playwright-cli run-code "async page => {
await page.emulateMedia({ reducedMotion: 'reduce' });
}"
# Emulate print media
playwright-cli run-code "async page => {
await page.emulateMedia({ media: 'print' });
}"
```
## Wait Strategies
```bash
# Wait for network idle
playwright-cli run-code "async page => {
await page.waitForLoadState('networkidle');
}"
# Wait for specific element
playwright-cli run-code "async page => {
await page.locator('.loading').waitFor({ state: 'hidden' });
}"
# Wait for function to return true
playwright-cli run-code "async page => {
await page.waitForFunction(() => window.appReady === true);
}"
# Wait with timeout
playwright-cli run-code "async page => {
await page.locator('.result').waitFor({ timeout: 10000 });
}"
```
## Frames and Iframes
```bash
# Work with iframe
playwright-cli run-code "async page => {
const frame = page.locator('iframe#my-iframe').contentFrame();
await frame.locator('button').click();
}"
# Get all frames
playwright-cli run-code "async page => {
const frames = page.frames();
return frames.map(f => f.url());
}"
```
## File Downloads
```bash
# Handle file download
playwright-cli run-code "async page => {
const downloadPromise = page.waitForEvent('download');
await page.getByRole('link', { name: 'Download' }).click();
const download = await downloadPromise;
await download.saveAs('./downloaded-file.pdf');
return download.suggestedFilename();
}"
```
## Clipboard
```bash
# Read clipboard (requires permission)
playwright-cli run-code "async page => {
await page.context().grantPermissions(['clipboard-read']);
return await page.evaluate(() => navigator.clipboard.readText());
}"
# Write to clipboard
playwright-cli run-code "async page => {
await page.evaluate(text => navigator.clipboard.writeText(text), 'Hello clipboard!');
}"
```
## Page Information
```bash
# Get page title
playwright-cli run-code "async page => {
return await page.title();
}"
# Get current URL
playwright-cli run-code "async page => {
return page.url();
}"
# Get page content
playwright-cli run-code "async page => {
return await page.content();
}"
# Get viewport size
playwright-cli run-code "async page => {
return page.viewportSize();
}"
```
## JavaScript Execution
```bash
# Execute JavaScript and return result
playwright-cli run-code "async page => {
return await page.evaluate(() => {
return {
userAgent: navigator.userAgent,
language: navigator.language,
cookiesEnabled: navigator.cookieEnabled
};
});
}"
# Pass arguments to evaluate
playwright-cli run-code "async page => {
const multiplier = 5;
return await page.evaluate(m => document.querySelectorAll('li').length * m, multiplier);
}"
```
## Error Handling
```bash
# Try-catch in run-code
playwright-cli run-code "async page => {
try {
await page.getByRole('button', { name: 'Submit' }).click({ timeout: 1000 });
return 'clicked';
} catch (e) {
return 'element not found';
}
}"
```
## Complex Workflows
```bash
# Login and save state
playwright-cli run-code "async page => {
await page.goto('https://example.com/login');
await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
await page.getByRole('textbox', { name: 'Password' }).fill('secret');
await page.getByRole('button', { name: 'Sign in' }).click();
await page.waitForURL('**/dashboard');
await page.context().storageState({ path: 'auth.json' });
return 'Login successful';
}"
# Scrape data from multiple pages
playwright-cli run-code "async page => {
const results = [];
for (let i = 1; i <= 3; i++) {
await page.goto(\`https://example.com/page/\${i}\`);
const items = await page.locator('.item').allTextContents();
results.push(...items);
}
return results;
}"
```
@@ -0,0 +1,169 @@
# Browser Session Management
Run multiple isolated browser sessions concurrently with state persistence.
## Named Browser Sessions
Use `-s` flag to isolate browser contexts:
```bash
# Browser 1: Authentication flow
playwright-cli -s=auth open https://app.example.com/login
# Browser 2: Public browsing (separate cookies, storage)
playwright-cli -s=public open https://example.com
# Commands are isolated by browser session
playwright-cli -s=auth fill e1 "user@example.com"
playwright-cli -s=public snapshot
```
## Browser Session Isolation Properties
Each browser session has independent:
- Cookies
- LocalStorage / SessionStorage
- IndexedDB
- Cache
- Browsing history
- Open tabs
## Browser Session Commands
```bash
# List all browser sessions
playwright-cli list
# Stop a browser session (close the browser)
playwright-cli close # stop the default browser
playwright-cli -s=mysession close # stop a named browser
# Stop all browser sessions
playwright-cli close-all
# Forcefully kill all daemon processes (for stale/zombie processes)
playwright-cli kill-all
# Delete browser session user data (profile directory)
playwright-cli delete-data # delete default browser data
playwright-cli -s=mysession delete-data # delete named browser data
```
## Environment Variable
Set a default browser session name via environment variable:
```bash
export PLAYWRIGHT_CLI_SESSION="mysession"
playwright-cli open example.com # Uses "mysession" automatically
```
## Common Patterns
### Concurrent Scraping
```bash
#!/bin/bash
# Scrape multiple sites concurrently
# Start all browsers
playwright-cli -s=site1 open https://site1.com &
playwright-cli -s=site2 open https://site2.com &
playwright-cli -s=site3 open https://site3.com &
wait
# Take snapshots from each
playwright-cli -s=site1 snapshot
playwright-cli -s=site2 snapshot
playwright-cli -s=site3 snapshot
# Cleanup
playwright-cli close-all
```
### A/B Testing Sessions
```bash
# Test different user experiences
playwright-cli -s=variant-a open "https://app.com?variant=a"
playwright-cli -s=variant-b open "https://app.com?variant=b"
# Compare
playwright-cli -s=variant-a screenshot
playwright-cli -s=variant-b screenshot
```
### Persistent Profile
By default, browser profile is kept in memory only. Use `--persistent` flag on `open` to persist the browser profile to disk:
```bash
# Use persistent profile (auto-generated location)
playwright-cli open https://example.com --persistent
# Use persistent profile with custom directory
playwright-cli open https://example.com --profile=/path/to/profile
```
## Default Browser Session
When `-s` is omitted, commands use the default browser session:
```bash
# These use the same default browser session
playwright-cli open https://example.com
playwright-cli snapshot
playwright-cli close # Stops default browser
```
## Browser Session Configuration
Configure a browser session with specific settings when opening:
```bash
# Open with config file
playwright-cli open https://example.com --config=.playwright/my-cli.json
# Open with specific browser
playwright-cli open https://example.com --browser=firefox
# Open in headed mode
playwright-cli open https://example.com --headed
# Open with persistent profile
playwright-cli open https://example.com --persistent
```
## Best Practices
### 1. Name Browser Sessions Semantically
```bash
# GOOD: Clear purpose
playwright-cli -s=github-auth open https://github.com
playwright-cli -s=docs-scrape open https://docs.example.com
# AVOID: Generic names
playwright-cli -s=s1 open https://github.com
```
### 2. Always Clean Up
```bash
# Stop browsers when done
playwright-cli -s=auth close
playwright-cli -s=scrape close
# Or stop all at once
playwright-cli close-all
# If browsers become unresponsive or zombie processes remain
playwright-cli kill-all
```
### 3. Delete Stale Browser Data
```bash
# Remove old browser data to free disk space
playwright-cli -s=oldsession delete-data
```
@@ -0,0 +1,275 @@
# Storage Management
Manage cookies, localStorage, sessionStorage, and browser storage state.
## Storage State
Save and restore complete browser state including cookies and storage.
### Save Storage State
```bash
# Save to auto-generated filename (storage-state-{timestamp}.json)
playwright-cli state-save
# Save to specific filename
playwright-cli state-save my-auth-state.json
```
### Restore Storage State
```bash
# Load storage state from file
playwright-cli state-load my-auth-state.json
# Reload page to apply cookies
playwright-cli open https://example.com
```
### Storage State File Format
The saved file contains:
```json
{
"cookies": [
{
"name": "session_id",
"value": "abc123",
"domain": "example.com",
"path": "/",
"expires": 1735689600,
"httpOnly": true,
"secure": true,
"sameSite": "Lax"
}
],
"origins": [
{
"origin": "https://example.com",
"localStorage": [
{ "name": "theme", "value": "dark" },
{ "name": "user_id", "value": "12345" }
]
}
]
}
```
## Cookies
### List All Cookies
```bash
playwright-cli cookie-list
```
### Filter Cookies by Domain
```bash
playwright-cli cookie-list --domain=example.com
```
### Filter Cookies by Path
```bash
playwright-cli cookie-list --path=/api
```
### Get Specific Cookie
```bash
playwright-cli cookie-get session_id
```
### Set a Cookie
```bash
# Basic cookie
playwright-cli cookie-set session abc123
# Cookie with options
playwright-cli cookie-set session abc123 --domain=example.com --path=/ --httpOnly --secure --sameSite=Lax
# Cookie with expiration (Unix timestamp)
playwright-cli cookie-set remember_me token123 --expires=1735689600
```
### Delete a Cookie
```bash
playwright-cli cookie-delete session_id
```
### Clear All Cookies
```bash
playwright-cli cookie-clear
```
### Advanced: Multiple Cookies or Custom Options
For complex scenarios like adding multiple cookies at once, use `run-code`:
```bash
playwright-cli run-code "async page => {
await page.context().addCookies([
{ name: 'session_id', value: 'sess_abc123', domain: 'example.com', path: '/', httpOnly: true },
{ name: 'preferences', value: JSON.stringify({ theme: 'dark' }), domain: 'example.com', path: '/' }
]);
}"
```
## Local Storage
### List All localStorage Items
```bash
playwright-cli localstorage-list
```
### Get Single Value
```bash
playwright-cli localstorage-get token
```
### Set Value
```bash
playwright-cli localstorage-set theme dark
```
### Set JSON Value
```bash
playwright-cli localstorage-set user_settings '{"theme":"dark","language":"en"}'
```
### Delete Single Item
```bash
playwright-cli localstorage-delete token
```
### Clear All localStorage
```bash
playwright-cli localstorage-clear
```
### Advanced: Multiple Operations
For complex scenarios like setting multiple values at once, use `run-code`:
```bash
playwright-cli run-code "async page => {
await page.evaluate(() => {
localStorage.setItem('token', 'jwt_abc123');
localStorage.setItem('user_id', '12345');
localStorage.setItem('expires_at', Date.now() + 3600000);
});
}"
```
## Session Storage
### List All sessionStorage Items
```bash
playwright-cli sessionstorage-list
```
### Get Single Value
```bash
playwright-cli sessionstorage-get form_data
```
### Set Value
```bash
playwright-cli sessionstorage-set step 3
```
### Delete Single Item
```bash
playwright-cli sessionstorage-delete step
```
### Clear sessionStorage
```bash
playwright-cli sessionstorage-clear
```
## IndexedDB
### List Databases
```bash
playwright-cli run-code "async page => {
return await page.evaluate(async () => {
const databases = await indexedDB.databases();
return databases;
});
}"
```
### Delete Database
```bash
playwright-cli run-code "async page => {
await page.evaluate(() => {
indexedDB.deleteDatabase('myDatabase');
});
}"
```
## Common Patterns
### Authentication State Reuse
```bash
# Step 1: Login and save state
playwright-cli open https://app.example.com/login
playwright-cli snapshot
playwright-cli fill e1 "user@example.com"
playwright-cli fill e2 "password123"
playwright-cli click e3
# Save the authenticated state
playwright-cli state-save auth.json
# Step 2: Later, restore state and skip login
playwright-cli state-load auth.json
playwright-cli open https://app.example.com/dashboard
# Already logged in!
```
### Save and Restore Roundtrip
```bash
# Set up authentication state
playwright-cli open https://example.com
playwright-cli eval "() => { document.cookie = 'session=abc123'; localStorage.setItem('user', 'john'); }"
# Save state to file
playwright-cli state-save my-session.json
# ... later, in a new session ...
# Restore state
playwright-cli state-load my-session.json
playwright-cli open https://example.com
# Cookies and localStorage are restored!
```
## Security Notes
- Never commit storage state files containing auth tokens
- Add `*.auth-state.json` to `.gitignore`
- Delete state files after automation completes
- Use environment variables for sensitive data
- By default, sessions run in-memory mode which is safer for sensitive operations
@@ -0,0 +1,88 @@
# Test Generation
Generate Playwright test code automatically as you interact with the browser.
## How It Works
Every action you perform with `playwright-cli` generates corresponding Playwright TypeScript code.
This code appears in the output and can be copied directly into your test files.
## Example Workflow
```bash
# Start a session
playwright-cli open https://example.com/login
# Take a snapshot to see elements
playwright-cli snapshot
# Output shows: e1 [textbox "Email"], e2 [textbox "Password"], e3 [button "Sign In"]
# Fill form fields - generates code automatically
playwright-cli fill e1 "user@example.com"
# Ran Playwright code:
# await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
playwright-cli fill e2 "password123"
# Ran Playwright code:
# await page.getByRole('textbox', { name: 'Password' }).fill('password123');
playwright-cli click e3
# Ran Playwright code:
# await page.getByRole('button', { name: 'Sign In' }).click();
```
## Building a Test File
Collect the generated code into a Playwright test:
```typescript
import { test, expect } from '@playwright/test';
test('login flow', async ({ page }) => {
// Generated code from playwright-cli session:
await page.goto('https://example.com/login');
await page.getByRole('textbox', { name: 'Email' }).fill('user@example.com');
await page.getByRole('textbox', { name: 'Password' }).fill('password123');
await page.getByRole('button', { name: 'Sign In' }).click();
// Add assertions
await expect(page).toHaveURL(/.*dashboard/);
});
```
## Best Practices
### 1. Use Semantic Locators
The generated code uses role-based locators when possible, which are more resilient:
```typescript
// Generated (good - semantic)
await page.getByRole('button', { name: 'Submit' }).click();
// Avoid (fragile - CSS selectors)
await page.locator('#submit-btn').click();
```
### 2. Explore Before Recording
Take snapshots to understand the page structure before recording actions:
```bash
playwright-cli open https://example.com
playwright-cli snapshot
# Review the element structure
playwright-cli click e5
```
### 3. Add Assertions Manually
Generated code captures actions but not assertions. Add expectations in your test:
```typescript
// Generated action
await page.getByRole('button', { name: 'Submit' }).click();
// Manual assertion
await expect(page.getByText('Success')).toBeVisible();
```
@@ -0,0 +1,139 @@
# Tracing
Capture detailed execution traces for debugging and analysis. Traces include DOM snapshots, screenshots, network activity, and console logs.
## Basic Usage
```bash
# Start trace recording
playwright-cli tracing-start
# Perform actions
playwright-cli open https://example.com
playwright-cli click e1
playwright-cli fill e2 "test"
# Stop trace recording
playwright-cli tracing-stop
```
## Trace Output Files
When you start tracing, Playwright creates a `traces/` directory with several files:
### `trace-{timestamp}.trace`
**Action log** - The main trace file containing:
- Every action performed (clicks, fills, navigations)
- DOM snapshots before and after each action
- Screenshots at each step
- Timing information
- Console messages
- Source locations
### `trace-{timestamp}.network`
**Network log** - Complete network activity:
- All HTTP requests and responses
- Request headers and bodies
- Response headers and bodies
- Timing (DNS, connect, TLS, TTFB, download)
- Resource sizes
- Failed requests and errors
### `resources/`
**Resources directory** - Cached resources:
- Images, fonts, stylesheets, scripts
- Response bodies for replay
- Assets needed to reconstruct page state
## What Traces Capture
| Category | Details |
|----------|---------|
| **Actions** | Clicks, fills, hovers, keyboard input, navigations |
| **DOM** | Full DOM snapshot before/after each action |
| **Screenshots** | Visual state at each step |
| **Network** | All requests, responses, headers, bodies, timing |
| **Console** | All console.log, warn, error messages |
| **Timing** | Precise timing for each operation |
## Use Cases
### Debugging Failed Actions
```bash
playwright-cli tracing-start
playwright-cli open https://app.example.com
# This click fails - why?
playwright-cli click e5
playwright-cli tracing-stop
# Open trace to see DOM state when click was attempted
```
### Analyzing Performance
```bash
playwright-cli tracing-start
playwright-cli open https://slow-site.com
playwright-cli tracing-stop
# View network waterfall to identify slow resources
```
### Capturing Evidence
```bash
# Record a complete user flow for documentation
playwright-cli tracing-start
playwright-cli open https://app.example.com/checkout
playwright-cli fill e1 "4111111111111111"
playwright-cli fill e2 "12/25"
playwright-cli fill e3 "123"
playwright-cli click e4
playwright-cli tracing-stop
# Trace shows exact sequence of events
```
## Trace vs Video vs Screenshot
| Feature | Trace | Video | Screenshot |
|---------|-------|-------|------------|
| **Format** | .trace file | .webm video | .png/.jpeg image |
| **DOM inspection** | Yes | No | No |
| **Network details** | Yes | No | No |
| **Step-by-step replay** | Yes | Continuous | Single frame |
| **File size** | Medium | Large | Small |
| **Best for** | Debugging | Demos | Quick capture |
## Best Practices
### 1. Start Tracing Before the Problem
```bash
# Trace the entire flow, not just the failing step
playwright-cli tracing-start
playwright-cli open https://example.com
# ... all steps leading to the issue ...
playwright-cli tracing-stop
```
### 2. Clean Up Old Traces
Traces can consume significant disk space:
```bash
# Remove traces older than 7 days
find .playwright-cli/traces -mtime +7 -delete
```
## Limitations
- Traces add overhead to automation
- Large traces can consume significant disk space
- Some dynamic content may not replay perfectly
@@ -0,0 +1,143 @@
# Video Recording
Capture browser automation sessions as video for debugging, documentation, or verification. Produces WebM (VP8/VP9 codec).
## Basic Recording
```bash
# Open browser first
playwright-cli open
# Start recording
playwright-cli video-start demo.webm
# Add a chapter marker for section transitions
playwright-cli video-chapter "Getting Started" --description="Opening the homepage" --duration=2000
# Navigate and perform actions
playwright-cli goto https://example.com
playwright-cli snapshot
playwright-cli click e1
# Add another chapter
playwright-cli video-chapter "Filling Form" --description="Entering test data" --duration=2000
playwright-cli fill e2 "test input"
# Stop and save
playwright-cli video-stop
```
## Best Practices
### 1. Use Descriptive Filenames
```bash
# Include context in filename
playwright-cli video-start recordings/login-flow-2024-01-15.webm
playwright-cli video-start recordings/checkout-test-run-42.webm
```
### 2. Record entire hero scripts.
When recording a video for the user or as a proof of work, it is best to create a code snippet and execute it with run-code.
It allows pulling appropriate pauses between the actions and annotating the video. There are new Playwright APIs for that.
1) Perform scenario using CLI and take note of all locators and actions. You'll need those locators to request thier bounding boxes for highlight.
2) Create a file with the intended script for video (below). Use pressSequentially w/ delay for nice typing, make reasonable pauses.
3) Use playwright-cli run-code --file your-script.js
**Important**: Overlays are `pointer-events: none` — they do not interfere with page interactions. You can safely keep sticky overlays visible while clicking, filling, or performing any actions on the page.
```js
async page => {
await page.screencast.start({ path: 'video.webm', size: { width: 1280, height: 800 } });
await page.goto('https://demo.playwright.dev/todomvc');
// Show a chapter card — blurs the page and shows a dialog.
// Blocks until duration expires, then auto-removes.
// Use this for simple use cases, but always feel free to hand-craft your own beautiful
// overlay via await page.screencast.showOverlay().
await page.screencast.showChapter('Adding Todo Items', {
description: 'We will add several items to the todo list.',
duration: 2000,
});
// Perform action
await page.getByRole('textbox', { name: 'What needs to be done?' }).pressSequentially('Walk the dog', { delay: 60 });
await page.getByRole('textbox', { name: 'What needs to be done?' }).press('Enter');
await page.waitForTimeout(1000);
// Show next chapter
await page.screencast.showChapter('Verifying Results', {
description: 'Checking the item appeared in the list.',
duration: 2000,
});
// Add a sticky annotation that stays while you perform actions.
// Overlays are pointer-events: none, so they won't block clicks.
const annotation = await page.screencast.showOverlay(`
<div style="position: absolute; top: 8px; right: 8px;
padding: 6px 12px; background: rgba(0,0,0,0.7);
border-radius: 8px; font-size: 13px; color: white;">
✓ Item added successfully
</div>
`);
// Perform more actions while the annotation is visible
await page.getByRole('textbox', { name: 'What needs to be done?' }).pressSequentially('Buy groceries', { delay: 60 });
await page.getByRole('textbox', { name: 'What needs to be done?' }).press('Enter');
await page.waitForTimeout(1500);
// Remove the annotation when done
await annotation.dispose();
// You can also highlight relevant locators and provide contextual annotations.
const bounds = await page.getByText('Walk the dog').boundingBox();
await page.screencast.showOverlay(`
<div style="position: absolute;
top: ${bounds.y}px;
left: ${bounds.x}px;
width: ${bounds.width}px;
height: ${bounds.height}px;
border: 1px solid red;">
</div>
<div style="position: absolute;
top: ${bounds.y + bounds.height + 5}px;
left: ${bounds.x + bounds.width / 2}px;
transform: translateX(-50%);
padding: 6px;
background: #808080;
border-radius: 10px;
font-size: 14px;
color: white;">Check it out, it is right above this text
</div>
`, { duration: 2000 });
await page.screencast.stop();
}
```
Embrace creativity, overlays are powerful.
### Overlay API Summary
| Method | Use Case |
|--------|----------|
| `page.screencast.showChapter(title, { description?, duration?, styleSheet? })` | Full-screen chapter card with blurred backdrop — ideal for section transitions |
| `page.screencast.showOverlay(html, { duration? })` | Custom HTML overlay — use for callouts, labels, highlights |
| `disposable.dispose()` | Remove a sticky overlay added without duration |
| `page.screencast.hideOverlays()` / `page.screencast.showOverlays()` | Temporarily hide/show all overlays |
## Tracing vs Video
| Feature | Video | Tracing |
|---------|-------|---------|
| Output | WebM file | Trace file (viewable in Trace Viewer) |
| Shows | Visual recording | DOM snapshots, network, console, actions |
| Use case | Demos, documentation | Debugging, analysis |
| Size | Larger | Smaller |
## Limitations
- Recording adds slight overhead to automation
- Large recordings can consume significant disk space
+73
View File
@@ -0,0 +1,73 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var command_exports = {};
__export(command_exports, {
declareCommand: () => declareCommand,
parseCommand: () => parseCommand
});
module.exports = __toCommonJS(command_exports);
var import_zodBundle = require("../../zodBundle");
function declareCommand(command) {
return command;
}
const kEmptyOptions = import_zodBundle.z.object({});
const kEmptyArgs = import_zodBundle.z.object({});
function parseCommand(command, args) {
const optionsObject = { ...args };
delete optionsObject["_"];
const optionsSchema = (command.options ?? kEmptyOptions).strict();
const options = zodParse(optionsSchema, optionsObject, "option");
const argsSchema = (command.args ?? kEmptyArgs).strict();
const argNames = [...Object.keys(argsSchema.shape)];
const argv = args["_"].slice(1);
if (argv.length > argNames.length)
throw new Error(`error: too many arguments: expected ${argNames.length}, received ${argv.length}`);
const argsObject = {};
argNames.forEach((name, index) => argsObject[name] = argv[index]);
const parsedArgsObject = zodParse(argsSchema, argsObject, "argument");
const toolName = typeof command.toolName === "function" ? command.toolName({ ...parsedArgsObject, ...options }) : command.toolName;
const toolParams = command.toolParams({ ...parsedArgsObject, ...options });
return { toolName, toolParams };
}
function zodParse(schema, data, type) {
try {
return schema.parse(data);
} catch (e) {
throw new Error(e.issues.map((issue) => {
const keys = issue.code === "unrecognized_keys" ? issue.keys : [""];
const props = keys.map((key) => [...issue.path, key].filter(Boolean).join("."));
return props.map((prop) => {
const label = type === "option" ? `'--${prop}' option` : `'${prop}' argument`;
switch (issue.code) {
case "invalid_type":
return "error: " + label + ": " + issue.message.replace(/Invalid input:/, "").trim();
case "unrecognized_keys":
return "error: unknown " + label;
default:
return "error: " + label + ": " + issue.message;
}
});
}).flat().join("\n"));
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
declareCommand,
parseCommand
});
+956
View File
@@ -0,0 +1,956 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var commands_exports = {};
__export(commands_exports, {
commands: () => commands
});
module.exports = __toCommonJS(commands_exports);
var import_zodBundle = require("../../zodBundle");
var import_command = require("./command");
const numberArg = import_zodBundle.z.preprocess((val, ctx) => {
const number = Number(val);
if (Number.isNaN(number)) {
ctx.issues.push({
code: "custom",
message: `expected number, received '${val}'`,
input: val
});
}
return number;
}, import_zodBundle.z.number());
function asRef(refOrSelector) {
if (refOrSelector === void 0)
return {};
if (refOrSelector.match(/^(f\d+)?e\d+$/))
return { ref: refOrSelector };
return { ref: "", selector: refOrSelector };
}
const open = (0, import_command.declareCommand)({
name: "open",
description: "Open the browser",
category: "core",
args: import_zodBundle.z.object({
url: import_zodBundle.z.string().optional().describe("The URL to navigate to")
}),
options: import_zodBundle.z.object({
browser: import_zodBundle.z.string().optional().describe("Browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge."),
config: import_zodBundle.z.string().optional().describe("Path to the configuration file, defaults to .playwright/cli.config.json"),
extension: import_zodBundle.z.boolean().optional().describe("Connect to browser extension"),
headed: import_zodBundle.z.boolean().optional().describe("Run browser in headed mode"),
persistent: import_zodBundle.z.boolean().optional().describe("Use persistent browser profile"),
profile: import_zodBundle.z.string().optional().describe("Use persistent browser profile, store profile in specified directory.")
}),
toolName: ({ url }) => url ? "browser_navigate" : "browser_snapshot",
toolParams: ({ url }) => url ? { url: url || "about:blank" } : { filename: "<auto>" }
});
const attach = (0, import_command.declareCommand)({
name: "attach",
description: "Attach to a running Playwright browser",
category: "core",
args: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Name or endpoint of the browser to attach to")
}),
options: import_zodBundle.z.object({
config: import_zodBundle.z.string().optional().describe("Path to the configuration file, defaults to .playwright/cli.config.json"),
session: import_zodBundle.z.string().optional().describe("Session name alias (defaults to the attach target name)")
}),
toolName: "browser_snapshot",
toolParams: () => ({ filename: "<auto>" })
});
const close = (0, import_command.declareCommand)({
name: "close",
description: "Close the browser",
category: "core",
args: import_zodBundle.z.object({}),
toolName: "",
toolParams: () => ({})
});
const goto = (0, import_command.declareCommand)({
name: "goto",
description: "Navigate to a URL",
category: "core",
args: import_zodBundle.z.object({
url: import_zodBundle.z.string().describe("The URL to navigate to")
}),
toolName: "browser_navigate",
toolParams: ({ url }) => ({ url })
});
const goBack = (0, import_command.declareCommand)({
name: "go-back",
description: "Go back to the previous page",
category: "navigation",
args: import_zodBundle.z.object({}),
toolName: "browser_navigate_back",
toolParams: () => ({})
});
const goForward = (0, import_command.declareCommand)({
name: "go-forward",
description: "Go forward to the next page",
category: "navigation",
args: import_zodBundle.z.object({}),
toolName: "browser_navigate_forward",
toolParams: () => ({})
});
const reload = (0, import_command.declareCommand)({
name: "reload",
description: "Reload the current page",
category: "navigation",
args: import_zodBundle.z.object({}),
toolName: "browser_reload",
toolParams: () => ({})
});
const pressKey = (0, import_command.declareCommand)({
name: "press",
description: "Press a key on the keyboard, `a`, `ArrowLeft`",
category: "keyboard",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`")
}),
toolName: "browser_press_key",
toolParams: ({ key }) => ({ key })
});
const type = (0, import_command.declareCommand)({
name: "type",
description: "Type text into editable element",
category: "core",
args: import_zodBundle.z.object({
text: import_zodBundle.z.string().describe("Text to type into the element")
}),
options: import_zodBundle.z.object({
submit: import_zodBundle.z.boolean().optional().describe("Whether to submit entered text (press Enter after)")
}),
toolName: "browser_press_sequentially",
toolParams: ({ text, submit }) => ({ text, submit })
});
const keydown = (0, import_command.declareCommand)({
name: "keydown",
description: "Press a key down on the keyboard",
category: "keyboard",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`")
}),
toolName: "browser_keydown",
toolParams: ({ key }) => ({ key })
});
const keyup = (0, import_command.declareCommand)({
name: "keyup",
description: "Press a key up on the keyboard",
category: "keyboard",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Name of the key to press or a character to generate, such as `ArrowLeft` or `a`")
}),
toolName: "browser_keyup",
toolParams: ({ key }) => ({ key })
});
const mouseMove = (0, import_command.declareCommand)({
name: "mousemove",
description: "Move mouse to a given position",
category: "mouse",
args: import_zodBundle.z.object({
x: numberArg.describe("X coordinate"),
y: numberArg.describe("Y coordinate")
}),
toolName: "browser_mouse_move_xy",
toolParams: ({ x, y }) => ({ x, y })
});
const mouseDown = (0, import_command.declareCommand)({
name: "mousedown",
description: "Press mouse down",
category: "mouse",
args: import_zodBundle.z.object({
button: import_zodBundle.z.string().optional().describe("Button to press, defaults to left")
}),
toolName: "browser_mouse_down",
toolParams: ({ button }) => ({ button })
});
const mouseUp = (0, import_command.declareCommand)({
name: "mouseup",
description: "Press mouse up",
category: "mouse",
args: import_zodBundle.z.object({
button: import_zodBundle.z.string().optional().describe("Button to press, defaults to left")
}),
toolName: "browser_mouse_up",
toolParams: ({ button }) => ({ button })
});
const mouseWheel = (0, import_command.declareCommand)({
name: "mousewheel",
description: "Scroll mouse wheel",
category: "mouse",
args: import_zodBundle.z.object({
dx: numberArg.describe("X delta"),
dy: numberArg.describe("Y delta")
}),
toolName: "browser_mouse_wheel",
toolParams: ({ dx: deltaX, dy: deltaY }) => ({ deltaX, deltaY })
});
const click = (0, import_command.declareCommand)({
name: "click",
description: "Perform click on a web page",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector"),
button: import_zodBundle.z.string().optional().describe("Button to click, defaults to left")
}),
options: import_zodBundle.z.object({
modifiers: import_zodBundle.z.array(import_zodBundle.z.string()).optional().describe("Modifier keys to press")
}),
toolName: "browser_click",
toolParams: ({ target, button, modifiers }) => ({ ...asRef(target), button, modifiers })
});
const doubleClick = (0, import_command.declareCommand)({
name: "dblclick",
description: "Perform double click on a web page",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector"),
button: import_zodBundle.z.string().optional().describe("Button to click, defaults to left")
}),
options: import_zodBundle.z.object({
modifiers: import_zodBundle.z.array(import_zodBundle.z.string()).optional().describe("Modifier keys to press")
}),
toolName: "browser_click",
toolParams: ({ target, button, modifiers }) => ({ ...asRef(target), button, modifiers, doubleClick: true })
});
const drag = (0, import_command.declareCommand)({
name: "drag",
description: "Perform drag and drop between two elements",
category: "core",
args: import_zodBundle.z.object({
startElement: import_zodBundle.z.string().describe("Exact source element reference from the page snapshot, or a unique element selector"),
endElement: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector")
}),
toolName: "browser_drag",
toolParams: ({ startElement, endElement }) => {
const start = asRef(startElement);
const end = asRef(endElement);
return { startRef: start.ref, startSelector: start.selector, endRef: end.ref, endSelector: end.selector };
}
});
const fill = (0, import_command.declareCommand)({
name: "fill",
description: "Fill text into editable element",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector"),
text: import_zodBundle.z.string().describe("Text to fill into the element")
}),
options: import_zodBundle.z.object({
submit: import_zodBundle.z.boolean().optional().describe("Whether to submit entered text (press Enter after)")
}),
toolName: "browser_type",
toolParams: ({ target, text, submit }) => ({ ...asRef(target), text, submit })
});
const hover = (0, import_command.declareCommand)({
name: "hover",
description: "Hover over element on page",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector")
}),
toolName: "browser_hover",
toolParams: ({ target }) => ({ ...asRef(target) })
});
const select = (0, import_command.declareCommand)({
name: "select",
description: "Select an option in a dropdown",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector"),
val: import_zodBundle.z.string().describe("Value to select in the dropdown")
}),
toolName: "browser_select_option",
toolParams: ({ target, val: value }) => ({ ...asRef(target), values: [value] })
});
const fileUpload = (0, import_command.declareCommand)({
name: "upload",
description: "Upload one or multiple files",
category: "core",
args: import_zodBundle.z.object({
file: import_zodBundle.z.string().describe("The absolute paths to the files to upload")
}),
toolName: "browser_file_upload",
toolParams: ({ file }) => ({ paths: [file] })
});
const check = (0, import_command.declareCommand)({
name: "check",
description: "Check a checkbox or radio button",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector")
}),
toolName: "browser_check",
toolParams: ({ target }) => ({ ...asRef(target) })
});
const uncheck = (0, import_command.declareCommand)({
name: "uncheck",
description: "Uncheck a checkbox or radio button",
category: "core",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().describe("Exact target element reference from the page snapshot, or a unique element selector")
}),
toolName: "browser_uncheck",
toolParams: ({ target }) => ({ ...asRef(target) })
});
const snapshot = (0, import_command.declareCommand)({
name: "snapshot",
description: "Capture page snapshot to obtain element ref",
category: "core",
args: import_zodBundle.z.object({
element: import_zodBundle.z.string().optional().describe("Element selector of the root element to capture a partial snapshot instead of the whole page")
}),
options: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("Save snapshot to markdown file instead of returning it in the response."),
depth: numberArg.optional().describe("Limit snapshot depth, unlimited by default.")
}),
toolName: "browser_snapshot",
toolParams: ({ filename, element, depth }) => ({ filename, selector: element, depth })
});
const evaluate = (0, import_command.declareCommand)({
name: "eval",
description: "Evaluate JavaScript expression on page or element",
category: "core",
args: import_zodBundle.z.object({
func: import_zodBundle.z.string().describe("() => { /* code */ } or (element) => { /* code */ } when element is provided"),
element: import_zodBundle.z.string().optional().describe("Exact target element reference from the page snapshot, or a unique element selector")
}),
options: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("Save evaluation result to a file instead of returning it in the response.")
}),
toolName: "browser_evaluate",
toolParams: ({ func, element, filename }) => ({ function: func, filename, ...asRef(element) })
});
const dialogAccept = (0, import_command.declareCommand)({
name: "dialog-accept",
description: "Accept a dialog",
category: "core",
args: import_zodBundle.z.object({
prompt: import_zodBundle.z.string().optional().describe("The text of the prompt in case of a prompt dialog.")
}),
toolName: "browser_handle_dialog",
toolParams: ({ prompt: promptText }) => ({ accept: true, promptText })
});
const dialogDismiss = (0, import_command.declareCommand)({
name: "dialog-dismiss",
description: "Dismiss a dialog",
category: "core",
args: import_zodBundle.z.object({}),
toolName: "browser_handle_dialog",
toolParams: () => ({ accept: false })
});
const resize = (0, import_command.declareCommand)({
name: "resize",
description: "Resize the browser window",
category: "core",
args: import_zodBundle.z.object({
w: numberArg.describe("Width of the browser window"),
h: numberArg.describe("Height of the browser window")
}),
toolName: "browser_resize",
toolParams: ({ w: width, h: height }) => ({ width, height })
});
const runCode = (0, import_command.declareCommand)({
name: "run-code",
description: "Run Playwright code snippet",
category: "devtools",
args: import_zodBundle.z.object({
code: import_zodBundle.z.string().optional().describe("A JavaScript function containing Playwright code to execute. It will be invoked with a single argument, page, which you can use for any page interaction.")
}),
options: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("Load code from the specified file.")
}),
toolName: "browser_run_code",
toolParams: ({ code, filename }) => ({ code, filename })
});
const tabList = (0, import_command.declareCommand)({
name: "tab-list",
description: "List all tabs",
category: "tabs",
args: import_zodBundle.z.object({}),
toolName: "browser_tabs",
toolParams: () => ({ action: "list" })
});
const tabNew = (0, import_command.declareCommand)({
name: "tab-new",
description: "Create a new tab",
category: "tabs",
args: import_zodBundle.z.object({
url: import_zodBundle.z.string().optional().describe("The URL to navigate to in the new tab. If omitted, the new tab will be blank.")
}),
toolName: "browser_tabs",
toolParams: ({ url }) => ({ action: "new", url })
});
const tabClose = (0, import_command.declareCommand)({
name: "tab-close",
description: "Close a browser tab",
category: "tabs",
args: import_zodBundle.z.object({
index: numberArg.optional().describe("Tab index. If omitted, current tab is closed.")
}),
toolName: "browser_tabs",
toolParams: ({ index }) => ({ action: "close", index })
});
const tabSelect = (0, import_command.declareCommand)({
name: "tab-select",
description: "Select a browser tab",
category: "tabs",
args: import_zodBundle.z.object({
index: numberArg.describe("Tab index")
}),
toolName: "browser_tabs",
toolParams: ({ index }) => ({ action: "select", index })
});
const stateLoad = (0, import_command.declareCommand)({
name: "state-load",
description: "Loads browser storage (authentication) state from a file",
category: "storage",
args: import_zodBundle.z.object({
filename: import_zodBundle.z.string().describe("File name to load the storage state from.")
}),
toolName: "browser_set_storage_state",
toolParams: ({ filename }) => ({ filename })
});
const stateSave = (0, import_command.declareCommand)({
name: "state-save",
description: "Saves the current storage (authentication) state to a file",
category: "storage",
args: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("File name to save the storage state to.")
}),
toolName: "browser_storage_state",
toolParams: ({ filename }) => ({ filename })
});
const cookieList = (0, import_command.declareCommand)({
name: "cookie-list",
description: "List all cookies (optionally filtered by domain/path)",
category: "storage",
args: import_zodBundle.z.object({}),
options: import_zodBundle.z.object({
domain: import_zodBundle.z.string().optional().describe("Filter cookies by domain"),
path: import_zodBundle.z.string().optional().describe("Filter cookies by path")
}),
toolName: "browser_cookie_list",
toolParams: ({ domain, path }) => ({ domain, path })
});
const cookieGet = (0, import_command.declareCommand)({
name: "cookie-get",
description: "Get a specific cookie by name",
category: "storage",
args: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Cookie name")
}),
toolName: "browser_cookie_get",
toolParams: ({ name }) => ({ name })
});
const cookieSet = (0, import_command.declareCommand)({
name: "cookie-set",
description: "Set a cookie with optional flags",
category: "storage",
args: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Cookie name"),
value: import_zodBundle.z.string().describe("Cookie value")
}),
options: import_zodBundle.z.object({
domain: import_zodBundle.z.string().optional().describe("Cookie domain"),
path: import_zodBundle.z.string().optional().describe("Cookie path"),
expires: numberArg.optional().describe("Cookie expiration as Unix timestamp"),
httpOnly: import_zodBundle.z.boolean().optional().describe("Whether the cookie is HTTP only"),
secure: import_zodBundle.z.boolean().optional().describe("Whether the cookie is secure"),
sameSite: import_zodBundle.z.enum(["Strict", "Lax", "None"]).optional().describe("Cookie SameSite attribute")
}),
toolName: "browser_cookie_set",
toolParams: ({ name, value, domain, path, expires, httpOnly, secure, sameSite }) => ({ name, value, domain, path, expires, httpOnly, secure, sameSite })
});
const cookieDelete = (0, import_command.declareCommand)({
name: "cookie-delete",
description: "Delete a specific cookie",
category: "storage",
args: import_zodBundle.z.object({
name: import_zodBundle.z.string().describe("Cookie name")
}),
toolName: "browser_cookie_delete",
toolParams: ({ name }) => ({ name })
});
const cookieClear = (0, import_command.declareCommand)({
name: "cookie-clear",
description: "Clear all cookies",
category: "storage",
args: import_zodBundle.z.object({}),
toolName: "browser_cookie_clear",
toolParams: () => ({})
});
const localStorageList = (0, import_command.declareCommand)({
name: "localstorage-list",
description: "List all localStorage key-value pairs",
category: "storage",
args: import_zodBundle.z.object({}),
toolName: "browser_localstorage_list",
toolParams: () => ({})
});
const localStorageGet = (0, import_command.declareCommand)({
name: "localstorage-get",
description: "Get a localStorage item by key",
category: "storage",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to get")
}),
toolName: "browser_localstorage_get",
toolParams: ({ key }) => ({ key })
});
const localStorageSet = (0, import_command.declareCommand)({
name: "localstorage-set",
description: "Set a localStorage item",
category: "storage",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to set"),
value: import_zodBundle.z.string().describe("Value to set")
}),
toolName: "browser_localstorage_set",
toolParams: ({ key, value }) => ({ key, value })
});
const localStorageDelete = (0, import_command.declareCommand)({
name: "localstorage-delete",
description: "Delete a localStorage item",
category: "storage",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to delete")
}),
toolName: "browser_localstorage_delete",
toolParams: ({ key }) => ({ key })
});
const localStorageClear = (0, import_command.declareCommand)({
name: "localstorage-clear",
description: "Clear all localStorage",
category: "storage",
args: import_zodBundle.z.object({}),
toolName: "browser_localstorage_clear",
toolParams: () => ({})
});
const sessionStorageList = (0, import_command.declareCommand)({
name: "sessionstorage-list",
description: "List all sessionStorage key-value pairs",
category: "storage",
args: import_zodBundle.z.object({}),
toolName: "browser_sessionstorage_list",
toolParams: () => ({})
});
const sessionStorageGet = (0, import_command.declareCommand)({
name: "sessionstorage-get",
description: "Get a sessionStorage item by key",
category: "storage",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to get")
}),
toolName: "browser_sessionstorage_get",
toolParams: ({ key }) => ({ key })
});
const sessionStorageSet = (0, import_command.declareCommand)({
name: "sessionstorage-set",
description: "Set a sessionStorage item",
category: "storage",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to set"),
value: import_zodBundle.z.string().describe("Value to set")
}),
toolName: "browser_sessionstorage_set",
toolParams: ({ key, value }) => ({ key, value })
});
const sessionStorageDelete = (0, import_command.declareCommand)({
name: "sessionstorage-delete",
description: "Delete a sessionStorage item",
category: "storage",
args: import_zodBundle.z.object({
key: import_zodBundle.z.string().describe("Key to delete")
}),
toolName: "browser_sessionstorage_delete",
toolParams: ({ key }) => ({ key })
});
const sessionStorageClear = (0, import_command.declareCommand)({
name: "sessionstorage-clear",
description: "Clear all sessionStorage",
category: "storage",
args: import_zodBundle.z.object({}),
toolName: "browser_sessionstorage_clear",
toolParams: () => ({})
});
const routeMock = (0, import_command.declareCommand)({
name: "route",
description: "Mock network requests matching a URL pattern",
category: "network",
args: import_zodBundle.z.object({
pattern: import_zodBundle.z.string().describe('URL pattern to match (e.g., "**/api/users")')
}),
options: import_zodBundle.z.object({
status: numberArg.optional().describe("HTTP status code (default: 200)"),
body: import_zodBundle.z.string().optional().describe("Response body (text or JSON string)"),
["content-type"]: import_zodBundle.z.string().optional().describe("Content-Type header"),
header: import_zodBundle.z.union([import_zodBundle.z.string(), import_zodBundle.z.array(import_zodBundle.z.string())]).optional().transform((v) => v ? Array.isArray(v) ? v : [v] : void 0).describe('Header to add in "Name: Value" format (repeatable)'),
["remove-header"]: import_zodBundle.z.string().optional().describe("Comma-separated header names to remove")
}),
toolName: "browser_route",
toolParams: ({ pattern, status, body, ["content-type"]: contentType, header: headers, ["remove-header"]: removeHeaders }) => ({
pattern,
status,
body,
contentType,
headers,
removeHeaders
})
});
const routeList = (0, import_command.declareCommand)({
name: "route-list",
description: "List all active network routes",
category: "network",
args: import_zodBundle.z.object({}),
toolName: "browser_route_list",
toolParams: () => ({})
});
const unroute = (0, import_command.declareCommand)({
name: "unroute",
description: "Remove routes matching a pattern (or all routes)",
category: "network",
args: import_zodBundle.z.object({
pattern: import_zodBundle.z.string().optional().describe("URL pattern to unroute (omit to remove all)")
}),
toolName: "browser_unroute",
toolParams: ({ pattern }) => ({ pattern })
});
const networkStateSet = (0, import_command.declareCommand)({
name: "network-state-set",
description: "Set the browser network state to online or offline",
category: "network",
args: import_zodBundle.z.object({
state: import_zodBundle.z.enum(["online", "offline"]).describe('Set to "offline" to simulate offline mode, "online" to restore network connectivity')
}),
toolName: "browser_network_state_set",
toolParams: ({ state }) => ({ state })
});
const screenshot = (0, import_command.declareCommand)({
name: "screenshot",
description: "screenshot of the current page or element",
category: "export",
args: import_zodBundle.z.object({
target: import_zodBundle.z.string().optional().describe("Exact target element reference from the page snapshot, or a unique element selector.")
}),
options: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("File name to save the screenshot to. Defaults to `page-{timestamp}.{png|jpeg}` if not specified."),
["full-page"]: import_zodBundle.z.boolean().optional().describe("When true, takes a screenshot of the full scrollable page, instead of the currently visible viewport.")
}),
toolName: "browser_take_screenshot",
toolParams: ({ target, filename, ["full-page"]: fullPage }) => ({ filename, ...asRef(target), fullPage })
});
const pdfSave = (0, import_command.declareCommand)({
name: "pdf",
description: "Save page as PDF",
category: "export",
args: import_zodBundle.z.object({}),
options: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("File name to save the pdf to. Defaults to `page-{timestamp}.pdf` if not specified.")
}),
toolName: "browser_pdf_save",
toolParams: ({ filename }) => ({ filename })
});
const consoleList = (0, import_command.declareCommand)({
name: "console",
description: "List console messages",
category: "devtools",
args: import_zodBundle.z.object({
["min-level"]: import_zodBundle.z.string().optional().describe('Level of the console messages to return. Each level includes the messages of more severe levels. Defaults to "info".')
}),
options: import_zodBundle.z.object({
clear: import_zodBundle.z.boolean().optional().describe("Whether to clear the console list")
}),
toolName: ({ clear }) => clear ? "browser_console_clear" : "browser_console_messages",
toolParams: ({ ["min-level"]: level, clear }) => clear ? {} : { level }
});
const networkRequests = (0, import_command.declareCommand)({
name: "network",
description: "List all network requests since loading the page",
category: "devtools",
args: import_zodBundle.z.object({}),
options: import_zodBundle.z.object({
static: import_zodBundle.z.boolean().optional().describe("Whether to include successful static resources like images, fonts, scripts, etc. Defaults to false."),
["request-body"]: import_zodBundle.z.boolean().optional().describe("Whether to include request body. Defaults to false."),
["request-headers"]: import_zodBundle.z.boolean().optional().describe("Whether to include request headers. Defaults to false."),
filter: import_zodBundle.z.string().optional().describe('Only return requests whose URL matches this regexp (e.g. "/api/.*user").'),
clear: import_zodBundle.z.boolean().optional().describe("Whether to clear the network list")
}),
toolName: ({ clear }) => clear ? "browser_network_clear" : "browser_network_requests",
toolParams: ({ static: s, "request-body": requestBody, "request-headers": requestHeaders, filter, clear }) => clear ? {} : { static: s, requestBody, requestHeaders, filter }
});
const tracingStart = (0, import_command.declareCommand)({
name: "tracing-start",
description: "Start trace recording",
category: "devtools",
args: import_zodBundle.z.object({}),
toolName: "browser_start_tracing",
toolParams: () => ({})
});
const tracingStop = (0, import_command.declareCommand)({
name: "tracing-stop",
description: "Stop trace recording",
category: "devtools",
args: import_zodBundle.z.object({}),
toolName: "browser_stop_tracing",
toolParams: () => ({})
});
const videoStart = (0, import_command.declareCommand)({
name: "video-start",
description: "Start video recording",
category: "devtools",
args: import_zodBundle.z.object({
filename: import_zodBundle.z.string().optional().describe("Filename to save the video.")
}),
options: import_zodBundle.z.object({
size: import_zodBundle.z.string().optional().describe('Video frame size, e.g. "800x600". If not specified, the size of the recorded video will fit 800x800.')
}),
toolName: "browser_start_video",
toolParams: ({ filename, size }) => {
const parsedSize = size ? size.split("x").map(Number) : void 0;
return { filename, size: parsedSize ? { width: parsedSize[0], height: parsedSize[1] } : void 0 };
}
});
const videoStop = (0, import_command.declareCommand)({
name: "video-stop",
description: "Stop video recording",
category: "devtools",
toolName: "browser_stop_video",
toolParams: () => ({})
});
const videoChapter = (0, import_command.declareCommand)({
name: "video-chapter",
description: "Add a chapter marker to the video recording",
category: "devtools",
args: import_zodBundle.z.object({
title: import_zodBundle.z.string().describe("Chapter title.")
}),
options: import_zodBundle.z.object({
description: import_zodBundle.z.string().optional().describe("Chapter description."),
duration: numberArg.optional().describe("Duration in milliseconds to show the chapter card.")
}),
toolName: "browser_video_chapter",
toolParams: ({ title, description, duration }) => ({ title, description, duration })
});
const devtoolsShow = (0, import_command.declareCommand)({
name: "show",
description: "Show browser DevTools",
category: "devtools",
args: import_zodBundle.z.object({}),
toolName: "",
toolParams: () => ({})
});
const resume = (0, import_command.declareCommand)({
name: "resume",
description: "Resume the test execution",
category: "devtools",
args: import_zodBundle.z.object({}),
toolName: "browser_resume",
toolParams: ({ step }) => ({ step })
});
const stepOver = (0, import_command.declareCommand)({
name: "step-over",
description: "Step over the next call in the test",
category: "devtools",
args: import_zodBundle.z.object({}),
toolName: "browser_resume",
toolParams: ({}) => ({ step: true })
});
const pauseAt = (0, import_command.declareCommand)({
name: "pause-at",
description: "Run the test up to a specific location and pause there",
category: "devtools",
args: import_zodBundle.z.object({
location: import_zodBundle.z.string().describe('Location to pause at. Format is <file>:<line>, e.g. "example.spec.ts:42".')
}),
toolName: "browser_resume",
toolParams: ({ location }) => ({ location })
});
const sessionList = (0, import_command.declareCommand)({
name: "list",
description: "List browser sessions",
category: "browsers",
args: import_zodBundle.z.object({}),
options: import_zodBundle.z.object({
all: import_zodBundle.z.boolean().optional().describe("List all browser sessions across all workspaces")
}),
toolName: "",
toolParams: () => ({})
});
const sessionCloseAll = (0, import_command.declareCommand)({
name: "close-all",
description: "Close all browser sessions",
category: "browsers",
toolName: "",
toolParams: () => ({})
});
const killAll = (0, import_command.declareCommand)({
name: "kill-all",
description: "Forcefully kill all browser sessions (for stale/zombie processes)",
category: "browsers",
toolName: "",
toolParams: () => ({})
});
const deleteData = (0, import_command.declareCommand)({
name: "delete-data",
description: "Delete session data",
category: "core",
toolName: "",
toolParams: () => ({})
});
const configPrint = (0, import_command.declareCommand)({
name: "config-print",
description: "Print the final resolved config after merging CLI options, environment variables and config file.",
category: "config",
hidden: true,
toolName: "browser_get_config",
toolParams: () => ({})
});
const install = (0, import_command.declareCommand)({
name: "install",
description: "Initialize workspace",
category: "install",
args: import_zodBundle.z.object({}),
options: import_zodBundle.z.object({
skills: import_zodBundle.z.string().optional().describe('Install skills to ".claude" (default) or ".agents" dir')
}),
toolName: "",
toolParams: () => ({})
});
const installBrowser = (0, import_command.declareCommand)({
name: "install-browser",
description: "Install browser",
category: "install",
args: import_zodBundle.z.object({
browser: import_zodBundle.z.string().optional().describe("Browser to install")
}),
options: import_zodBundle.z.object({
["with-deps"]: import_zodBundle.z.boolean().optional().describe("Install system dependencies for browsers"),
["dry-run"]: import_zodBundle.z.boolean().optional().describe("Do not execute installation, only print information"),
list: import_zodBundle.z.boolean().optional().describe("Prints list of browsers from all Playwright installations"),
force: import_zodBundle.z.boolean().optional().describe("Force reinstall of already installed browsers"),
["only-shell"]: import_zodBundle.z.boolean().optional().describe("Only install headless shell when installing Chromium"),
["no-shell"]: import_zodBundle.z.boolean().optional().describe("Do not install Chromium headless shell")
}),
toolName: "",
toolParams: () => ({})
});
const tray = (0, import_command.declareCommand)({
name: "tray",
description: "Run tray",
category: "config",
hidden: true,
toolName: "",
toolParams: () => ({})
});
const commandsArray = [
// core category
open,
attach,
close,
goto,
type,
click,
doubleClick,
fill,
drag,
hover,
select,
fileUpload,
check,
uncheck,
snapshot,
evaluate,
consoleList,
dialogAccept,
dialogDismiss,
resize,
runCode,
deleteData,
// navigation category
goBack,
goForward,
reload,
// keyboard category
pressKey,
keydown,
keyup,
// mouse category
mouseMove,
mouseDown,
mouseUp,
mouseWheel,
// export category
screenshot,
pdfSave,
// tabs category
tabList,
tabNew,
tabClose,
tabSelect,
// storage category
stateLoad,
stateSave,
cookieList,
cookieGet,
cookieSet,
cookieDelete,
cookieClear,
localStorageList,
localStorageGet,
localStorageSet,
localStorageDelete,
localStorageClear,
sessionStorageList,
sessionStorageGet,
sessionStorageSet,
sessionStorageDelete,
sessionStorageClear,
// network category
routeMock,
routeList,
unroute,
networkStateSet,
// config category
configPrint,
// install category
install,
installBrowser,
// devtools category
networkRequests,
tracingStart,
tracingStop,
videoStart,
videoStop,
videoChapter,
devtoolsShow,
pauseAt,
resume,
stepOver,
// session category
sessionList,
sessionCloseAll,
killAll,
// Hidden commands
tray
];
const commands = Object.fromEntries(commandsArray.map((cmd) => [cmd.name, cmd]));
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
commands
});
+157
View File
@@ -0,0 +1,157 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var daemon_exports = {};
__export(daemon_exports, {
startCliDaemonServer: () => startCliDaemonServer
});
module.exports = __toCommonJS(daemon_exports);
var import_fs = __toESM(require("fs"));
var import_net = __toESM(require("net"));
var import_path = __toESM(require("path"));
var import_network = require("../../server/utils/network");
var import_fileUtils = require("../../server/utils/fileUtils");
var import_processLauncher = require("../../server/utils/processLauncher");
var import_browserBackend = require("../backend/browserBackend");
var import_tools = require("../backend/tools");
var import_command = require("./command");
var import_commands = require("./commands");
var import_socketConnection = require("../utils/socketConnection");
var import_registry = require("../cli-client/registry");
async function socketExists(socketPath) {
try {
const stat = await import_fs.default.promises.stat(socketPath);
if (stat?.isSocket())
return true;
} catch (e) {
}
return false;
}
async function startCliDaemonServer(sessionName, browserContext, browserInfo, contextConfig = {}, clientInfo = (0, import_registry.createClientInfo)(), options) {
const sessionConfig = createSessionConfig(clientInfo, sessionName, browserInfo, options);
const { socketPath } = sessionConfig;
if (process.platform !== "win32" && await socketExists(socketPath)) {
try {
await import_fs.default.promises.unlink(socketPath);
} catch (error) {
throw error;
}
}
const backend = new import_browserBackend.BrowserBackend(contextConfig, browserContext, import_tools.browserTools);
await backend.initialize({ cwd: process.cwd() });
if (browserContext.isClosed())
throw new Error("Browser context was closed before the daemon could start");
const server = import_net.default.createServer((socket) => {
const connection = new import_socketConnection.SocketConnection(socket);
connection.onmessage = async (message) => {
const { id, method, params } = message;
try {
if (method === "stop") {
await deleteSessionFile(clientInfo, sessionConfig);
const sendAck = async () => connection.send({ id, result: "ok" }).catch(() => {
});
if (options?.exitOnClose)
(0, import_processLauncher.gracefullyProcessExitDoNotHang)(0, () => sendAck());
else
await sendAck();
} else if (method === "run") {
const { toolName, toolParams } = parseCliCommand(params.args);
if (params.cwd)
toolParams._meta = { cwd: params.cwd };
const response = await backend.callTool(toolName, toolParams);
await connection.send({ id, result: formatResult(response) });
} else {
throw new Error(`Unknown method: ${method}`);
}
} catch (e) {
const error = process.env.PWDEBUGIMPL ? e.stack || e.message : e.message;
connection.send({ id, error }).catch(() => {
});
}
};
});
(0, import_network.decorateServer)(server);
browserContext.on("close", () => Promise.resolve().then(async () => {
await deleteSessionFile(clientInfo, sessionConfig);
if (options?.exitOnClose)
(0, import_processLauncher.gracefullyProcessExitDoNotHang)(0);
}));
await new Promise((resolve, reject) => {
server.on("error", reject);
server.listen(socketPath, () => resolve());
});
await saveSessionFile(clientInfo, sessionConfig);
return socketPath;
}
async function saveSessionFile(clientInfo, sessionConfig) {
await import_fs.default.promises.mkdir(clientInfo.daemonProfilesDir, { recursive: true });
const sessionFile = import_path.default.join(clientInfo.daemonProfilesDir, `${sessionConfig.name}.session`);
await import_fs.default.promises.writeFile(sessionFile, JSON.stringify(sessionConfig, null, 2));
}
async function deleteSessionFile(clientInfo, sessionConfig) {
await import_fs.default.promises.unlink(sessionConfig.socketPath).catch(() => {
});
if (!sessionConfig.cli.persistent) {
const sessionFile = import_path.default.join(clientInfo.daemonProfilesDir, `${sessionConfig.name}.session`);
await import_fs.default.promises.rm(sessionFile).catch(() => {
});
}
}
function formatResult(result) {
const isError = result.isError;
const text = result.content[0].type === "text" ? result.content[0].text : void 0;
return { isError, text };
}
function parseCliCommand(args) {
const command = import_commands.commands[args._[0]];
if (!command)
throw new Error("Command is required");
return (0, import_command.parseCommand)(command, args);
}
function daemonSocketPath(clientInfo, sessionName) {
return (0, import_fileUtils.makeSocketPath)("cli", `${clientInfo.workspaceDirHash}-${sessionName}`);
}
function createSessionConfig(clientInfo, sessionName, browserInfo, options = {}) {
return {
name: sessionName,
version: clientInfo.version,
timestamp: Date.now(),
socketPath: daemonSocketPath(clientInfo, sessionName),
workspaceDir: clientInfo.workspaceDir,
cli: { persistent: options.persistent },
browser: {
browserName: browserInfo.browserName,
launchOptions: browserInfo.launchOptions,
userDataDir: browserInfo.userDataDir
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
startCliDaemonServer
});
+177
View File
@@ -0,0 +1,177 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var helpGenerator_exports = {};
__export(helpGenerator_exports, {
generateHelp: () => generateHelp,
generateHelpJSON: () => generateHelpJSON,
generateReadme: () => generateReadme
});
module.exports = __toCommonJS(helpGenerator_exports);
var import_zodBundle = require("../../zodBundle");
var import_commands = require("./commands");
function commandArgs(command) {
const args = [];
const shape = command.args ? command.args.shape : {};
for (const [name, schema] of Object.entries(shape)) {
const zodSchema = schema;
const description = zodSchema.description ?? "";
args.push({ name, description, optional: zodSchema.safeParse(void 0).success });
}
return args;
}
function commandArgsText(args) {
return args.map((a) => a.optional ? `[${a.name}]` : `<${a.name}>`).join(" ");
}
function generateCommandHelp(command) {
const args = commandArgs(command);
const lines = [
`playwright-cli ${command.name} ${commandArgsText(args)}`,
"",
command.description,
""
];
if (args.length) {
lines.push("Arguments:");
lines.push(...args.map((a) => formatWithGap(` ${a.optional ? `[${a.name}]` : `<${a.name}>`}`, a.description.toLowerCase())));
}
if (command.options) {
lines.push("Options:");
const optionsShape = command.options.shape;
for (const [name, schema] of Object.entries(optionsShape)) {
const zodSchema = schema;
const description = (zodSchema.description ?? "").toLowerCase();
lines.push(formatWithGap(` --${name}`, description));
}
}
return lines.join("\n");
}
const categories = [
{ name: "core", title: "Core" },
{ name: "navigation", title: "Navigation" },
{ name: "keyboard", title: "Keyboard" },
{ name: "mouse", title: "Mouse" },
{ name: "export", title: "Save as" },
{ name: "tabs", title: "Tabs" },
{ name: "storage", title: "Storage" },
{ name: "network", title: "Network" },
{ name: "devtools", title: "DevTools" },
{ name: "install", title: "Install" },
{ name: "config", title: "Configuration" },
{ name: "browsers", title: "Browser sessions" }
];
function generateHelp() {
const lines = [];
lines.push("Usage: playwright-cli <command> [args] [options]");
lines.push("Usage: playwright-cli -s=<session> <command> [args] [options]");
const commandsByCategory = /* @__PURE__ */ new Map();
for (const c of categories)
commandsByCategory.set(c.name, []);
for (const command of Object.values(import_commands.commands)) {
if (command.hidden)
continue;
commandsByCategory.get(command.category).push(command);
}
for (const c of categories) {
const cc = commandsByCategory.get(c.name);
if (!cc.length)
continue;
lines.push(`
${c.title}:`);
for (const command of cc)
lines.push(generateHelpEntry(command));
}
lines.push("\nGlobal options:");
lines.push(formatWithGap(" --help [command]", "print help"));
lines.push(formatWithGap(" --version", "print version"));
return lines.join("\n");
}
function generateReadme() {
const lines = [];
lines.push("\n## Commands");
const commandsByCategory = /* @__PURE__ */ new Map();
for (const c of categories)
commandsByCategory.set(c.name, []);
for (const command of Object.values(import_commands.commands))
commandsByCategory.get(command.category).push(command);
for (const c of categories) {
const cc = commandsByCategory.get(c.name);
if (!cc.length)
continue;
lines.push(`
### ${c.title}
`);
lines.push("```bash");
for (const command of cc)
lines.push(generateReadmeEntry(command));
lines.push("```");
}
return lines.join("\n");
}
function generateHelpEntry(command) {
const args = commandArgs(command);
const prefix = ` ${command.name} ${commandArgsText(args)}`;
const suffix = command.description.toLowerCase();
return formatWithGap(prefix, suffix);
}
function generateReadmeEntry(command) {
const args = commandArgs(command);
const prefix = `playwright-cli ${command.name} ${commandArgsText(args)}`;
const suffix = "# " + command.description.toLowerCase();
return formatWithGap(prefix, suffix, 40);
}
function unwrapZodType(schema) {
if ("unwrap" in schema && typeof schema.unwrap === "function")
return unwrapZodType(schema.unwrap());
return schema;
}
function isBooleanSchema(schema) {
return unwrapZodType(schema) instanceof import_zodBundle.z.ZodBoolean;
}
function generateHelpJSON() {
const booleanOptions = /* @__PURE__ */ new Set();
const commandEntries = {};
for (const [name, command] of Object.entries(import_commands.commands)) {
const flags = {};
if (command.options) {
const optionsShape = command.options.shape;
for (const [flagName, schema] of Object.entries(optionsShape)) {
const isBoolean = isBooleanSchema(schema);
flags[flagName] = isBoolean ? "boolean" : "string";
if (isBoolean)
booleanOptions.add(flagName);
}
}
commandEntries[name] = { help: generateCommandHelp(command), flags };
}
return {
global: generateHelp(),
commands: commandEntries,
booleanOptions: [...booleanOptions]
};
}
function formatWithGap(prefix, text, threshold = 30) {
const indent = Math.max(1, threshold - prefix.length);
return prefix + " ".repeat(indent) + text;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
generateHelp,
generateHelpJSON,
generateReadme
});
+129
View File
@@ -0,0 +1,129 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var import_fs = __toESM(require("fs"));
var import_os = __toESM(require("os"));
var import_path = __toESM(require("path"));
var import_daemon = require("./daemon");
var import_watchdog = require("../mcp/watchdog");
var import_browserFactory = require("../mcp/browserFactory");
var configUtils = __toESM(require("../mcp/config"));
var import_registry = require("../cli-client/registry");
var import_utilsBundle = require("../../utilsBundle");
var import_registry2 = require("../../server/registry/index");
import_utilsBundle.program.argument("[session-name]", "name of the session to create or connect to", "default").option("--headed", "run in headed mode (non-headless)").option("--extension", "run with the extension").option("--browser <name>", "browser to use (chromium, chrome, firefox, webkit)").option("--persistent", "use a persistent browser context").option("--profile <path>", "path to the user data dir").option("--config <path>", "path to the config file; by default uses .playwright/cli.config.json in the project directory and ~/.playwright/cli.config.json as global config").option("--endpoint <endpoint>", "attach to a running Playwright browser endpoint").option("--init-workspace", "initialize workspace").option("--init-skills <value>", 'install skills for the given agent type ("claude" or "agents")').action(async (sessionName, options) => {
if (options.initWorkspace) {
await initWorkspace(options.initSkills);
return;
}
(0, import_watchdog.setupExitWatchdog)();
const clientInfo = (0, import_registry.createClientInfo)();
const mcpConfig = await configUtils.resolveCLIConfigForCLI(clientInfo.daemonProfilesDir, sessionName, options);
const clientInfoEx = {
cwd: process.cwd(),
sessionName,
workspaceDir: clientInfo.workspaceDir
};
try {
const { browser, browserInfo } = await (0, import_browserFactory.createBrowserWithInfo)(mcpConfig, clientInfoEx);
const browserContext = mcpConfig.browser.isolated ? await browser.newContext(mcpConfig.browser.contextOptions) : browser.contexts()[0];
if (!browserContext)
throw new Error("Error: unable to connect to a browser that does not have any contexts");
const persistent = options.persistent || options.profile || mcpConfig.browser.userDataDir ? true : void 0;
const socketPath = await (0, import_daemon.startCliDaemonServer)(sessionName, browserContext, browserInfo, mcpConfig, clientInfo, { persistent, exitOnClose: true });
console.log(`### Success
Daemon listening on ${socketPath}`);
console.log("<EOF>");
} catch (error) {
const message = process.env.PWDEBUGIMPL ? error.stack || error.message : error.message;
console.log(`### Error
${message}`);
console.log("<EOF>");
}
});
void import_utilsBundle.program.parseAsync();
function defaultConfigFile() {
return import_path.default.resolve(".playwright", "cli.config.json");
}
function globalConfigFile() {
return import_path.default.join(process.env["PWTEST_CLI_GLOBAL_CONFIG"] ?? import_os.default.homedir(), ".playwright", "cli.config.json");
}
async function initWorkspace(initSkills) {
const cwd = process.cwd();
const playwrightDir = import_path.default.join(cwd, ".playwright");
await import_fs.default.promises.mkdir(playwrightDir, { recursive: true });
console.log(`\u2705 Workspace initialized at \`${cwd}\`.`);
if (initSkills) {
const skillSourceDir = import_path.default.join(__dirname, "../cli-client/skill");
const target = initSkills === "agents" ? "agents" : "claude";
const skillDestDir = import_path.default.join(cwd, `.${target}`, "skills", "playwright-cli");
if (!import_fs.default.existsSync(skillSourceDir)) {
console.error("\u274C Skills source directory not found:", skillSourceDir);
process.exit(1);
}
await import_fs.default.promises.cp(skillSourceDir, skillDestDir, { recursive: true });
console.log(`\u2705 Skills installed to \`${import_path.default.relative(cwd, skillDestDir)}\`.`);
}
await ensureConfiguredBrowserInstalled();
}
async function ensureConfiguredBrowserInstalled() {
if (import_fs.default.existsSync(defaultConfigFile()) || import_fs.default.existsSync(globalConfigFile())) {
const clientInfo = (0, import_registry.createClientInfo)();
const config = await configUtils.resolveCLIConfigForCLI(clientInfo.daemonProfilesDir, "default", {});
const browserName = config.browser.browserName;
const channel = config.browser.launchOptions.channel;
if (!channel || channel.startsWith("chromium")) {
const executable = import_registry2.registry.findExecutable(channel ?? browserName);
if (executable && !import_fs.default.existsSync(executable.executablePath()))
await import_registry2.registry.install([executable]);
}
} else {
const channel = await findOrInstallDefaultBrowser();
if (channel !== "chrome")
await createDefaultConfig(channel);
}
}
async function findOrInstallDefaultBrowser() {
const channels = ["chrome", "msedge"];
for (const channel of channels) {
const executable = import_registry2.registry.findExecutable(channel);
if (!executable?.executablePath())
continue;
console.log(`\u2705 Found ${channel}, will use it as the default browser.`);
return channel;
}
const chromiumExecutable = import_registry2.registry.findExecutable("chromium");
if (!import_fs.default.existsSync(chromiumExecutable?.executablePath()))
await import_registry2.registry.install([chromiumExecutable]);
return "chromium";
}
async function createDefaultConfig(channel) {
const config = {
browser: {
browserName: "chromium",
launchOptions: { channel }
}
};
await import_fs.default.promises.writeFile(defaultConfigFile(), JSON.stringify(config, null, 2));
console.log(`\u2705 Created default config for ${channel} at ${import_path.default.relative(process.cwd(), defaultConfigFile())}.`);
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

+284
View File
@@ -0,0 +1,284 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var dashboardApp_exports = {};
__export(dashboardApp_exports, {
syncLocalStorageWithSettings: () => syncLocalStorageWithSettings
});
module.exports = __toCommonJS(dashboardApp_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_net = __toESM(require("net"));
var import__ = require("../../..");
var import_httpServer = require("../../server/utils/httpServer");
var import_fileUtils = require("../../server/utils/fileUtils");
var import_processLauncher = require("../../server/utils/processLauncher");
var import_registry = require("../../server/registry/index");
var import_dashboardController = require("./dashboardController");
var import_serverRegistry = require("../../serverRegistry");
var import_connect = require("../utils/connect");
function readBody(request) {
return new Promise((resolve, reject) => {
const chunks = [];
request.on("data", (chunk) => chunks.push(chunk));
request.on("end", () => {
try {
const text = Buffer.concat(chunks).toString();
resolve(text ? JSON.parse(text) : {});
} catch (e) {
reject(e);
}
});
request.on("error", reject);
});
}
async function parseRequest(request) {
const body = await readBody(request);
if (!body.guid)
throw new Error("Dashboard app is too old, please close it and open again");
return { guid: body.guid };
}
function sendJSON(response, data, statusCode = 200) {
response.statusCode = statusCode;
response.setHeader("Content-Type", "application/json");
response.end(JSON.stringify(data));
}
async function loadBrowserDescriptorSessions(wsPath) {
const entriesByWorkspace = await import_serverRegistry.serverRegistry.list();
const sessions = [];
for (const [, entries] of entriesByWorkspace) {
for (const entry of entries) {
let wsUrl;
if (entry.canConnect) {
const url = new URL(wsPath, "http://localhost");
url.searchParams.set("guid", entry.browser.guid);
wsUrl = url.pathname + url.search;
}
sessions.push({ ...entry, wsUrl });
}
}
return sessions;
}
const browserGuidToDashboardConnection = /* @__PURE__ */ new Map();
async function handleApiRequest(httpServer, request, response) {
const url = new URL(request.url, httpServer.urlPrefix("human-readable"));
const apiPath = url.pathname;
if (apiPath === "/api/sessions/list" && request.method === "GET") {
const sessions = await loadBrowserDescriptorSessions(httpServer.wsGuid());
sendJSON(response, { sessions });
return;
}
if (apiPath === "/api/sessions/close" && request.method === "POST") {
const { guid } = await parseRequest(request);
let browser;
try {
const browserDescriptor = import_serverRegistry.serverRegistry.readDescriptor(guid);
browser = await (0, import_connect.connectToBrowserAcrossVersions)(browserDescriptor);
} catch (e) {
sendJSON(response, { error: "Failed to connect to browser socket: " + e.message }, 500);
return;
}
try {
await Promise.all(browser.contexts().map((context) => context.close()));
await browser.close();
sendJSON(response, { success: true });
return;
} catch (e) {
sendJSON(response, { error: "Failed to close browser: " + e.message }, 500);
return;
}
}
if (apiPath === "/api/sessions/delete-data" && request.method === "POST") {
const { guid } = await parseRequest(request);
try {
await import_serverRegistry.serverRegistry.deleteUserData(guid);
} catch (e) {
sendJSON(response, { error: "Failed to delete session data: " + e.message }, 500);
return;
}
sendJSON(response, { success: true });
return;
}
response.statusCode = 404;
response.end(JSON.stringify({ error: "Not found" }));
}
async function openDashboardApp() {
const httpServer = new import_httpServer.HttpServer();
const libDir = require.resolve("playwright-core/package.json");
const dashboardDir = import_path.default.join(import_path.default.dirname(libDir), "lib/vite/dashboard");
httpServer.routePrefix("/api/", (request, response) => {
handleApiRequest(httpServer, request, response).catch((e) => {
response.statusCode = 500;
response.end(JSON.stringify({ error: e.message }));
});
return true;
});
httpServer.createWebSocket((url2) => {
const guid = url2.searchParams.get("guid");
if (!guid)
throw new Error("Unsupported WebSocket URL: " + url2.toString());
const browserDescriptor = import_serverRegistry.serverRegistry.readDescriptor(guid);
const cdpPageId = url2.searchParams.get("cdpPageId");
if (cdpPageId) {
const connection2 = browserGuidToDashboardConnection.get(guid);
if (!connection2)
throw new Error("CDP connection not found for session: " + guid);
const page2 = connection2.pageForId(cdpPageId);
if (!page2)
throw new Error("Page not found for page ID: " + cdpPageId);
return new import_dashboardController.CDPConnection(page2);
}
const cdpUrl = new URL(httpServer.urlPrefix("human-readable"));
cdpUrl.pathname = httpServer.wsGuid();
cdpUrl.searchParams.set("guid", guid);
const connection = new import_dashboardController.DashboardConnection(browserDescriptor, cdpUrl, () => browserGuidToDashboardConnection.delete(guid));
browserGuidToDashboardConnection.set(guid, connection);
return connection;
});
httpServer.routePrefix("/", (request, response) => {
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname;
const filePath = pathname === "/" ? "index.html" : pathname.substring(1);
const resolved = import_path.default.join(dashboardDir, filePath);
if (!resolved.startsWith(dashboardDir))
return false;
return httpServer.serveFile(request, response, resolved);
});
await httpServer.start();
const url = httpServer.urlPrefix("human-readable");
const { page } = await launchApp("dashboard");
await page.goto(url);
return page;
}
async function launchApp(appName) {
const channel = (0, import_registry.findChromiumChannelBestEffort)("javascript");
const debugPort = parseInt(process.env.PLAYWRIGHT_DASHBOARD_DEBUG_PORT, 10) || void 0;
const context = await import__.chromium.launchPersistentContext("", {
ignoreDefaultArgs: ["--enable-automation"],
channel,
headless: debugPort !== void 0,
args: [
"--app=data:text/html,",
"--test-type=",
`--window-size=1280,800`,
`--window-position=100,100`,
...debugPort !== void 0 ? [`--remote-debugging-port=${debugPort}`] : []
],
viewport: null
});
const [page] = context.pages();
if (process.platform === "darwin") {
context.on("page", async (newPage) => {
if (newPage.mainFrame().url() === "chrome://new-tab-page/") {
await page.bringToFront();
await newPage.close();
}
});
}
page.on("close", () => {
(0, import_processLauncher.gracefullyProcessExitDoNotHang)(0);
});
const image = await import_fs.default.promises.readFile(import_path.default.join(__dirname, "appIcon.png"));
await page._setDockTile?.(image);
await syncLocalStorageWithSettings(page, appName);
return { context, page };
}
async function syncLocalStorageWithSettings(page, appName) {
const settingsFile = import_path.default.join(import_registry.registryDirectory, ".settings", `${appName}.json`);
await page.exposeBinding("_saveSerializedSettings", (_, settings2) => {
import_fs.default.mkdirSync(import_path.default.dirname(settingsFile), { recursive: true });
import_fs.default.writeFileSync(settingsFile, settings2);
});
const settings = await import_fs.default.promises.readFile(settingsFile, "utf-8").catch(() => "{}");
await page.addInitScript(
`(${String((settings2) => {
if (location && location.protocol === "data:")
return;
if (window.top !== window)
return;
Object.entries(settings2).map(([k, v]) => localStorage[k] = v);
window.saveSettings = () => {
window._saveSerializedSettings(JSON.stringify({ ...localStorage }));
};
})})(${settings});
`
);
}
function dashboardSocketPath() {
return (0, import_fileUtils.makeSocketPath)("dashboard", "app");
}
async function acquireSingleton() {
const socketPath = dashboardSocketPath();
if (process.platform !== "win32")
await import_fs.default.promises.mkdir(import_path.default.dirname(socketPath), { recursive: true });
return await new Promise((resolve, reject) => {
const server = import_net.default.createServer();
server.listen(socketPath, () => resolve(server));
server.on("error", (err) => {
if (err.code !== "EADDRINUSE")
return reject(err);
const client = import_net.default.connect(socketPath, () => {
client.write("bringToFront");
client.end();
reject(new Error("already running"));
});
client.on("error", () => {
if (process.platform !== "win32")
import_fs.default.unlinkSync(socketPath);
server.listen(socketPath, () => resolve(server));
});
});
});
}
async function main() {
let server;
process.on("exit", () => server?.close());
const underTest = !!process.env.PLAYWRIGHT_DASHBOARD_DEBUG_PORT;
if (!underTest) {
try {
server = await acquireSingleton();
} catch {
return;
}
}
const page = await openDashboardApp();
server?.on("connection", (socket) => {
socket.on("data", (data) => {
if (data.toString() === "bringToFront")
page?.bringToFront().catch(() => {
});
});
});
}
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error);
});
void main();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
syncLocalStorageWithSettings
});
+296
View File
@@ -0,0 +1,296 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var dashboardController_exports = {};
__export(dashboardController_exports, {
CDPConnection: () => CDPConnection,
DashboardConnection: () => DashboardConnection
});
module.exports = __toCommonJS(dashboardController_exports);
var import_eventsHelper = require("../../server/utils/eventsHelper");
var import_connect = require("../utils/connect");
class DashboardConnection {
constructor(browserDescriptor, cdpUrl, onclose) {
this.version = 1;
this.selectedPage = null;
this._lastFrameData = null;
this._lastViewportSize = null;
this._pageListeners = [];
this._contextListeners = [];
this._eventListeners = /* @__PURE__ */ new Map();
this._browserDescriptor = browserDescriptor;
this._cdpUrl = cdpUrl;
this._onclose = onclose;
}
on(event, listener) {
let set = this._eventListeners.get(event);
if (!set) {
set = /* @__PURE__ */ new Set();
this._eventListeners.set(event, set);
}
set.add(listener);
}
off(event, listener) {
this._eventListeners.get(event)?.delete(listener);
}
_emit(event, params) {
this.sendEvent?.(event, params);
const set = this._eventListeners.get(event);
if (set) {
for (const fn of set)
fn(params);
}
}
onconnect() {
this._initPromise = this._init();
this._initPromise.catch(() => this.close?.());
}
async _init() {
this._browser = await (0, import_connect.connectToBrowserAcrossVersions)(this._browserDescriptor);
this._context = this._browser.contexts()[0];
this._contextListeners.push(
import_eventsHelper.eventsHelper.addEventListener(this._context, "page", (page) => {
this._sendTabList();
if (!this.selectedPage)
this._selectPage(page);
})
);
const pages = this._context.pages();
if (pages.length > 0)
this._selectPage(pages[0]);
this._sendCachedState();
}
onclose() {
this._deselectPage();
this._contextListeners.forEach((d) => d.dispose());
this._contextListeners = [];
this._onclose();
this._browser?.close().catch(() => {
});
}
async dispatch(method, params) {
await this._initPromise;
return this[method]?.(params);
}
async selectTab(params) {
const page = this._context.pages().find((p) => this._pageId(p) === params.pageId);
if (page)
await this._selectPage(page);
}
async closeTab(params) {
const page = this._context.pages().find((p) => this._pageId(p) === params.pageId);
if (page)
await page.close({ reason: "Closed in Dashboard" });
}
async newTab() {
const page = await this._context.newPage();
await this._selectPage(page);
}
async navigate(params) {
if (!this.selectedPage || !params.url)
return;
const page = this.selectedPage;
await page.goto(params.url);
}
async back() {
await this.selectedPage?.goBack();
}
async forward() {
await this.selectedPage?.goForward();
}
async reload() {
await this.selectedPage?.reload();
}
async mousemove(params) {
await this.selectedPage?.mouse.move(params.x, params.y);
}
async mousedown(params) {
await this.selectedPage?.mouse.move(params.x, params.y);
await this.selectedPage?.mouse.down({ button: params.button || "left" });
}
async mouseup(params) {
await this.selectedPage?.mouse.move(params.x, params.y);
await this.selectedPage?.mouse.up({ button: params.button || "left" });
}
async wheel(params) {
await this.selectedPage?.mouse.wheel(params.deltaX, params.deltaY);
}
async keydown(params) {
await this.selectedPage?.keyboard.down(params.key);
}
async keyup(params) {
await this.selectedPage?.keyboard.up(params.key);
}
async _selectPage(page) {
if (this.selectedPage === page)
return;
if (this.selectedPage) {
this._pageListeners.forEach((d) => d.dispose());
this._pageListeners = [];
await this.selectedPage.screencast.stop();
}
this.selectedPage = page;
this._lastFrameData = null;
this._lastViewportSize = null;
this._sendTabList();
this._pageListeners.push(
import_eventsHelper.eventsHelper.addEventListener(page, "close", () => {
this._deselectPage();
const pages = page.context().pages();
if (pages.length > 0)
this._selectPage(pages[0]);
this._sendTabList();
}),
import_eventsHelper.eventsHelper.addEventListener(page, "framenavigated", (frame) => {
if (frame === page.mainFrame())
this._sendTabList();
})
);
const size = { width: 1280, height: 800 };
await page.screencast.start({
onFrame: ({ data }) => this._writeFrame(data, page.viewportSize()?.width ?? 0, page.viewportSize()?.height ?? 0),
size
});
}
_deselectPage() {
if (!this.selectedPage)
return;
this._pageListeners.forEach((d) => d.dispose());
this._pageListeners = [];
this.selectedPage.screencast.stop().catch(() => {
});
this.selectedPage = null;
this._lastFrameData = null;
this._lastViewportSize = null;
}
async pickLocator() {
if (!this.selectedPage)
return;
const locator = await this.selectedPage.pickLocator();
this._emit("elementPicked", { selector: locator.toString() });
}
async cancelPickLocator() {
await this.selectedPage?.cancelPickLocator();
}
_sendCachedState() {
if (this._lastFrameData && this._lastViewportSize)
this._emit("frame", { data: this._lastFrameData, viewportWidth: this._lastViewportSize.width, viewportHeight: this._lastViewportSize.height });
this._sendTabList();
}
async tabs() {
return { tabs: await this._tabList() };
}
async _tabList() {
const pages = this._context.pages();
if (pages.length === 0)
return [];
const devtoolsUrl = await this._devtoolsUrl(pages[0]);
return await Promise.all(pages.map(async (page) => {
const title = await page.title();
return {
pageId: this._pageId(page),
title,
url: page.url(),
selected: page === this.selectedPage,
inspectorUrl: devtoolsUrl ? await this._pageInspectorUrl(page, devtoolsUrl) : "data:text/plain,Dashboard only supported in Chromium based browsers"
};
}));
}
pageForId(pageId) {
return this._context?.pages().find((p) => this._pageId(p) === pageId);
}
_pageId(p) {
return p._guid;
}
async _devtoolsUrl(page) {
const cdpPort = this._browserDescriptor.browser.launchOptions.cdpPort;
if (cdpPort)
return new URL(`http://localhost:${cdpPort}/devtools/`);
const browserRevision = await getBrowserRevision(page);
if (!browserRevision)
return null;
return new URL(`https://chrome-devtools-frontend.appspot.com/serve_rev/${browserRevision}/`);
}
async _pageInspectorUrl(page, devtoolsUrl) {
const inspector = new URL("./devtools_app.html", devtoolsUrl);
const cdp = new URL(this._cdpUrl);
cdp.searchParams.set("cdpPageId", this._pageId(page));
inspector.searchParams.set("ws", `${cdp.host}${cdp.pathname}${cdp.search}`);
const url = inspector.toString();
return url;
}
_sendTabList() {
this._tabList().then((tabs) => this._emit("tabs", { tabs }));
}
_writeFrame(frame, viewportWidth, viewportHeight) {
const data = frame.toString("base64");
this._lastFrameData = data;
this._lastViewportSize = { width: viewportWidth, height: viewportHeight };
this._emit("frame", { data, viewportWidth, viewportHeight });
}
}
async function getBrowserRevision(page) {
try {
const session = await page.context().newCDPSession(page);
const version = await session.send("Browser.getVersion");
await session.detach();
return version.revision;
} catch (error) {
return null;
}
}
class CDPConnection {
constructor(page) {
this._rawSession = null;
this._rawSessionListeners = [];
this._page = page;
}
onconnect() {
this._initializePromise = this._initializeRawSession();
}
async dispatch(method, params) {
await this._initializePromise;
if (!this._rawSession)
throw new Error("CDP session is not initialized");
return await this._rawSession.send(method, params);
}
onclose() {
this._rawSessionListeners.forEach((listener) => listener.dispose());
this._rawSession?.detach().catch(() => {
});
this._rawSession = null;
this._initializePromise = void 0;
}
async _initializeRawSession() {
const session = await this._page.context().newCDPSession(this._page);
this._rawSession = session;
this._rawSessionListeners = [
import_eventsHelper.eventsHelper.addEventListener(session, "event", ({ method, params }) => {
this.sendEvent?.(method, params);
}),
import_eventsHelper.eventsHelper.addEventListener(session, "close", () => {
this.close?.();
})
];
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CDPConnection,
DashboardConnection
});
+60
View File
@@ -0,0 +1,60 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var exports_exports = {};
__export(exports_exports, {
BrowserBackend: () => import_browserBackend.BrowserBackend,
Tab: () => import_tab.Tab,
browserTools: () => import_tools.browserTools,
createClientInfo: () => import_registry.createClientInfo,
createConnection: () => import_mcp.createConnection,
filteredTools: () => import_tools.filteredTools,
logUnhandledError: () => import_log.logUnhandledError,
parseResponse: () => import_response.parseResponse,
setupExitWatchdog: () => import_watchdog.setupExitWatchdog,
start: () => import_server.start,
startCliDaemonServer: () => import_daemon.startCliDaemonServer,
toMcpTool: () => import_tool.toMcpTool
});
module.exports = __toCommonJS(exports_exports);
var import_registry = require("./cli-client/registry");
var import_daemon = require("./cli-daemon/daemon");
var import_log = require("./mcp/log");
var import_watchdog = require("./mcp/watchdog");
var import_tool = require("./utils/mcp/tool");
var import_browserBackend = require("./backend/browserBackend");
var import_response = require("./backend/response");
var import_tab = require("./backend/tab");
var import_tools = require("./backend/tools");
var import_server = require("./utils/mcp/server");
var import_mcp = require("./mcp/index");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BrowserBackend,
Tab,
browserTools,
createClientInfo,
createConnection,
filteredTools,
logUnhandledError,
parseResponse,
setupExitWatchdog,
start,
startCliDaemonServer,
toMcpTool
});
+233
View File
@@ -0,0 +1,233 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var browserFactory_exports = {};
__export(browserFactory_exports, {
createBrowser: () => createBrowser,
createBrowserWithInfo: () => createBrowserWithInfo,
isProfileLocked: () => isProfileLocked
});
module.exports = __toCommonJS(browserFactory_exports);
var import_crypto = __toESM(require("crypto"));
var import_fs = __toESM(require("fs"));
var import_net = __toESM(require("net"));
var import_path = __toESM(require("path"));
var playwright = __toESM(require("../../.."));
var import_registry = require("../../server/registry/index");
var import_log = require("./log");
var import_context = require("../backend/context");
var import_extensionContextFactory = require("./extensionContextFactory");
var import_connect = require("../utils/connect");
var import_serverRegistry = require("../../serverRegistry");
var import_connect2 = require("../../client/connect");
async function createBrowser(config, clientInfo) {
const { browser } = await createBrowserWithInfo(config, clientInfo);
return browser;
}
async function createBrowserWithInfo(config, clientInfo) {
if (config.browser.remoteEndpoint)
return await createRemoteBrowser(config);
let browser;
if (config.browser.cdpEndpoint)
browser = await createCDPBrowser(config, clientInfo);
else if (config.browser.isolated)
browser = await createIsolatedBrowser(config, clientInfo);
else if (config.extension)
browser = await (0, import_extensionContextFactory.createExtensionBrowser)(config, clientInfo);
else
browser = await createPersistentBrowser(config, clientInfo);
return { browser, browserInfo: browserInfo(browser, config) };
}
function browserInfo(browser, config) {
return {
// eslint-disable-next-line no-restricted-syntax
guid: browser._guid,
browserName: config.browser.browserName,
launchOptions: config.browser.launchOptions,
userDataDir: config.browser.userDataDir
};
}
async function createIsolatedBrowser(config, clientInfo) {
(0, import_log.testDebug)("create browser (isolated)");
await injectCdpPort(config.browser);
const browserType = playwright[config.browser.browserName];
const tracesDir = await computeTracesDir(config, clientInfo);
const browser = await browserType.launch({
tracesDir,
...config.browser.launchOptions,
handleSIGINT: false,
handleSIGTERM: false
}).catch((error) => {
if (error.message.includes("Executable doesn't exist"))
throwBrowserIsNotInstalledError(config);
throw error;
});
await startServer(browser, clientInfo);
return browser;
}
async function createCDPBrowser(config, clientInfo) {
(0, import_log.testDebug)("create browser (cdp)");
const browser = await playwright.chromium.connectOverCDP(config.browser.cdpEndpoint, {
headers: config.browser.cdpHeaders,
timeout: config.browser.cdpTimeout
});
await startServer(browser, clientInfo);
return browser;
}
async function createRemoteBrowser(config) {
(0, import_log.testDebug)("create browser (remote)");
const descriptor = await import_serverRegistry.serverRegistry.find(config.browser.remoteEndpoint);
if (descriptor) {
const browser2 = await (0, import_connect.connectToBrowserAcrossVersions)(descriptor);
return {
browser: browser2,
browserInfo: {
guid: descriptor.browser.guid,
browserName: descriptor.browser.browserName,
launchOptions: descriptor.browser.launchOptions,
userDataDir: descriptor.browser.userDataDir
}
};
}
const endpoint = config.browser.remoteEndpoint;
const playwrightObject = playwright;
const browser = await (0, import_connect2.connectToBrowser)(playwrightObject, { endpoint });
browser._connectToBrowserType(playwrightObject[browser._browserName], {}, void 0);
return { browser, browserInfo: browserInfo(browser, config) };
}
async function createPersistentBrowser(config, clientInfo) {
(0, import_log.testDebug)("create browser (persistent)");
await injectCdpPort(config.browser);
const userDataDir = config.browser.userDataDir ?? await createUserDataDir(config, clientInfo);
const tracesDir = await computeTracesDir(config, clientInfo);
if (await isProfileLocked5Times(userDataDir))
throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
const browserType = playwright[config.browser.browserName];
const launchOptions = {
tracesDir,
...config.browser.launchOptions,
...config.browser.contextOptions,
handleSIGINT: false,
handleSIGTERM: false,
ignoreDefaultArgs: [
"--disable-extensions"
]
};
try {
const browserContext = await browserType.launchPersistentContext(userDataDir, launchOptions);
const browser = browserContext.browser();
await startServer(browser, clientInfo);
return browser;
} catch (error) {
if (error.message.includes("Executable doesn't exist"))
throwBrowserIsNotInstalledError(config);
if (error.message.includes("cannot open shared object file: No such file or directory")) {
const browserName = launchOptions.channel ?? config.browser.browserName;
throw new Error(`Missing system dependencies required to run browser ${browserName}. Install them with: sudo npx playwright install-deps ${browserName}`);
}
if (error.message.includes("ProcessSingleton") || error.message.includes("exitCode=21"))
throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
throw error;
}
}
async function createUserDataDir(config, clientInfo) {
const dir = process.env.PWMCP_PROFILES_DIR_FOR_TEST ?? import_registry.registryDirectory;
const browserToken = config.browser.launchOptions?.channel ?? config.browser?.browserName;
const rootPathToken = createHash(clientInfo.cwd);
const result = import_path.default.join(dir, `mcp-${browserToken}-${rootPathToken}`);
await import_fs.default.promises.mkdir(result, { recursive: true });
return result;
}
async function injectCdpPort(browserConfig) {
if (browserConfig.browserName === "chromium")
browserConfig.launchOptions.cdpPort = await findFreePort();
}
async function findFreePort() {
return new Promise((resolve, reject) => {
const server = import_net.default.createServer();
server.listen(0, "127.0.0.1", () => {
const { port } = server.address();
server.close(() => resolve(port));
});
server.on("error", reject);
});
}
function createHash(data) {
return import_crypto.default.createHash("sha256").update(data).digest("hex").slice(0, 7);
}
async function computeTracesDir(config, clientInfo) {
return import_path.default.resolve((0, import_context.outputDir)({ config, cwd: clientInfo.cwd }), "traces");
}
async function isProfileLocked5Times(userDataDir) {
for (let i = 0; i < 5; i++) {
if (!isProfileLocked(userDataDir))
return false;
await new Promise((f) => setTimeout(f, 1e3));
}
return true;
}
function isProfileLocked(userDataDir) {
const lockFile = process.platform === "win32" ? "lockfile" : "SingletonLock";
const lockPath = import_path.default.join(userDataDir, lockFile);
if (process.platform === "win32") {
try {
const fd = import_fs.default.openSync(lockPath, "r+");
import_fs.default.closeSync(fd);
return false;
} catch (e) {
return e.code !== "ENOENT";
}
}
try {
const target = import_fs.default.readlinkSync(lockPath);
const pid = parseInt(target.split("-").pop() || "", 10);
if (isNaN(pid))
return false;
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
function throwBrowserIsNotInstalledError(config) {
const channel = config.browser.launchOptions?.channel ?? config.browser.browserName;
if (config.skillMode)
throw new Error(`Browser "${channel}" is not installed. Run \`playwright-cli install-browser ${channel}\` to install`);
else
throw new Error(`Browser "${channel}" is not installed. Run \`npx @playwright/mcp install-browser ${channel}\` to install`);
}
async function startServer(browser, clientInfo) {
if (clientInfo.sessionName)
await browser.bind(clientInfo.sessionName, { workspaceDir: clientInfo.workspaceDir });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createBrowser,
createBrowserWithInfo,
isProfileLocked
});
+352
View File
@@ -0,0 +1,352 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var cdpRelay_exports = {};
__export(cdpRelay_exports, {
CDPRelayServer: () => CDPRelayServer
});
module.exports = __toCommonJS(cdpRelay_exports);
var import_child_process = require("child_process");
var import_os = __toESM(require("os"));
var import_utilsBundle = require("../../utilsBundle");
var import_registry = require("../../server/registry/index");
var import_manualPromise = require("../../utils/isomorphic/manualPromise");
var import_http2 = require("../utils/mcp/http");
var import_log = require("./log");
var protocol = __toESM(require("./protocol"));
const debugLogger = (0, import_utilsBundle.debug)("pw:mcp:relay");
class CDPRelayServer {
constructor(server, browserChannel, userDataDir, executablePath) {
this._playwrightConnection = null;
this._extensionConnection = null;
this._nextSessionId = 1;
this._wsHost = (0, import_http2.addressToString)(server.address(), { protocol: "ws" });
this._browserChannel = browserChannel;
this._userDataDir = userDataDir;
this._executablePath = executablePath;
const uuid = crypto.randomUUID();
this._cdpPath = `/cdp/${uuid}`;
this._extensionPath = `/extension/${uuid}`;
this._resetExtensionConnection();
this._wss = new import_utilsBundle.wsServer({ server });
this._wss.on("connection", this._onConnection.bind(this));
}
cdpEndpoint() {
return `${this._wsHost}${this._cdpPath}`;
}
extensionEndpoint() {
return `${this._wsHost}${this._extensionPath}`;
}
async ensureExtensionConnectionForMCPContext(clientInfo) {
debugLogger("Ensuring extension connection for MCP context");
if (this._extensionConnection)
return;
this._connectBrowser(clientInfo);
debugLogger("Waiting for incoming extension connection");
await Promise.race([
this._extensionConnectionPromise,
new Promise((_, reject) => setTimeout(() => {
reject(new Error(`Extension connection timeout. Make sure the "Playwright MCP Bridge" extension is installed. See https://github.com/microsoft/playwright-mcp/blob/main/packages/extension/README.md for installation instructions.`));
}, process.env.PWMCP_TEST_CONNECTION_TIMEOUT ? parseInt(process.env.PWMCP_TEST_CONNECTION_TIMEOUT, 10) : 5e3))
]);
debugLogger("Extension connection established");
}
_connectBrowser(clientInfo) {
const mcpRelayEndpoint = `${this._wsHost}${this._extensionPath}`;
const url = new URL("chrome-extension://mmlmfjhmonkocbjadbfplnigmagldckm/connect.html");
url.searchParams.set("mcpRelayUrl", mcpRelayEndpoint);
const client = {
name: "Playwright Agent",
version: require("../../../package.json").version
};
url.searchParams.set("client", JSON.stringify(client));
url.searchParams.set("protocolVersion", process.env.PWMCP_TEST_PROTOCOL_VERSION ?? protocol.VERSION.toString());
const token = process.env.PLAYWRIGHT_MCP_EXTENSION_TOKEN;
if (token)
url.searchParams.set("token", token);
const href = url.toString();
const channel = import_registry.registry.isChromiumAlias(this._browserChannel) ? "chromium" : this._browserChannel;
let executablePath = this._executablePath;
if (!executablePath) {
const executableInfo = import_registry.registry.findExecutable(channel);
if (!executableInfo)
throw new Error(`Unsupported channel: "${this._browserChannel}"`);
executablePath = executableInfo.executablePath();
if (!executablePath)
throw new Error(`"${this._browserChannel}" executable not found. Make sure it is installed at a standard location.`);
}
const args = [];
if (this._userDataDir)
args.push(`--user-data-dir=${this._userDataDir}`);
if (import_os.default.platform() === "linux" && channel === "chromium")
args.push("--no-sandbox");
args.push(href);
(0, import_child_process.spawn)(executablePath, args, {
windowsHide: true,
detached: true,
shell: false,
stdio: "ignore"
});
}
stop() {
this.closeConnections("Server stopped");
this._wss.close();
}
closeConnections(reason) {
this._closePlaywrightConnection(reason);
this._closeExtensionConnection(reason);
}
_onConnection(ws2, request) {
const url = new URL(`http://localhost${request.url}`);
debugLogger(`New connection to ${url.pathname}`);
if (url.pathname === this._cdpPath) {
this._handlePlaywrightConnection(ws2);
} else if (url.pathname === this._extensionPath) {
this._handleExtensionConnection(ws2);
} else {
debugLogger(`Invalid path: ${url.pathname}`);
ws2.close(4004, "Invalid path");
}
}
_handlePlaywrightConnection(ws2) {
if (this._playwrightConnection) {
debugLogger("Rejecting second Playwright connection");
ws2.close(1e3, "Another CDP client already connected");
return;
}
this._playwrightConnection = ws2;
ws2.on("message", async (data) => {
try {
const message = JSON.parse(data.toString());
await this._handlePlaywrightMessage(message);
} catch (error) {
debugLogger(`Error while handling Playwright message
${data.toString()}
`, error);
}
});
ws2.on("close", () => {
if (this._playwrightConnection !== ws2)
return;
this._playwrightConnection = null;
this._closeExtensionConnection("Playwright client disconnected");
debugLogger("Playwright WebSocket closed");
});
ws2.on("error", (error) => {
debugLogger("Playwright WebSocket error:", error);
});
debugLogger("Playwright MCP connected");
}
_closeExtensionConnection(reason) {
this._extensionConnection?.close(reason);
this._extensionConnectionPromise.reject(new Error(reason));
this._resetExtensionConnection();
}
_resetExtensionConnection() {
this._connectedTabInfo = void 0;
this._extensionConnection = null;
this._extensionConnectionPromise = new import_manualPromise.ManualPromise();
void this._extensionConnectionPromise.catch(import_log.logUnhandledError);
}
_closePlaywrightConnection(reason) {
if (this._playwrightConnection?.readyState === import_utilsBundle.ws.OPEN)
this._playwrightConnection.close(1e3, reason);
this._playwrightConnection = null;
}
_handleExtensionConnection(ws2) {
if (this._extensionConnection) {
ws2.close(1e3, "Another extension connection already established");
return;
}
this._extensionConnection = new ExtensionConnection(ws2);
this._extensionConnection.onclose = (c, reason) => {
debugLogger("Extension WebSocket closed:", reason, c === this._extensionConnection);
if (this._extensionConnection !== c)
return;
this._resetExtensionConnection();
this._closePlaywrightConnection(`Extension disconnected: ${reason}`);
};
this._extensionConnection.onmessage = this._handleExtensionMessage.bind(this);
this._extensionConnectionPromise.resolve();
}
_handleExtensionMessage(method, params) {
switch (method) {
case "forwardCDPEvent":
const sessionId = params.sessionId || this._connectedTabInfo?.sessionId;
this._sendToPlaywright({
sessionId,
method: params.method,
params: params.params
});
break;
}
}
async _handlePlaywrightMessage(message) {
debugLogger("\u2190 Playwright:", `${message.method} (id=${message.id})`);
const { id, sessionId, method, params } = message;
try {
const result = await this._handleCDPCommand(method, params, sessionId);
this._sendToPlaywright({ id, sessionId, result });
} catch (e) {
debugLogger("Error in the extension:", e);
this._sendToPlaywright({
id,
sessionId,
error: { message: e.message }
});
}
}
async _handleCDPCommand(method, params, sessionId) {
switch (method) {
case "Browser.getVersion": {
return {
protocolVersion: "1.3",
product: "Chrome/Extension-Bridge",
userAgent: "CDP-Bridge-Server/1.0.0"
};
}
case "Browser.setDownloadBehavior": {
return {};
}
case "Target.setAutoAttach": {
if (sessionId)
break;
const { targetInfo } = await this._extensionConnection.send("attachToTab", {});
this._connectedTabInfo = {
targetInfo,
sessionId: `pw-tab-${this._nextSessionId++}`
};
debugLogger("Simulating auto-attach");
this._sendToPlaywright({
method: "Target.attachedToTarget",
params: {
sessionId: this._connectedTabInfo.sessionId,
targetInfo: {
...this._connectedTabInfo.targetInfo,
attached: true
},
waitingForDebugger: false
}
});
return {};
}
case "Target.getTargetInfo": {
return this._connectedTabInfo?.targetInfo;
}
}
return await this._forwardToExtension(method, params, sessionId);
}
async _forwardToExtension(method, params, sessionId) {
if (!this._extensionConnection)
throw new Error("Extension not connected");
if (this._connectedTabInfo?.sessionId === sessionId)
sessionId = void 0;
return await this._extensionConnection.send("forwardCDPCommand", { sessionId, method, params });
}
_sendToPlaywright(message) {
debugLogger("\u2192 Playwright:", `${message.method ?? `response(id=${message.id})`}`);
this._playwrightConnection?.send(JSON.stringify(message));
}
}
class ExtensionConnection {
constructor(ws2) {
this._callbacks = /* @__PURE__ */ new Map();
this._lastId = 0;
this._ws = ws2;
this._ws.on("message", this._onMessage.bind(this));
this._ws.on("close", this._onClose.bind(this));
this._ws.on("error", this._onError.bind(this));
}
async send(method, params) {
if (this._ws.readyState !== import_utilsBundle.ws.OPEN)
throw new Error(`Unexpected WebSocket state: ${this._ws.readyState}`);
const id = ++this._lastId;
this._ws.send(JSON.stringify({ id, method, params }));
const error = new Error(`Protocol error: ${method}`);
return new Promise((resolve, reject) => {
this._callbacks.set(id, { resolve, reject, error });
});
}
close(message) {
debugLogger("closing extension connection:", message);
if (this._ws.readyState === import_utilsBundle.ws.OPEN)
this._ws.close(1e3, message);
}
_onMessage(event) {
const eventData = event.toString();
let parsedJson;
try {
parsedJson = JSON.parse(eventData);
} catch (e) {
debugLogger(`<closing ws> Closing websocket due to malformed JSON. eventData=${eventData} e=${e?.message}`);
this._ws.close();
return;
}
try {
this._handleParsedMessage(parsedJson);
} catch (e) {
debugLogger(`<closing ws> Closing websocket due to failed onmessage callback. eventData=${eventData} e=${e?.message}`);
this._ws.close();
}
}
_handleParsedMessage(object) {
if (object.id && this._callbacks.has(object.id)) {
const callback = this._callbacks.get(object.id);
this._callbacks.delete(object.id);
if (object.error) {
const error = callback.error;
error.message = object.error;
callback.reject(error);
} else {
callback.resolve(object.result);
}
} else if (object.id) {
debugLogger("\u2190 Extension: unexpected response", object);
} else {
this.onmessage?.(object.method, object.params);
}
}
_onClose(event) {
debugLogger(`<ws closed> code=${event.code} reason=${event.reason}`);
this._dispose();
this.onclose?.(this, event.reason);
}
_onError(event) {
debugLogger(`<ws error> message=${event.message} type=${event.type} target=${event.target}`);
this._dispose();
}
_dispose() {
for (const callback of this._callbacks.values())
callback.reject(new Error("WebSocket closed"));
this._callbacks.clear();
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CDPRelayServer
});
+7
View File
@@ -0,0 +1,7 @@
"use strict";
var import_utilsBundle = require("../../utilsBundle");
var import_program = require("./program");
const packageJSON = require("../../../package.json");
const p = import_utilsBundle.program.version("Version " + packageJSON.version).name("Playwright MCP");
(0, import_program.decorateMCPCommand)(p);
void import_utilsBundle.program.parseAsync(process.argv);
+16
View File
@@ -0,0 +1,16 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var config_d_exports = {};
module.exports = __toCommonJS(config_d_exports);
+446
View File
@@ -0,0 +1,446 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var config_exports = {};
__export(config_exports, {
commaSeparatedList: () => commaSeparatedList,
configFromEnv: () => configFromEnv,
dotenvFileLoader: () => dotenvFileLoader,
enumParser: () => enumParser,
headerParser: () => headerParser,
loadConfig: () => loadConfig,
numberParser: () => numberParser,
resolutionParser: () => resolutionParser,
resolveCLIConfigForCLI: () => resolveCLIConfigForCLI,
resolveCLIConfigForMCP: () => resolveCLIConfigForMCP,
resolveConfig: () => resolveConfig,
semicolonSeparatedList: () => semicolonSeparatedList
});
module.exports = __toCommonJS(config_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_os = __toESM(require("os"));
var import__ = require("../../..");
var import_utilsBundle = require("../../utilsBundle");
var import_configIni = require("./configIni");
async function fileExistsAsync(resolved) {
try {
return (await import_fs.default.promises.stat(resolved)).isFile();
} catch {
return false;
}
}
const defaultConfig = {
browser: {
launchOptions: {},
contextOptions: {}
},
timeouts: {
action: 5e3,
navigation: 6e4,
expect: 5e3
}
};
async function resolveConfig(config) {
const merged = mergeConfig(defaultConfig, config);
const browser = await validateBrowserConfig(merged.browser);
return { ...merged, browser };
}
async function resolveCLIConfigForMCP(cliOptions, env) {
const envOverrides = configFromEnv(env);
const cliOverrides = configFromCLIOptions(cliOptions);
const configFile = cliOverrides.configFile ?? envOverrides.configFile;
const configInFile = await loadConfig(configFile);
let result = defaultConfig;
result = mergeConfig(result, configInFile);
result = mergeConfig(result, envOverrides);
result = mergeConfig(result, cliOverrides);
const browser = await validateBrowserConfig(result.browser);
if (browser.launchOptions.headless === void 0)
browser.launchOptions.headless = import_os.default.platform() === "linux" && !process.env.DISPLAY;
return { ...result, browser, configFile };
}
async function resolveCLIConfigForCLI(daemonProfilesDir, sessionName, options, env) {
const config = options.config ? import_path.default.resolve(options.config) : void 0;
try {
const defaultConfigFile = import_path.default.resolve(".playwright", "cli.config.json");
if (!config && import_fs.default.existsSync(defaultConfigFile))
options.config = defaultConfigFile;
} catch {
}
const daemonOverrides = configFromCLIOptions({
endpoint: options.endpoint,
config: options.config,
browser: options.browser,
headless: options.headed ? false : void 0,
extension: options.extension,
userDataDir: options.profile,
snapshotMode: "full"
});
const envOverrides = configFromEnv(env);
const configFile = daemonOverrides.configFile ?? envOverrides.configFile;
const configInFile = await loadConfig(configFile);
const globalConfigPath = import_path.default.join((env ?? process.env)["PWTEST_CLI_GLOBAL_CONFIG"] ?? import_os.default.homedir(), ".playwright", "cli.config.json");
const globalConfigInFile = await loadConfig(import_fs.default.existsSync(globalConfigPath) ? globalConfigPath : void 0);
let result = defaultConfig;
result = mergeConfig(result, globalConfigInFile);
result = mergeConfig(result, configInFile);
result = mergeConfig(result, envOverrides);
result = mergeConfig(result, daemonOverrides);
if (result.browser.isolated === void 0)
result.browser.isolated = !options.profile && !options.persistent && !result.browser.userDataDir && !result.browser.remoteEndpoint && !result.extension;
if (!result.extension && !result.browser.isolated && !result.browser.userDataDir && !result.browser.remoteEndpoint) {
const browserToken = result.browser.launchOptions?.channel ?? result.browser?.browserName;
const userDataDir = import_path.default.resolve(daemonProfilesDir, `ud-${sessionName}-${browserToken}`);
result.browser.userDataDir = userDataDir;
}
if (result.browser.launchOptions.headless === void 0)
result.browser.launchOptions.headless = true;
const browser = await validateBrowserConfig(result.browser);
return { ...result, browser, configFile, skillMode: true };
}
async function validateBrowserConfig(browser) {
let browserName = browser.browserName;
if (!browserName) {
browserName = "chromium";
if (browser.launchOptions.channel === void 0)
browser.launchOptions.channel = "chrome";
}
if (browser.browserName === "chromium" && browser.launchOptions.chromiumSandbox === void 0) {
if (process.platform === "linux")
browser.launchOptions.chromiumSandbox = browser.launchOptions.channel !== "chromium" && browser.launchOptions.channel !== "chrome-for-testing";
else
browser.launchOptions.chromiumSandbox = true;
}
if (browser.isolated && browser.userDataDir)
throw new Error("Browser userDataDir is not supported in isolated mode.");
if (browser.initScript) {
for (const script of browser.initScript) {
if (!await fileExistsAsync(script))
throw new Error(`Init script file does not exist: ${script}`);
}
}
if (browser.initPage) {
for (const page of browser.initPage) {
if (!await fileExistsAsync(page))
throw new Error(`Init page file does not exist: ${page}`);
}
}
if (browser.contextOptions.viewport === void 0) {
if (browser.launchOptions.headless)
browser.contextOptions.viewport = { width: 1280, height: 720 };
else
browser.contextOptions.viewport = null;
}
return { ...browser, browserName };
}
function configFromCLIOptions(cliOptions) {
let browserName;
let channel;
switch (cliOptions.browser) {
case "chrome":
case "chrome-beta":
case "chrome-canary":
case "chrome-dev":
case "msedge":
case "msedge-beta":
case "msedge-canary":
case "msedge-dev":
browserName = "chromium";
channel = cliOptions.browser;
break;
case "chromium":
browserName = "chromium";
channel = "chrome-for-testing";
break;
case "firefox":
browserName = "firefox";
break;
case "webkit":
browserName = "webkit";
break;
}
const launchOptions = {
channel,
executablePath: cliOptions.executablePath,
headless: cliOptions.headless
};
if (cliOptions.sandbox !== void 0)
launchOptions.chromiumSandbox = cliOptions.sandbox;
if (cliOptions.proxyServer) {
launchOptions.proxy = {
server: cliOptions.proxyServer
};
if (cliOptions.proxyBypass)
launchOptions.proxy.bypass = cliOptions.proxyBypass;
}
if (cliOptions.device && cliOptions.cdpEndpoint)
throw new Error("Device emulation is not supported with cdpEndpoint.");
const contextOptions = cliOptions.device ? import__.devices[cliOptions.device] : {};
if (cliOptions.storageState)
contextOptions.storageState = cliOptions.storageState;
if (cliOptions.userAgent)
contextOptions.userAgent = cliOptions.userAgent;
if (cliOptions.viewportSize)
contextOptions.viewport = cliOptions.viewportSize;
if (cliOptions.ignoreHttpsErrors)
contextOptions.ignoreHTTPSErrors = true;
if (cliOptions.blockServiceWorkers)
contextOptions.serviceWorkers = "block";
if (cliOptions.grantPermissions)
contextOptions.permissions = cliOptions.grantPermissions;
const config = {
browser: {
browserName,
isolated: cliOptions.isolated,
userDataDir: cliOptions.userDataDir,
launchOptions,
contextOptions,
cdpEndpoint: cliOptions.cdpEndpoint,
cdpHeaders: cliOptions.cdpHeader,
cdpTimeout: cliOptions.cdpTimeout,
initPage: cliOptions.initPage,
initScript: cliOptions.initScript,
remoteEndpoint: cliOptions.endpoint
},
extension: cliOptions.extension,
server: {
port: cliOptions.port,
host: cliOptions.host,
allowedHosts: cliOptions.allowedHosts
},
capabilities: cliOptions.caps,
console: {
level: cliOptions.consoleLevel
},
network: {
allowedOrigins: cliOptions.allowedOrigins,
blockedOrigins: cliOptions.blockedOrigins
},
allowUnrestrictedFileAccess: cliOptions.allowUnrestrictedFileAccess,
codegen: cliOptions.codegen,
saveSession: cliOptions.saveSession,
secrets: cliOptions.secrets,
sharedBrowserContext: cliOptions.sharedBrowserContext,
snapshot: cliOptions.snapshotMode ? { mode: cliOptions.snapshotMode } : void 0,
outputDir: cliOptions.outputDir,
imageResponses: cliOptions.imageResponses,
testIdAttribute: cliOptions.testIdAttribute,
timeouts: {
action: cliOptions.timeoutAction,
navigation: cliOptions.timeoutNavigation
}
};
return { ...config, configFile: cliOptions.config };
}
function configFromEnv(env) {
const e = env ?? process.env;
const options = {};
options.allowedHosts = commaSeparatedList(e.PLAYWRIGHT_MCP_ALLOWED_HOSTS);
options.allowedOrigins = semicolonSeparatedList(e.PLAYWRIGHT_MCP_ALLOWED_ORIGINS);
options.allowUnrestrictedFileAccess = envToBoolean(e.PLAYWRIGHT_MCP_ALLOW_UNRESTRICTED_FILE_ACCESS);
options.blockedOrigins = semicolonSeparatedList(e.PLAYWRIGHT_MCP_BLOCKED_ORIGINS);
options.blockServiceWorkers = envToBoolean(e.PLAYWRIGHT_MCP_BLOCK_SERVICE_WORKERS);
options.browser = envToString(e.PLAYWRIGHT_MCP_BROWSER);
options.caps = commaSeparatedList(e.PLAYWRIGHT_MCP_CAPS);
options.cdpEndpoint = envToString(e.PLAYWRIGHT_MCP_CDP_ENDPOINT);
options.cdpHeader = headerParser(envToString(e.PLAYWRIGHT_MCP_CDP_HEADERS));
options.cdpTimeout = numberParser(e.PLAYWRIGHT_MCP_CDP_TIMEOUT);
options.config = envToString(e.PLAYWRIGHT_MCP_CONFIG);
if (e.PLAYWRIGHT_MCP_CONSOLE_LEVEL)
options.consoleLevel = enumParser("--console-level", ["error", "warning", "info", "debug"], e.PLAYWRIGHT_MCP_CONSOLE_LEVEL);
options.device = envToString(e.PLAYWRIGHT_MCP_DEVICE);
options.executablePath = envToString(e.PLAYWRIGHT_MCP_EXECUTABLE_PATH);
options.extension = envToBoolean(e.PLAYWRIGHT_MCP_EXTENSION);
options.grantPermissions = commaSeparatedList(e.PLAYWRIGHT_MCP_GRANT_PERMISSIONS);
options.headless = envToBoolean(e.PLAYWRIGHT_MCP_HEADLESS);
options.host = envToString(e.PLAYWRIGHT_MCP_HOST);
options.ignoreHttpsErrors = envToBoolean(e.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
const initPage = envToString(e.PLAYWRIGHT_MCP_INIT_PAGE);
if (initPage)
options.initPage = [initPage];
const initScript = envToString(e.PLAYWRIGHT_MCP_INIT_SCRIPT);
if (initScript)
options.initScript = [initScript];
options.isolated = envToBoolean(e.PLAYWRIGHT_MCP_ISOLATED);
if (e.PLAYWRIGHT_MCP_IMAGE_RESPONSES)
options.imageResponses = enumParser("--image-responses", ["allow", "omit"], e.PLAYWRIGHT_MCP_IMAGE_RESPONSES);
options.sandbox = envToBoolean(e.PLAYWRIGHT_MCP_SANDBOX);
options.outputDir = envToString(e.PLAYWRIGHT_MCP_OUTPUT_DIR);
options.port = numberParser(e.PLAYWRIGHT_MCP_PORT);
options.proxyBypass = envToString(e.PLAYWRIGHT_MCP_PROXY_BYPASS);
options.proxyServer = envToString(e.PLAYWRIGHT_MCP_PROXY_SERVER);
options.secrets = dotenvFileLoader(e.PLAYWRIGHT_MCP_SECRETS_FILE);
options.storageState = envToString(e.PLAYWRIGHT_MCP_STORAGE_STATE);
options.testIdAttribute = envToString(e.PLAYWRIGHT_MCP_TEST_ID_ATTRIBUTE);
options.timeoutAction = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_ACTION);
options.timeoutNavigation = numberParser(e.PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION);
options.userAgent = envToString(e.PLAYWRIGHT_MCP_USER_AGENT);
options.userDataDir = envToString(e.PLAYWRIGHT_MCP_USER_DATA_DIR);
options.viewportSize = resolutionParser("--viewport-size", e.PLAYWRIGHT_MCP_VIEWPORT_SIZE);
return configFromCLIOptions(options);
}
async function loadConfig(configFile) {
if (!configFile)
return {};
if (configFile.endsWith(".ini"))
return (0, import_configIni.configFromIniFile)(configFile);
try {
const data = await import_fs.default.promises.readFile(configFile, "utf8");
return JSON.parse(data.charCodeAt(0) === 65279 ? data.slice(1) : data);
} catch {
return (0, import_configIni.configFromIniFile)(configFile);
}
}
function pickDefined(obj) {
return Object.fromEntries(
Object.entries(obj ?? {}).filter(([_, v]) => v !== void 0)
);
}
function mergeConfig(base, overrides) {
const browser = {
...pickDefined(base.browser),
...pickDefined(overrides.browser),
browserName: overrides.browser?.browserName ?? base.browser?.browserName,
isolated: overrides.browser?.isolated ?? base.browser?.isolated,
launchOptions: {
...pickDefined(base.browser?.launchOptions),
...pickDefined(overrides.browser?.launchOptions),
// Assistant mode is not a part of the public API.
...{ assistantMode: true }
},
contextOptions: {
...pickDefined(base.browser?.contextOptions),
...pickDefined(overrides.browser?.contextOptions)
}
};
if (browser.browserName !== "chromium" && browser.launchOptions)
delete browser.launchOptions.channel;
return {
...pickDefined(base),
...pickDefined(overrides),
browser,
console: {
...pickDefined(base.console),
...pickDefined(overrides.console)
},
network: {
...pickDefined(base.network),
...pickDefined(overrides.network)
},
server: {
...pickDefined(base.server),
...pickDefined(overrides.server)
},
snapshot: {
...pickDefined(base.snapshot),
...pickDefined(overrides.snapshot)
},
timeouts: {
...pickDefined(base.timeouts),
...pickDefined(overrides.timeouts)
}
};
}
function semicolonSeparatedList(value) {
if (!value)
return void 0;
return value.split(";").map((v) => v.trim());
}
function commaSeparatedList(value) {
if (!value)
return void 0;
return value.split(",").map((v) => v.trim());
}
function dotenvFileLoader(value) {
if (!value)
return void 0;
return import_utilsBundle.dotenv.parse(import_fs.default.readFileSync(value, "utf8"));
}
function numberParser(value) {
if (!value)
return void 0;
return +value;
}
function resolutionParser(name, value) {
if (!value)
return void 0;
if (value.includes("x")) {
const [width, height] = value.split("x").map((v) => +v);
if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0)
throw new Error(`Invalid resolution format: use ${name}="800x600"`);
return { width, height };
}
if (value.includes(",")) {
const [width, height] = value.split(",").map((v) => +v);
if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0)
throw new Error(`Invalid resolution format: use ${name}="800x600"`);
return { width, height };
}
throw new Error(`Invalid resolution format: use ${name}="800x600"`);
}
function headerParser(arg, previous) {
if (!arg)
return previous;
const result = { ...previous ?? {} };
const colonIndex = arg.indexOf(":");
const name = colonIndex === -1 ? arg.trim() : arg.substring(0, colonIndex).trim();
const value = colonIndex === -1 ? "" : arg.substring(colonIndex + 1).trim();
result[name] = value;
return result;
}
function enumParser(name, options, value) {
if (!options.includes(value))
throw new Error(`Invalid ${name}: ${value}. Valid values are: ${options.join(", ")}`);
return value;
}
function envToBoolean(value) {
if (value === "true" || value === "1")
return true;
if (value === "false" || value === "0")
return false;
return void 0;
}
function envToString(value) {
return value ? value.trim() : void 0;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
commaSeparatedList,
configFromEnv,
dotenvFileLoader,
enumParser,
headerParser,
loadConfig,
numberParser,
resolutionParser,
resolveCLIConfigForCLI,
resolveCLIConfigForMCP,
resolveConfig,
semicolonSeparatedList
});
+189
View File
@@ -0,0 +1,189 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var configIni_exports = {};
__export(configIni_exports, {
configFromIniFile: () => configFromIniFile,
configsFromIniFile: () => configsFromIniFile
});
module.exports = __toCommonJS(configIni_exports);
var import_fs = __toESM(require("fs"));
var import_utilsBundle = require("../../utilsBundle");
function configFromIniFile(filePath) {
const content = import_fs.default.readFileSync(filePath, "utf8");
const parsed = import_utilsBundle.ini.parse(content);
return iniEntriesToConfig(parsed);
}
function configsFromIniFile(filePath) {
const content = import_fs.default.readFileSync(filePath, "utf8");
const parsed = import_utilsBundle.ini.parse(content);
const result = /* @__PURE__ */ new Map();
for (const [sectionName, sectionData] of Object.entries(parsed)) {
if (typeof sectionData !== "object" || sectionData === null)
continue;
result.set(sectionName, iniEntriesToConfig(sectionData));
}
return result;
}
function iniEntriesToConfig(entries) {
const config = {};
for (const [targetPath, rawValue] of Object.entries(entries)) {
const type = longhandTypes[targetPath];
const value = type ? coerceToType(rawValue, type) : coerceIniValue(rawValue);
setNestedValue(config, targetPath, value);
}
return config;
}
function coerceToType(value, type) {
switch (type) {
case "string":
return String(value);
case "number":
return Number(value);
case "boolean":
if (typeof value === "boolean")
return value;
return value === "true" || value === "1";
case "string[]":
if (Array.isArray(value))
return value.map(String);
return [String(value)];
case "size": {
if (typeof value === "string" && value.includes("x")) {
const [w, h] = value.split("x").map(Number);
if (!isNaN(w) && !isNaN(h) && w > 0 && h > 0)
return { width: w, height: h };
}
return void 0;
}
}
}
function coerceIniValue(value) {
if (typeof value !== "string")
return value;
const trimmed = value.trim();
if (trimmed === "")
return trimmed;
const num = Number(trimmed);
if (!isNaN(num))
return num;
return value;
}
function setNestedValue(obj, dotPath, value) {
const parts = dotPath.split(".");
let current = obj;
for (let i = 0; i < parts.length - 1; i++) {
const part = parts[i];
if (!(part in current) || typeof current[part] !== "object" || current[part] === null)
current[part] = {};
current = current[part];
}
current[parts[parts.length - 1]] = value;
}
const longhandTypes = {
// browser direct
"browser.browserName": "string",
"browser.isolated": "boolean",
"browser.userDataDir": "string",
"browser.cdpEndpoint": "string",
"browser.cdpTimeout": "number",
"browser.remoteEndpoint": "string",
"browser.initPage": "string[]",
"browser.initScript": "string[]",
// browser.launchOptions
"browser.launchOptions.channel": "string",
"browser.launchOptions.headless": "boolean",
"browser.launchOptions.executablePath": "string",
"browser.launchOptions.chromiumSandbox": "boolean",
"browser.launchOptions.args": "string[]",
"browser.launchOptions.downloadsPath": "string",
"browser.launchOptions.handleSIGHUP": "boolean",
"browser.launchOptions.handleSIGINT": "boolean",
"browser.launchOptions.handleSIGTERM": "boolean",
"browser.launchOptions.slowMo": "number",
"browser.launchOptions.timeout": "number",
"browser.launchOptions.tracesDir": "string",
"browser.launchOptions.proxy.server": "string",
"browser.launchOptions.proxy.bypass": "string",
"browser.launchOptions.proxy.username": "string",
"browser.launchOptions.proxy.password": "string",
// browser.contextOptions
"browser.contextOptions.acceptDownloads": "boolean",
"browser.contextOptions.baseURL": "string",
"browser.contextOptions.bypassCSP": "boolean",
"browser.contextOptions.colorScheme": "string",
"browser.contextOptions.contrast": "string",
"browser.contextOptions.deviceScaleFactor": "number",
"browser.contextOptions.forcedColors": "string",
"browser.contextOptions.hasTouch": "boolean",
"browser.contextOptions.ignoreHTTPSErrors": "boolean",
"browser.contextOptions.isMobile": "boolean",
"browser.contextOptions.javaScriptEnabled": "boolean",
"browser.contextOptions.locale": "string",
"browser.contextOptions.offline": "boolean",
"browser.contextOptions.permissions": "string[]",
"browser.contextOptions.reducedMotion": "string",
"browser.contextOptions.screen": "size",
"browser.contextOptions.serviceWorkers": "string",
"browser.contextOptions.storageState": "string",
"browser.contextOptions.strictSelectors": "boolean",
"browser.contextOptions.timezoneId": "string",
"browser.contextOptions.userAgent": "string",
"browser.contextOptions.viewport": "size",
// top-level
"extension": "boolean",
"capabilities": "string[]",
"saveSession": "boolean",
"saveTrace": "boolean",
"saveVideo": "size",
"sharedBrowserContext": "boolean",
"outputDir": "string",
"imageResponses": "string",
"allowUnrestrictedFileAccess": "boolean",
"codegen": "string",
"testIdAttribute": "string",
// server
"server.port": "number",
"server.host": "string",
"server.allowedHosts": "string[]",
// console
"console.level": "string",
// network
"network.allowedOrigins": "string[]",
"network.blockedOrigins": "string[]",
// timeouts
"timeouts.action": "number",
"timeouts.navigation": "number",
// snapshot
"snapshot.mode": "string"
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
configFromIniFile,
configsFromIniFile
});
+55
View File
@@ -0,0 +1,55 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var extensionContextFactory_exports = {};
__export(extensionContextFactory_exports, {
createExtensionBrowser: () => createExtensionBrowser
});
module.exports = __toCommonJS(extensionContextFactory_exports);
var playwright = __toESM(require("../../.."));
var import_utilsBundle = require("../../utilsBundle");
var import_network = require("../../server/utils/network");
var import_cdpRelay = require("./cdpRelay");
const debugLogger = (0, import_utilsBundle.debug)("pw:mcp:relay");
async function createExtensionBrowser(config, clientInfo) {
const httpServer = (0, import_network.createHttpServer)();
await (0, import_network.startHttpServer)(httpServer, {});
const relay = new import_cdpRelay.CDPRelayServer(
httpServer,
config.browser.launchOptions.channel || "chrome",
config.browser.userDataDir,
config.browser.launchOptions.executablePath
);
debugLogger(`CDP relay server started, extension endpoint: ${relay.extensionEndpoint()}.`);
await relay.ensureExtensionConnectionForMCPContext(clientInfo);
return await playwright.chromium.connectOverCDP(relay.cdpEndpoint(), { isLocal: true });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createExtensionBrowser
});
+62
View File
@@ -0,0 +1,62 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var mcp_exports = {};
__export(mcp_exports, {
createConnection: () => createConnection
});
module.exports = __toCommonJS(mcp_exports);
var import_config = require("./config");
var import_tools = require("../backend/tools");
var import_browserFactory = require("./browserFactory");
var import_browserBackend = require("../backend/browserBackend");
var import_server = require("../utils/mcp/server");
const packageJSON = require("../../../package.json");
async function createConnection(userConfig = {}, contextGetter) {
const config = await (0, import_config.resolveConfig)(userConfig);
const tools = (0, import_tools.filteredTools)(config);
const backendFactory = {
name: "api",
nameInConfig: "api",
version: packageJSON.version,
toolSchemas: tools.map((tool) => tool.schema),
create: async (clientInfo) => {
const browser = contextGetter ? new SimpleBrowser(await contextGetter()) : await (0, import_browserFactory.createBrowser)(config, clientInfo);
const context = config.browser.isolated ? await browser.newContext(config.browser.contextOptions) : browser.contexts()[0];
return new import_browserBackend.BrowserBackend(config, context, tools);
},
disposed: async () => {
}
};
return (0, import_server.createServer)("api", packageJSON.version, backendFactory, false);
}
class SimpleBrowser {
constructor(context) {
this._context = context;
}
contexts() {
return [this._context];
}
async newContext() {
throw new Error("Creating a new context is not supported in SimpleBrowserContextFactory.");
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createConnection
});
+35
View File
@@ -0,0 +1,35 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var log_exports = {};
__export(log_exports, {
logUnhandledError: () => logUnhandledError,
testDebug: () => testDebug
});
module.exports = __toCommonJS(log_exports);
var import_utilsBundle = require("../../utilsBundle");
const errorDebug = (0, import_utilsBundle.debug)("pw:mcp:error");
function logUnhandledError(error) {
errorDebug(error);
}
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
logUnhandledError,
testDebug
});
File diff suppressed because one or more lines are too long
+28
View File
@@ -0,0 +1,28 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var protocol_exports = {};
__export(protocol_exports, {
VERSION: () => VERSION
});
module.exports = __toCommonJS(protocol_exports);
const VERSION = 1;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
VERSION
});
+44
View File
@@ -0,0 +1,44 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var watchdog_exports = {};
__export(watchdog_exports, {
setupExitWatchdog: () => setupExitWatchdog
});
module.exports = __toCommonJS(watchdog_exports);
var import_utils = require("../../utils");
var import_log = require("./log");
function setupExitWatchdog() {
let isExiting = false;
const handleExit = async (signal) => {
if (isExiting)
return;
isExiting = true;
setTimeout(() => process.exit(0), 15e3);
(0, import_log.testDebug)("gracefully closing " + import_utils.gracefullyCloseSet.size);
await (0, import_utils.gracefullyCloseAll)();
process.exit(0);
};
process.stdin.on("close", () => handleExit("close"));
process.on("SIGINT", () => handleExit("SIGINT"));
process.on("SIGTERM", () => handleExit("SIGTERM"));
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
setupExitWatchdog
});
+171
View File
@@ -0,0 +1,171 @@
---
name: playwright-trace
description: Inspect Playwright trace files from the command line — list actions, view requests, console, errors, snapshots and screenshots.
allowed-tools: Bash(npx:*)
---
# Playwright Trace CLI
Inspect `.zip` trace files produced by Playwright tests without opening a browser.
## Workflow
1. Start with `trace open <trace.zip>` to extract the trace and see its metadata.
2. Use `trace actions` to see all actions with their action IDs.
3. Use `trace action <action-id>` to drill into a specific action — see parameters, logs, source location, and available snapshots.
4. Use `trace requests`, `trace console`, or `trace errors` for cross-cutting views.
5. Use `trace snapshot <action-id>` to get the DOM snapshot, or run a browser command against it.
6. Use `trace close` to remove the extracted trace data when done.
All commands after `open` operate on the currently opened trace — no need to pass the trace file again. Opening a new trace replaces the previous one.
## Commands
### Open a trace
```bash
# Extract trace and show metadata: browser, viewport, duration, action/error counts
npx playwright trace open <trace.zip>
```
### Close a trace
```bash
# Remove extracted trace data
npx playwright trace close
```
### Actions
```bash
# List all actions as a tree with action IDs and timing
npx playwright trace actions
# Filter by action title (regex, case-insensitive)
npx playwright trace actions --grep "click"
# Only failed actions
npx playwright trace actions --errors-only
```
### Action details
```bash
# Show full details for one action: params, result, logs, source, snapshots
npx playwright trace action <action-id>
```
The `action` command displays available snapshot phases (before, input, after) and the exact command to extract them.
### Requests
```bash
# All network requests: method, status, URL, duration, size
npx playwright trace requests
# Filter by URL pattern
npx playwright trace requests --grep "api"
# Filter by HTTP method
npx playwright trace requests --method POST
# Only failed requests (status >= 400)
npx playwright trace requests --failed
```
### Request details
```bash
# Show full details for one request: headers, body, security
npx playwright trace request <request-id>
```
### Console
```bash
# All console messages and stdout/stderr
npx playwright trace console
# Only errors
npx playwright trace console --errors-only
# Only browser console (no stdout/stderr)
npx playwright trace console --browser
# Only stdout/stderr (no browser console)
npx playwright trace console --stdio
```
### Errors
```bash
# All errors with stack traces and associated actions
npx playwright trace errors
```
### Snapshots
The `snapshot` command loads the DOM snapshot for an action into a headless browser and runs a single browser command against it. Without a browser command, it returns the accessibility snapshot.
```bash
# Get the accessibility snapshot (default)
npx playwright trace snapshot <action-id>
# Use a specific phase
npx playwright trace snapshot <action-id> --name before
# Run eval to query the DOM
npx playwright trace snapshot <action-id> -- eval "document.title"
npx playwright trace snapshot <action-id> -- eval "document.querySelector('#error').textContent"
# Eval on a specific element ref (from the snapshot)
npx playwright trace snapshot <action-id> -- eval "el => el.getAttribute('data-testid')" e5
# Take a screenshot of the snapshot
npx playwright trace snapshot <action-id> -- screenshot
# Redirect output to a file
npx playwright trace snapshot <action-id> -- eval "document.body.outerHTML" --filename=page.html
npx playwright trace snapshot <action-id> -- screenshot --filename=screenshot.png
```
Only three browser commands are useful on a frozen snapshot: `snapshot`, `eval`, and `screenshot`.
### Attachments
```bash
# List all trace attachments
npx playwright trace attachments
# Extract an attachment by its number
npx playwright trace attachment 1
npx playwright trace attachment 1 -o out.png
```
## Typical investigation
```bash
# 1. Open the trace and see what's inside
npx playwright trace open test-results/my-test/trace.zip
# 2. What actions ran?
npx playwright trace actions
# 3. Which action failed?
npx playwright trace actions --errors-only
# 4. What went wrong?
npx playwright trace action 12
# 5. What did the page look like at that moment?
npx playwright trace snapshot 12
# 6. Query the DOM for more detail
npx playwright trace snapshot 12 -- eval "document.querySelector('.error-message').textContent"
# 7. Any relevant network failures?
npx playwright trace requests --failed
# 8. Any console errors?
npx playwright trace console --errors-only
```
+48
View File
@@ -0,0 +1,48 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var installSkill_exports = {};
__export(installSkill_exports, {
installSkill: () => installSkill
});
module.exports = __toCommonJS(installSkill_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
async function installSkill() {
const cwd = process.cwd();
const skillSource = import_path.default.join(__dirname, "SKILL.md");
const destDir = import_path.default.join(cwd, ".claude", "skills", "playwright-trace");
await import_fs.default.promises.mkdir(destDir, { recursive: true });
const destFile = import_path.default.join(destDir, "SKILL.md");
await import_fs.default.promises.copyFile(skillSource, destFile);
console.log(`\u2705 Skill installed to \`${import_path.default.relative(cwd, destFile)}\`.`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
installSkill
});
+142
View File
@@ -0,0 +1,142 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceActions_exports = {};
__export(traceActions_exports, {
traceAction: () => traceAction,
traceActions: () => traceActions
});
module.exports = __toCommonJS(traceActions_exports);
var import_traceModel = require("../../utils/isomorphic/trace/traceModel");
var import_locatorGenerators = require("../../utils/isomorphic/locatorGenerators");
var import_traceUtils = require("./traceUtils");
var import_formatUtils = require("../../utils/isomorphic/formatUtils");
async function traceActions(options) {
const trace = await (0, import_traceUtils.loadTrace)();
const actions = filterActions(trace.model.actions, options);
const { rootItem } = (0, import_traceModel.buildActionTree)(actions);
console.log(` ${"#".padStart(4)} ${"Time".padEnd(9)} ${"Action".padEnd(55)} ${"Duration".padStart(8)}`);
console.log(` ${"\u2500".repeat(4)} ${"\u2500".repeat(9)} ${"\u2500".repeat(55)} ${"\u2500".repeat(8)}`);
const visit = (item, indent) => {
const action = item.action;
const ordinal = trace.callIdToOrdinal.get(action.callId) ?? "?";
const ts = (0, import_traceUtils.formatTimestamp)(action.startTime, trace.model.startTime);
const duration = action.endTime ? (0, import_formatUtils.msToString)(action.endTime - action.startTime) : "running";
const title = (0, import_traceUtils.actionTitle)(action);
const locator = actionLocator(action);
const error = action.error ? " \u2717" : "";
const prefix = ` ${(ordinal + ".").padStart(4)} ${ts} ${indent}`;
console.log(`${prefix}${title.padEnd(Math.max(1, 55 - indent.length))} ${duration.padStart(8)}${error}`);
if (locator)
console.log(`${" ".repeat(prefix.length)}${locator}`);
for (const child of item.children)
visit(child, indent + " ");
};
for (const child of rootItem.children)
visit(child, "");
}
function filterActions(actions, options) {
let result = actions.filter((a) => a.group !== "configuration");
if (options.grep) {
const pattern = new RegExp(options.grep, "i");
result = result.filter((a) => pattern.test((0, import_traceUtils.actionTitle)(a)) || pattern.test(actionLocator(a) || ""));
}
if (options.errorsOnly)
result = result.filter((a) => !!a.error);
return result;
}
function actionLocator(action, sdkLanguage) {
return action.params.selector ? (0, import_locatorGenerators.asLocatorDescription)(sdkLanguage || "javascript", action.params.selector) : void 0;
}
async function traceAction(actionId) {
const trace = await (0, import_traceUtils.loadTrace)();
const action = trace.resolveActionId(actionId);
if (!action) {
console.error(`Action '${actionId}' not found. Use 'trace actions' to see available action IDs.`);
process.exitCode = 1;
return;
}
const title = (0, import_traceUtils.actionTitle)(action);
console.log(`
${title}
`);
console.log(" Time");
console.log(` start: ${(0, import_traceUtils.formatTimestamp)(action.startTime, trace.model.startTime)}`);
const duration = action.endTime ? (0, import_formatUtils.msToString)(action.endTime - action.startTime) : action.error ? "Timed Out" : "Running";
console.log(` duration: ${duration}`);
const paramKeys = Object.keys(action.params).filter((name) => name !== "info");
if (paramKeys.length) {
console.log("\n Parameters");
for (const key of paramKeys) {
const value = formatParamValue(action.params[key]);
console.log(` ${key}: ${value}`);
}
}
if (action.result) {
console.log("\n Return value");
for (const [key, value] of Object.entries(action.result))
console.log(` ${key}: ${formatParamValue(value)}`);
}
if (action.error) {
console.log("\n Error");
console.log(` ${action.error.message}`);
}
if (action.log.length) {
console.log("\n Log");
for (const entry of action.log) {
const time = entry.time !== -1 ? (0, import_traceUtils.formatTimestamp)(entry.time, trace.model.startTime) : "";
console.log(` ${time.padEnd(12)} ${entry.message}`);
}
}
if (action.stack?.length) {
console.log("\n Source");
for (const frame of action.stack.slice(0, 5)) {
const file = frame.file.replace(/.*[/\\](.*)/, "$1");
console.log(` ${file}:${frame.line}:${frame.column}`);
}
}
const snapshots = [];
if (action.beforeSnapshot)
snapshots.push("before");
if (action.inputSnapshot)
snapshots.push("input");
if (action.afterSnapshot)
snapshots.push("after");
if (snapshots.length) {
console.log("\n Snapshots");
console.log(` available: ${snapshots.join(", ")}`);
console.log(` usage: npx playwright trace snapshot ${actionId} --name <${snapshots.join("|")}>`);
}
console.log("");
}
function formatParamValue(value) {
if (value === void 0 || value === null)
return String(value);
if (typeof value === "string")
return `"${value}"`;
if (typeof value !== "object")
return String(value);
if (value.guid)
return "<handle>";
return JSON.stringify(value).slice(0, 1e3);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceAction,
traceActions
});
+69
View File
@@ -0,0 +1,69 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceAttachments_exports = {};
__export(traceAttachments_exports, {
traceAttachment: () => traceAttachment,
traceAttachments: () => traceAttachments
});
module.exports = __toCommonJS(traceAttachments_exports);
var import_traceUtils = require("./traceUtils");
async function traceAttachments() {
const trace = await (0, import_traceUtils.loadTrace)();
if (!trace.model.attachments.length) {
console.log(" No attachments");
return;
}
console.log(` ${"#".padStart(4)} ${"Name".padEnd(40)} ${"Content-Type".padEnd(30)} ${"Action".padEnd(8)}`);
console.log(` ${"\u2500".repeat(4)} ${"\u2500".repeat(40)} ${"\u2500".repeat(30)} ${"\u2500".repeat(8)}`);
for (let i = 0; i < trace.model.attachments.length; i++) {
const a = trace.model.attachments[i];
const actionOrdinal = trace.callIdToOrdinal.get(a.callId);
console.log(` ${(i + 1 + ".").padStart(4)} ${a.name.padEnd(40)} ${a.contentType.padEnd(30)} ${(actionOrdinal !== void 0 ? String(actionOrdinal) : a.callId).padEnd(8)}`);
}
}
async function traceAttachment(attachmentId, options) {
const trace = await (0, import_traceUtils.loadTrace)();
const ordinal = parseInt(attachmentId, 10);
const attachment = !isNaN(ordinal) && ordinal >= 1 && ordinal <= trace.model.attachments.length ? trace.model.attachments[ordinal - 1] : void 0;
if (!attachment) {
console.error(`Attachment '${attachmentId}' not found. Use 'trace attachments' to see available attachments.`);
process.exitCode = 1;
return;
}
let content;
if (attachment.sha1) {
const blob = await trace.loader.resourceForSha1(attachment.sha1);
if (blob)
content = Buffer.from(await blob.arrayBuffer());
} else if (attachment.base64) {
content = Buffer.from(attachment.base64, "base64");
}
if (!content) {
console.error(`Could not extract attachment content.`);
process.exitCode = 1;
return;
}
const outFile = await (0, import_traceUtils.saveOutputFile)(attachment.name, content, options.output);
console.log(` Attachment saved to ${outFile}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceAttachment,
traceAttachments
});
+87
View File
@@ -0,0 +1,87 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceCli_exports = {};
__export(traceCli_exports, {
addTraceCommands: () => addTraceCommands
});
module.exports = __toCommonJS(traceCli_exports);
function addTraceCommands(program, logErrorAndExit) {
const traceCommand = program.command("trace").description("inspect trace files from the command line");
traceCommand.command("open <trace>").description("extract trace file for inspection").action(async (trace) => {
const { traceOpen } = require("./traceOpen");
traceOpen(trace).catch(logErrorAndExit);
});
traceCommand.command("close").description("remove extracted trace data").action(async () => {
const { closeTrace } = require("./traceUtils");
closeTrace().catch(logErrorAndExit);
});
traceCommand.command("actions").description("list actions in the trace").option("--grep <pattern>", "filter actions by title pattern").option("--errors-only", "only show failed actions").action(async (options) => {
const { traceActions } = require("./traceActions");
traceActions(options).catch(logErrorAndExit);
});
traceCommand.command("action <action-id>").description("show details of a specific action").action(async (actionId) => {
const { traceAction } = require("./traceActions");
traceAction(actionId).catch(logErrorAndExit);
});
traceCommand.command("requests").description("show network requests").option("--grep <pattern>", "filter by URL pattern").option("--method <method>", "filter by HTTP method").option("--status <code>", "filter by status code").option("--failed", "only show failed requests (status >= 400)").action(async (options) => {
const { traceRequests } = require("./traceRequests");
traceRequests(options).catch(logErrorAndExit);
});
traceCommand.command("request <request-id>").description("show details of a specific network request").action(async (requestId) => {
const { traceRequest } = require("./traceRequests");
traceRequest(requestId).catch(logErrorAndExit);
});
traceCommand.command("console").description("show console messages").option("--errors-only", "only show errors").option("--warnings", "show errors and warnings").option("--browser", "only browser console messages").option("--stdio", "only stdout/stderr").action(async (options) => {
const { traceConsole } = require("./traceConsole");
traceConsole(options).catch(logErrorAndExit);
});
traceCommand.command("errors").description("show errors with stack traces").action(async () => {
const { traceErrors } = require("./traceErrors");
traceErrors().catch(logErrorAndExit);
});
traceCommand.command("snapshot <action-id>").description("run a playwright-cli command against a DOM snapshot").option("--name <name>", "snapshot phase: before, input, or after").option("--serve", "serve snapshot on localhost and keep running").allowUnknownOption(true).allowExcessArguments(true).action(async (actionId, options, cmd) => {
try {
const { traceSnapshot } = require("./traceSnapshot");
const browserArgs = cmd.args.slice(1);
await traceSnapshot(actionId, { ...options, browserArgs });
} catch (e) {
logErrorAndExit(e);
}
});
traceCommand.command("screenshot <action-id>").description("save screencast screenshot for an action").option("-o, --output <path>", "output file path").action(async (actionId, options) => {
const { traceScreenshot } = require("./traceScreenshot");
traceScreenshot(actionId, options).catch(logErrorAndExit);
});
traceCommand.command("attachments").description("list trace attachments").action(async () => {
const { traceAttachments } = require("./traceAttachments");
traceAttachments().catch(logErrorAndExit);
});
traceCommand.command("attachment <attachment-id>").description("extract a trace attachment by its number").option("-o, --output <path>", "output file path").action(async (attachmentId, options) => {
const { traceAttachment } = require("./traceAttachments");
traceAttachment(attachmentId, options).catch(logErrorAndExit);
});
traceCommand.command("install-skill").description("install SKILL.md for LLM integration").action(async () => {
const { installSkill } = require("./installSkill");
installSkill().catch(logErrorAndExit);
});
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addTraceCommands
});
+97
View File
@@ -0,0 +1,97 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceConsole_exports = {};
__export(traceConsole_exports, {
traceConsole: () => traceConsole
});
module.exports = __toCommonJS(traceConsole_exports);
var import_traceUtils = require("./traceUtils");
async function traceConsole(options) {
const trace = await (0, import_traceUtils.loadTrace)();
const model = trace.model;
const items = [];
for (const event of model.events) {
if (event.type === "console") {
if (options.stdio)
continue;
const level = event.messageType;
if (options.errorsOnly && level !== "error")
continue;
if (options.warnings && level !== "error" && level !== "warning")
continue;
const url = event.location.url;
const filename = url ? url.substring(url.lastIndexOf("/") + 1) : "<anonymous>";
items.push({
type: "browser",
level,
text: event.text,
location: `${filename}:${event.location.lineNumber}`,
timestamp: event.time
});
}
if (event.type === "event" && event.method === "pageError") {
if (options.stdio)
continue;
const error = event.params.error;
items.push({
type: "browser",
level: "error",
text: error?.error?.message || String(error?.value || ""),
timestamp: event.time
});
}
}
for (const event of model.stdio) {
if (options.browser)
continue;
if (options.errorsOnly && event.type !== "stderr")
continue;
if (options.warnings && event.type !== "stderr")
continue;
let text = "";
if (event.text)
text = event.text.trim();
if (event.base64)
text = Buffer.from(event.base64, "base64").toString("utf-8").trim();
if (!text)
continue;
items.push({
type: event.type,
level: event.type === "stderr" ? "error" : "info",
text,
timestamp: event.timestamp
});
}
items.sort((a, b) => a.timestamp - b.timestamp);
if (!items.length) {
console.log(" No console entries");
return;
}
for (const item of items) {
const ts = (0, import_traceUtils.formatTimestamp)(item.timestamp, model.startTime);
const source = item.type === "browser" ? "[browser]" : `[${item.type}]`;
const level = item.level.padEnd(8);
const location = item.location ? ` ${item.location}` : "";
console.log(` ${ts} ${source.padEnd(10)} ${level} ${item.text}${location}`);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceConsole
});
+55
View File
@@ -0,0 +1,55 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceErrors_exports = {};
__export(traceErrors_exports, {
traceErrors: () => traceErrors
});
module.exports = __toCommonJS(traceErrors_exports);
var import_traceUtils = require("./traceUtils");
async function traceErrors() {
const trace = await (0, import_traceUtils.loadTrace)();
const model = trace.model;
if (!model.errorDescriptors.length) {
console.log(" No errors");
return;
}
for (const error of model.errorDescriptors) {
if (error.action) {
const title = (0, import_traceUtils.actionTitle)(error.action);
console.log(`
\u2717 ${title}`);
} else {
console.log(`
\u2717 Error`);
}
if (error.stack?.length) {
const frame = error.stack[0];
const file = frame.file.replace(/.*[/\\](.*)/, "$1");
console.log(` at ${file}:${frame.line}:${frame.column}`);
}
console.log("");
const indented = error.message.split("\n").map((l) => ` ${l}`).join("\n");
console.log(indented);
}
console.log("");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceErrors
});
+69
View File
@@ -0,0 +1,69 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceOpen_exports = {};
__export(traceOpen_exports, {
traceOpen: () => traceOpen
});
module.exports = __toCommonJS(traceOpen_exports);
var import_traceUtils = require("./traceUtils");
var import_formatUtils = require("../../utils/isomorphic/formatUtils");
async function traceOpen(traceFile) {
await (0, import_traceUtils.openTrace)(traceFile);
await traceInfo();
}
async function traceInfo() {
const trace = await (0, import_traceUtils.loadTrace)();
const model = trace.model;
const info = {
browser: model.browserName || "unknown",
platform: model.platform || "unknown",
playwrightVersion: model.playwrightVersion || "unknown",
title: model.title || "",
duration: (0, import_formatUtils.msToString)(model.endTime - model.startTime),
durationMs: model.endTime - model.startTime,
startTime: model.wallTime ? new Date(model.wallTime).toISOString() : "unknown",
viewport: model.options.viewport ? `${model.options.viewport.width}x${model.options.viewport.height}` : "default",
actions: model.actions.length,
pages: model.pages.length,
network: model.resources.length,
errors: model.errorDescriptors.length,
attachments: model.attachments.length,
consoleMessages: model.events.filter((e) => e.type === "console").length
};
console.log("");
console.log(` Browser: ${info.browser}`);
console.log(` Platform: ${info.platform}`);
console.log(` Playwright: ${info.playwrightVersion}`);
if (info.title)
console.log(` Title: ${info.title}`);
console.log(` Duration: ${info.duration}`);
console.log(` Start time: ${info.startTime}`);
console.log(` Viewport: ${info.viewport}`);
console.log(` Actions: ${info.actions}`);
console.log(` Pages: ${info.pages}`);
console.log(` Network: ${info.network} requests`);
console.log(` Errors: ${info.errors}`);
console.log(` Attachments: ${info.attachments}`);
console.log(` Console: ${info.consoleMessages} messages`);
console.log("");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceOpen
});
+96
View File
@@ -0,0 +1,96 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceParser_exports = {};
__export(traceParser_exports, {
DirTraceLoaderBackend: () => DirTraceLoaderBackend,
extractTrace: () => extractTrace
});
module.exports = __toCommonJS(traceParser_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_zipFile = require("../../server/utils/zipFile");
class DirTraceLoaderBackend {
constructor(dir) {
this._dir = dir;
}
isLive() {
return false;
}
async entryNames() {
const entries = [];
const walk = async (dir, prefix) => {
const items = await import_fs.default.promises.readdir(dir, { withFileTypes: true });
for (const item of items) {
if (item.isDirectory())
await walk(import_path.default.join(dir, item.name), prefix ? `${prefix}/${item.name}` : item.name);
else
entries.push(prefix ? `${prefix}/${item.name}` : item.name);
}
};
await walk(this._dir, "");
return entries;
}
async hasEntry(entryName) {
try {
await import_fs.default.promises.access(import_path.default.join(this._dir, entryName));
return true;
} catch {
return false;
}
}
async readText(entryName) {
try {
return await import_fs.default.promises.readFile(import_path.default.join(this._dir, entryName), "utf-8");
} catch {
}
}
async readBlob(entryName) {
try {
const buffer = await import_fs.default.promises.readFile(import_path.default.join(this._dir, entryName));
return new Blob([new Uint8Array(buffer)]);
} catch {
}
}
}
async function extractTrace(traceFile, outDir) {
const zipFile = new import_zipFile.ZipFile(traceFile);
const entries = await zipFile.entries();
for (const entry of entries) {
const outPath = import_path.default.join(outDir, entry);
await import_fs.default.promises.mkdir(import_path.default.dirname(outPath), { recursive: true });
const buffer = await zipFile.read(entry);
await import_fs.default.promises.writeFile(outPath, buffer);
}
zipFile.close();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
DirTraceLoaderBackend,
extractTrace
});
+182
View File
@@ -0,0 +1,182 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceRequests_exports = {};
__export(traceRequests_exports, {
traceRequest: () => traceRequest,
traceRequests: () => traceRequests
});
module.exports = __toCommonJS(traceRequests_exports);
var import_path = __toESM(require("path"));
var import_traceUtils = require("./traceUtils");
var import_formatUtils = require("../../utils/isomorphic/formatUtils");
async function traceRequests(options) {
const trace = await (0, import_traceUtils.loadTrace)();
const model = trace.model;
let indexed = model.resources.map((r, i) => ({ resource: r, ordinal: i + 1 }));
if (options.grep) {
const pattern = new RegExp(options.grep, "i");
indexed = indexed.filter(({ resource: r }) => pattern.test(r.request.url));
}
if (options.method)
indexed = indexed.filter(({ resource: r }) => r.request.method.toLowerCase() === options.method.toLowerCase());
if (options.status) {
const code = parseInt(options.status, 10);
indexed = indexed.filter(({ resource: r }) => r.response.status === code);
}
if (options.failed)
indexed = indexed.filter(({ resource: r }) => r.response.status >= 400 || r.response.status === -1);
if (!indexed.length) {
console.log(" No network requests");
return;
}
console.log(` ${"#".padStart(4)} ${"Method".padEnd(8)} ${"Status".padEnd(8)} ${"Name".padEnd(45)} ${"Duration".padStart(10)} ${"Size".padStart(8)} ${"Route".padEnd(10)}`);
console.log(` ${"\u2500".repeat(4)} ${"\u2500".repeat(8)} ${"\u2500".repeat(8)} ${"\u2500".repeat(45)} ${"\u2500".repeat(10)} ${"\u2500".repeat(8)} ${"\u2500".repeat(10)}`);
for (const { resource: r, ordinal } of indexed) {
let name;
try {
const url = new URL(r.request.url);
name = url.pathname.substring(url.pathname.lastIndexOf("/") + 1);
if (!name)
name = url.host;
if (url.search)
name += url.search;
} catch {
name = r.request.url;
}
if (name.length > 45)
name = name.substring(0, 42) + "...";
const status = r.response.status > 0 ? String(r.response.status) : "ERR";
const size = r.response._transferSize > 0 ? r.response._transferSize : r.response.bodySize;
const route = formatRouteStatus(r);
console.log(` ${(ordinal + ".").padStart(4)} ${r.request.method.padEnd(8)} ${status.padEnd(8)} ${name.padEnd(45)} ${(0, import_formatUtils.msToString)(r.time).padStart(10)} ${bytesToString(size).padStart(8)} ${route.padEnd(10)}`);
}
}
async function traceRequest(requestId) {
const trace = await (0, import_traceUtils.loadTrace)();
const model = trace.model;
const ordinal = parseInt(requestId, 10);
const resource = !isNaN(ordinal) && ordinal >= 1 && ordinal <= model.resources.length ? model.resources[ordinal - 1] : void 0;
if (!resource) {
console.error(`Request '${requestId}' not found. Use 'trace requests' to see available request IDs.`);
process.exitCode = 1;
return;
}
const r = resource;
const status = r.response.status > 0 ? `${r.response.status} ${r.response.statusText}` : "ERR";
const size = r.response._transferSize > 0 ? r.response._transferSize : r.response.bodySize;
console.log(`
${r.request.method} ${r.request.url}
`);
console.log(" General");
console.log(` status: ${status}`);
console.log(` duration: ${(0, import_formatUtils.msToString)(r.time)}`);
console.log(` size: ${bytesToString(size)}`);
if (r.response.content.mimeType)
console.log(` type: ${r.response.content.mimeType}`);
const route = formatRouteStatus(r);
if (route)
console.log(` route: ${route}`);
if (r.serverIPAddress)
console.log(` server: ${r.serverIPAddress}${r._serverPort ? ":" + r._serverPort : ""}`);
if (r.response._failureText)
console.log(` error: ${r.response._failureText}`);
if (r.request.headers.length) {
console.log("\n Request headers");
for (const h of r.request.headers)
console.log(` ${h.name}: ${h.value}`);
}
if (r.request.postData) {
console.log("\n Request body");
const resource2 = r.request.postData._sha1 ?? r.request.postData._file;
if (resource2) {
console.log(` ${import_path.default.relative(process.cwd(), import_path.default.join(trace.model.traceUri, "resources", resource2))}`);
} else {
const text = r.request.postData.text.length > 2e3 ? r.request.postData.text.substring(0, 2e3) + "..." : r.request.postData.text;
console.log(` ${text}`);
}
}
if (r.response.headers.length) {
console.log("\n Response headers");
for (const h of r.response.headers)
console.log(` ${h.name}: ${h.value}`);
}
if (r.response.bodySize > 0) {
const resource2 = r.response.content._sha1 ?? r.response.content._file;
if (resource2) {
console.log("\n Response body");
console.log(` ${import_path.default.relative(process.cwd(), import_path.default.join(trace.model.traceUri, "resources", resource2))}`);
} else if (r.response.content.text) {
const text = r.response.content.text.length > 2e3 ? r.response.content.text.substring(0, 2e3) + "..." : r.response.content.text;
console.log("\n Response body");
console.log(` ${text}`);
}
}
if (r._securityDetails) {
console.log("\n Security");
if (r._securityDetails.protocol)
console.log(` protocol: ${r._securityDetails.protocol}`);
if (r._securityDetails.subjectName)
console.log(` subject: ${r._securityDetails.subjectName}`);
if (r._securityDetails.issuer)
console.log(` issuer: ${r._securityDetails.issuer}`);
}
console.log("");
}
function bytesToString(bytes) {
if (bytes < 0 || !isFinite(bytes))
return "-";
if (bytes === 0)
return "0";
if (bytes < 1e3)
return bytes.toFixed(0);
const kb = bytes / 1024;
if (kb < 1e3)
return kb.toFixed(1) + "K";
const mb = kb / 1024;
if (mb < 1e3)
return mb.toFixed(1) + "M";
const gb = mb / 1024;
return gb.toFixed(1) + "G";
}
function formatRouteStatus(r) {
if (r._wasAborted)
return "aborted";
if (r._wasContinued)
return "continued";
if (r._wasFulfilled)
return "fulfilled";
if (r._apiRequest)
return "api";
return "";
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceRequest,
traceRequests
});
+68
View File
@@ -0,0 +1,68 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceScreenshot_exports = {};
__export(traceScreenshot_exports, {
traceScreenshot: () => traceScreenshot
});
module.exports = __toCommonJS(traceScreenshot_exports);
var import_traceUtils = require("./traceUtils");
async function traceScreenshot(actionId, options) {
const trace = await (0, import_traceUtils.loadTrace)();
const action = trace.resolveActionId(actionId);
if (!action) {
console.error(`Action '${actionId}' not found.`);
process.exitCode = 1;
return;
}
const pageId = action.pageId;
if (!pageId) {
console.error(`Action '${actionId}' has no associated page.`);
process.exitCode = 1;
return;
}
const callId = action.callId;
const storage = trace.loader.storage();
const snapshotNames = ["input", "before", "after"];
let sha1;
for (const name of snapshotNames) {
const renderer = storage.snapshotByName(pageId, `${name}@${callId}`);
sha1 = renderer?.closestScreenshot();
if (sha1)
break;
}
if (!sha1) {
console.error(`No screenshot found for action '${actionId}'.`);
process.exitCode = 1;
return;
}
const blob = await trace.loader.resourceForSha1(sha1);
if (!blob) {
console.error(`Screenshot resource not found.`);
process.exitCode = 1;
return;
}
const defaultName = `screenshot-${actionId}.png`;
const buffer = Buffer.from(await blob.arrayBuffer());
const outFile = await (0, import_traceUtils.saveOutputFile)(defaultName, buffer, options.output);
console.log(` Screenshot saved to ${outFile}`);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceScreenshot
});
+149
View File
@@ -0,0 +1,149 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceSnapshot_exports = {};
__export(traceSnapshot_exports, {
traceSnapshot: () => traceSnapshot
});
module.exports = __toCommonJS(traceSnapshot_exports);
var import_browserBackend = require("../backend/browserBackend");
var import_tools = require("../backend/tools");
var playwright = __toESM(require("../../.."));
var import_utils = require("../../utils");
var import_command = require("../cli-daemon/command");
var import_minimist = require("../cli-client/minimist");
var import_commands = require("../cli-daemon/commands");
var import_traceUtils = require("./traceUtils");
async function traceSnapshot(actionId, options) {
const trace = await (0, import_traceUtils.loadTrace)();
const action = trace.resolveActionId(actionId);
if (!action) {
console.error(`Action '${actionId}' not found.`);
process.exitCode = 1;
return;
}
const pageId = action.pageId;
if (!pageId) {
console.error(`Action '${actionId}' has no associated page.`);
process.exitCode = 1;
return;
}
const callId = action.callId;
const storage = trace.loader.storage();
let snapshotName;
let renderer;
if (options.name) {
snapshotName = options.name;
renderer = storage.snapshotByName(pageId, `${snapshotName}@${callId}`);
} else {
for (const candidate of ["input", "before", "after"]) {
renderer = storage.snapshotByName(pageId, `${candidate}@${callId}`);
if (renderer) {
snapshotName = candidate;
break;
}
}
}
if (!renderer || !snapshotName) {
console.error(`No snapshot found for action '${actionId}'.`);
process.exitCode = 1;
return;
}
const snapshotKey = `${snapshotName}@${callId}`;
const server = await serveTraceSnapshot(storage, trace.loader, pageId, snapshotKey);
if (options.serve) {
console.log(`Serving snapshot at ${server.url}`);
await new Promise(() => {
});
return;
}
await runCommandOnSnapshot(server, options.browserArgs || []);
}
async function serveTraceSnapshot(storage, loader, pageId, snapshotKey) {
const { SnapshotServer } = require("../../utils/isomorphic/trace/snapshotServer");
const { HttpServer } = require("../../server/utils/httpServer");
const snapshotServer = new SnapshotServer(storage, (sha1) => loader.resourceForSha1(sha1));
const httpServer = new HttpServer();
httpServer.routePrefix("/snapshot", (request, response) => {
const url = new URL("http://localhost" + request.url);
const searchParams = url.searchParams;
searchParams.set("name", snapshotKey);
const snapshotResponse = snapshotServer.serveSnapshot(pageId, searchParams, "/snapshot");
response.statusCode = snapshotResponse.status;
snapshotResponse.headers.forEach((value, key) => response.setHeader(key, value));
snapshotResponse.text().then((text) => response.end(text));
return true;
});
httpServer.routePrefix("/", (_request, response) => {
response.statusCode = 302;
response.setHeader("Location", "/snapshot");
response.end();
return true;
});
await httpServer.start({ preferredPort: 0 });
return { url: httpServer.urlPrefix("human-readable"), stop: () => httpServer.stop() };
}
async function runCommandOnSnapshot(server, browserArgs) {
const browser = await playwright.chromium.launch({ headless: true });
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(server.url);
const backend = new import_browserBackend.BrowserBackend({
snapshot: { mode: "full" },
outputMode: "file",
skillMode: true
}, context, import_tools.browserTools);
await backend.initialize({ cwd: process.cwd() });
try {
if (!browserArgs.length)
browserArgs = ["snapshot"];
const args = (0, import_minimist.minimist)(browserArgs, { string: ["_"] });
const command = import_commands.commands[args._[0]];
if (!command)
throw new Error(`Unknown command: ${args._[0]}`);
const { toolName, toolParams } = (0, import_command.parseCommand)(command, args);
const result = await backend.callTool(toolName, toolParams);
const text = result.content[0]?.type === "text" ? result.content[0].text : void 0;
if (text)
console.log(text);
if (result.isError) {
console.error("Command failed.");
process.exitCode = 1;
}
} catch (e) {
console.error(e.message);
process.exitCode = 1;
} finally {
await server.stop().catch((e) => console.error(e));
await (0, import_utils.gracefullyCloseAll)();
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
traceSnapshot
});
+153
View File
@@ -0,0 +1,153 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var traceUtils_exports = {};
__export(traceUtils_exports, {
LoadedTrace: () => LoadedTrace,
actionTitle: () => actionTitle,
closeTrace: () => closeTrace,
formatTimestamp: () => formatTimestamp,
loadTrace: () => loadTrace,
openTrace: () => openTrace,
saveOutputFile: () => saveOutputFile
});
module.exports = __toCommonJS(traceUtils_exports);
var import_fs = __toESM(require("fs"));
var import_path = __toESM(require("path"));
var import_traceModel = require("../../utils/isomorphic/trace/traceModel");
var import_traceLoader = require("../../utils/isomorphic/trace/traceLoader");
var import_protocolFormatter = require("../../utils/isomorphic/protocolFormatter");
var import_traceParser = require("./traceParser");
const traceDir = import_path.default.join(".playwright-cli", "trace");
const cliOutputDir = ".playwright-cli";
class LoadedTrace {
constructor(model, loader, ordinals) {
this.model = model;
this.loader = loader;
this.ordinalToCallId = ordinals.ordinalToCallId;
this.callIdToOrdinal = ordinals.callIdToOrdinal;
}
resolveActionId(actionId) {
const ordinal = parseInt(actionId, 10);
if (!isNaN(ordinal)) {
const callId = this.ordinalToCallId.get(ordinal);
if (callId)
return this.model.actions.find((a) => a.callId === callId);
}
return this.model.actions.find((a) => a.callId === actionId);
}
}
function ensureTraceOpen() {
if (!import_fs.default.existsSync(traceDir))
throw new Error(`No trace opened. Run 'npx playwright trace open <file>' first.`);
return traceDir;
}
async function closeTrace() {
if (import_fs.default.existsSync(traceDir))
await import_fs.default.promises.rm(traceDir, { recursive: true });
}
async function openTrace(traceFile) {
const filePath = import_path.default.resolve(traceFile);
if (!import_fs.default.existsSync(filePath))
throw new Error(`Trace file not found: ${filePath}`);
await closeTrace();
await import_fs.default.promises.mkdir(traceDir, { recursive: true });
if (filePath.endsWith(".zip"))
await (0, import_traceParser.extractTrace)(filePath, traceDir);
else
await import_fs.default.promises.writeFile(import_path.default.join(traceDir, ".link"), filePath, "utf-8");
}
async function loadTrace() {
const dir = ensureTraceOpen();
const linkFile = import_path.default.join(dir, ".link");
let traceDir2;
let traceFile;
if (import_fs.default.existsSync(linkFile)) {
const tracePath = await import_fs.default.promises.readFile(linkFile, "utf-8");
traceDir2 = import_path.default.dirname(tracePath);
traceFile = import_path.default.basename(tracePath);
} else {
traceDir2 = dir;
}
const backend = new import_traceParser.DirTraceLoaderBackend(traceDir2);
const loader = new import_traceLoader.TraceLoader();
await loader.load(backend, traceFile);
const model = new import_traceModel.TraceModel(traceDir2, loader.contextEntries);
return new LoadedTrace(model, loader, buildOrdinalMap(model));
}
function formatTimestamp(ms, base) {
const relative = ms - base;
if (relative < 0)
return "0:00.000";
const totalMs = Math.floor(relative);
const minutes = Math.floor(totalMs / 6e4);
const seconds = Math.floor(totalMs % 6e4 / 1e3);
const millis = totalMs % 1e3;
return `${minutes}:${seconds.toString().padStart(2, "0")}.${millis.toString().padStart(3, "0")}`;
}
function actionTitle(action) {
return (0, import_protocolFormatter.renderTitleForCall)({ ...action, type: action.class }) || `${action.class}.${action.method}`;
}
async function saveOutputFile(fileName, content, explicitOutput) {
let outFile;
if (explicitOutput) {
outFile = explicitOutput;
} else {
await import_fs.default.promises.mkdir(cliOutputDir, { recursive: true });
outFile = import_path.default.join(cliOutputDir, fileName);
}
await import_fs.default.promises.writeFile(outFile, content);
return outFile;
}
function buildOrdinalMap(model) {
const actions = model.actions.filter((a) => a.group !== "configuration");
const { rootItem } = (0, import_traceModel.buildActionTree)(actions);
const ordinalToCallId = /* @__PURE__ */ new Map();
const callIdToOrdinal = /* @__PURE__ */ new Map();
let ordinal = 1;
const visit = (item) => {
ordinalToCallId.set(ordinal, item.action.callId);
callIdToOrdinal.set(item.action.callId, ordinal);
ordinal++;
for (const child of item.children)
visit(child);
};
for (const child of rootItem.children)
visit(child);
return { ordinalToCallId, callIdToOrdinal };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
LoadedTrace,
actionTitle,
closeTrace,
formatTimestamp,
loadTrace,
openTrace,
saveOutputFile
});
+32
View File
@@ -0,0 +1,32 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var connect_exports = {};
__export(connect_exports, {
connectToBrowserAcrossVersions: () => connectToBrowserAcrossVersions
});
module.exports = __toCommonJS(connect_exports);
async function connectToBrowserAcrossVersions(descriptor) {
const pw = require(descriptor.playwrightLib);
const browserType = pw[descriptor.browser.browserName];
return await browserType.connect(descriptor.endpoint ?? descriptor.pipeName);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
connectToBrowserAcrossVersions
});
+152
View File
@@ -0,0 +1,152 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var http_exports = {};
__export(http_exports, {
addressToString: () => addressToString,
startMcpHttpServer: () => startMcpHttpServer
});
module.exports = __toCommonJS(http_exports);
var import_assert = __toESM(require("assert"));
var import_crypto = __toESM(require("crypto"));
var import_utilsBundle = require("../../../utilsBundle");
var mcpBundle = __toESM(require("../../../mcpBundle"));
var import_network = require("../../../server/utils/network");
var mcpServer = __toESM(require("./server"));
const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
async function startMcpHttpServer(config, serverBackendFactory, allowedHosts) {
const httpServer = (0, import_network.createHttpServer)();
await (0, import_network.startHttpServer)(httpServer, config);
return await installHttpTransport(httpServer, serverBackendFactory, allowedHosts);
}
function addressToString(address, options) {
(0, import_assert.default)(address, "Could not bind server socket");
if (typeof address === "string")
throw new Error("Unexpected address type: " + address);
let host = address.family === "IPv4" ? address.address : `[${address.address}]`;
if (options.normalizeLoopback && (host === "0.0.0.0" || host === "[::]" || host === "[::1]" || host === "127.0.0.1"))
host = "localhost";
return `${options.protocol}://${host}:${address.port}`;
}
async function installHttpTransport(httpServer, serverBackendFactory, allowedHosts) {
const url = addressToString(httpServer.address(), { protocol: "http", normalizeLoopback: true });
const host = new URL(url).host;
allowedHosts = (allowedHosts || [host]).map((h) => h.toLowerCase());
const allowAnyHost = allowedHosts.includes("*");
const sseSessions = /* @__PURE__ */ new Map();
const streamableSessions = /* @__PURE__ */ new Map();
httpServer.on("request", async (req, res) => {
if (!allowAnyHost) {
const host2 = req.headers.host?.toLowerCase();
if (!host2) {
res.statusCode = 400;
return res.end("Missing host");
}
if (!allowedHosts.includes(host2)) {
res.statusCode = 403;
return res.end("Access is only allowed at " + allowedHosts.join(", "));
}
}
const url2 = new URL(`http://localhost${req.url}`);
if (url2.pathname === "/killkillkill" && req.method === "GET") {
res.statusCode = 200;
res.end("Killing process");
process.emit("SIGINT");
return;
}
if (url2.pathname.startsWith("/sse"))
await handleSSE(serverBackendFactory, req, res, url2, sseSessions);
else
await handleStreamable(serverBackendFactory, req, res, streamableSessions);
});
return url;
}
async function handleSSE(serverBackendFactory, req, res, url, sessions) {
if (req.method === "POST") {
const sessionId = url.searchParams.get("sessionId");
if (!sessionId) {
res.statusCode = 400;
return res.end("Missing sessionId");
}
const transport = sessions.get(sessionId);
if (!transport) {
res.statusCode = 404;
return res.end("Session not found");
}
return await transport.handlePostMessage(req, res);
} else if (req.method === "GET") {
const transport = new mcpBundle.SSEServerTransport("/sse", res);
sessions.set(transport.sessionId, transport);
testDebug(`create SSE session`);
await mcpServer.connect(serverBackendFactory, transport, false);
res.on("close", () => {
testDebug(`delete SSE session`);
sessions.delete(transport.sessionId);
});
return;
}
res.statusCode = 405;
res.end("Method not allowed");
}
async function handleStreamable(serverBackendFactory, req, res, sessions) {
const sessionId = req.headers["mcp-session-id"];
if (sessionId) {
const transport = sessions.get(sessionId);
if (!transport) {
res.statusCode = 404;
res.end("Session not found");
return;
}
return await transport.handleRequest(req, res);
}
if (req.method === "POST") {
const transport = new mcpBundle.StreamableHTTPServerTransport({
sessionIdGenerator: () => import_crypto.default.randomUUID(),
onsessioninitialized: async (sessionId2) => {
testDebug(`create http session`);
await mcpServer.connect(serverBackendFactory, transport, true);
sessions.set(sessionId2, transport);
}
});
transport.onclose = () => {
if (!transport.sessionId)
return;
sessions.delete(transport.sessionId);
testDebug(`delete http session`);
};
await transport.handleRequest(req, res);
return;
}
res.statusCode = 400;
res.end("Invalid request");
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
addressToString,
startMcpHttpServer
});
+230
View File
@@ -0,0 +1,230 @@
"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var server_exports = {};
__export(server_exports, {
allRootPaths: () => allRootPaths,
connect: () => connect,
createServer: () => createServer,
firstRootPath: () => firstRootPath,
start: () => start
});
module.exports = __toCommonJS(server_exports);
var import_url = require("url");
var import_utilsBundle = require("../../../utilsBundle");
var mcpBundle = __toESM(require("../../../mcpBundle"));
var import_http = require("./http");
var import_tool = require("./tool");
const serverDebug = (0, import_utilsBundle.debug)("pw:mcp:server");
const serverDebugResponse = (0, import_utilsBundle.debug)("pw:mcp:server:response");
class BackendManager {
constructor() {
this._backends = /* @__PURE__ */ new Map();
}
async createBackend(factory, clientInfo) {
const backend = await factory.create(clientInfo);
await backend.initialize?.(clientInfo);
this._backends.set(backend, factory);
return backend;
}
async disposeBackend(backend) {
const factory = this._backends.get(backend);
if (!factory)
return;
await backend.dispose?.();
await factory.disposed(backend).catch(serverDebug);
this._backends.delete(backend);
}
}
const backendManager = new BackendManager();
async function connect(factory, transport, runHeartbeat) {
const server = createServer(factory.name, factory.version, factory, runHeartbeat);
await server.connect(transport);
}
function createServer(name, version, factory, runHeartbeat) {
const server = new mcpBundle.Server({ name, version }, {
capabilities: {
tools: {}
}
});
server.setRequestHandler(mcpBundle.ListToolsRequestSchema, async () => {
serverDebug("listTools");
return { tools: factory.toolSchemas.map((s) => (0, import_tool.toMcpTool)(s)) };
});
let backendPromise;
const onClose = () => backendPromise?.then((b) => backendManager.disposeBackend(b)).catch(serverDebug);
addServerListener(server, "close", onClose);
server.setRequestHandler(mcpBundle.CallToolRequestSchema, async (request, extra) => {
serverDebug("callTool", request);
const progressToken = request.params._meta?.progressToken;
let progressCounter = 0;
const progress = progressToken ? (params) => {
extra.sendNotification({
method: "notifications/progress",
params: {
progressToken,
progress: params.progress ?? ++progressCounter,
total: params.total,
message: params.message
}
}).catch((e) => serverDebug("notification", e));
} : () => {
};
try {
if (!backendPromise) {
backendPromise = initializeServer(server, factory, runHeartbeat).catch((e) => {
backendPromise = void 0;
throw e;
});
}
const backend = await backendPromise;
const toolResult = await backend.callTool(request.params.name, request.params.arguments || {}, progress);
if (toolResult.isClose) {
await backendManager.disposeBackend(backend).catch(serverDebug);
backendPromise = void 0;
delete toolResult.isClose;
}
const mergedResult = mergeTextParts(toolResult);
serverDebugResponse("callResult", mergedResult);
return mergedResult;
} catch (error) {
return {
content: [{ type: "text", text: "### Error\n" + String(error) }],
isError: true
};
}
});
return server;
}
const initializeServer = async (server, factory, runHeartbeat) => {
const capabilities = server.getClientCapabilities();
let clientRoots = [];
if (capabilities?.roots) {
const { roots } = await server.listRoots().catch((e) => {
serverDebug(e);
return { roots: [] };
});
clientRoots = roots;
}
const clientInfo = {
cwd: firstRootPath(clientRoots)
};
const backend = await backendManager.createBackend(factory, clientInfo);
if (runHeartbeat)
startHeartbeat(server);
return backend;
};
const startHeartbeat = (server) => {
const beat = () => {
Promise.race([
server.ping(),
new Promise((_, reject) => setTimeout(() => reject(new Error("ping timeout")), 5e3))
]).then(() => {
setTimeout(beat, 3e3);
}).catch(() => {
void server.close();
});
};
beat();
};
function addServerListener(server, event, listener) {
const oldListener = server[`on${event}`];
server[`on${event}`] = () => {
oldListener?.();
listener();
};
}
async function start(serverBackendFactory, options = {}) {
if (options.port === void 0) {
await connect(serverBackendFactory, new mcpBundle.StdioServerTransport(), false);
return;
}
const url = await (0, import_http.startMcpHttpServer)(options, serverBackendFactory, options.allowedHosts);
const mcpConfig = { mcpServers: {} };
mcpConfig.mcpServers[serverBackendFactory.nameInConfig] = {
url: `${url}/mcp`
};
const message = [
`Listening on ${url}`,
"Put this in your client config:",
JSON.stringify(mcpConfig, void 0, 2),
"For legacy SSE transport support, you can use the /sse endpoint instead."
].join("\n");
console.error(message);
}
function firstRootPath(roots) {
return allRootPaths(roots)[0];
}
function allRootPaths(roots) {
const paths = [];
for (const root of roots) {
const url = new URL(root.uri);
let rootPath;
try {
rootPath = (0, import_url.fileURLToPath)(url);
} catch (e) {
if (e.code === "ERR_INVALID_FILE_URL_PATH" && process.platform === "win32")
rootPath = decodeURIComponent(url.pathname);
}
if (!rootPath)
continue;
paths.push(rootPath);
}
if (paths.length === 0)
paths.push(process.cwd());
return paths;
}
function mergeTextParts(result) {
const content = [];
const testParts = [];
for (const part of result.content) {
if (part.type === "text") {
testParts.push(part.text);
continue;
}
if (testParts.length > 0) {
content.push({ type: "text", text: testParts.join("\n") });
testParts.length = 0;
}
content.push(part);
}
if (testParts.length > 0)
content.push({ type: "text", text: testParts.join("\n") });
return {
...result,
content
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
allRootPaths,
connect,
createServer,
firstRootPath,
start
});
+47
View File
@@ -0,0 +1,47 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var tool_exports = {};
__export(tool_exports, {
defineToolSchema: () => defineToolSchema,
toMcpTool: () => toMcpTool
});
module.exports = __toCommonJS(tool_exports);
var import_zodBundle = require("../../../zodBundle");
function toMcpTool(tool) {
const readOnly = tool.type === "readOnly" || tool.type === "assertion";
return {
name: tool.name,
description: tool.description,
inputSchema: import_zodBundle.z.toJSONSchema(tool.inputSchema),
annotations: {
title: tool.title,
readOnlyHint: readOnly,
destructiveHint: !readOnly,
openWorldHint: true
}
};
}
function defineToolSchema(tool) {
return tool;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
defineToolSchema,
toMcpTool
});
+108
View File
@@ -0,0 +1,108 @@
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var socketConnection_exports = {};
__export(socketConnection_exports, {
SocketConnection: () => SocketConnection,
compareSemver: () => compareSemver
});
module.exports = __toCommonJS(socketConnection_exports);
class SocketConnection {
constructor(socket) {
this._pendingBuffers = [];
this._socket = socket;
socket.on("data", (buffer) => this._onData(buffer));
socket.on("close", () => {
this.onclose?.();
});
socket.on("error", (e) => console.error(`error: ${e.message}`));
}
async send(message) {
await new Promise((resolve, reject) => {
this._socket.write(`${JSON.stringify(message)}
`, (error) => {
if (error)
reject(error);
else
resolve(void 0);
});
});
}
close() {
this._socket.destroy();
}
_onData(buffer) {
let end = buffer.indexOf("\n");
if (end === -1) {
this._pendingBuffers.push(buffer);
return;
}
this._pendingBuffers.push(buffer.slice(0, end));
const message = Buffer.concat(this._pendingBuffers).toString();
this._dispatchMessage(message);
let start = end + 1;
end = buffer.indexOf("\n", start);
while (end !== -1) {
const message2 = buffer.toString(void 0, start, end);
this._dispatchMessage(message2);
start = end + 1;
end = buffer.indexOf("\n", start);
}
this._pendingBuffers = [buffer.slice(start)];
}
_dispatchMessage(message) {
try {
this.onmessage?.(JSON.parse(message));
} catch (e) {
console.error("failed to dispatch message", e);
}
}
}
function compareSemver(a, b) {
const aBase = a.replace(/-.*$/, "");
const bBase = b.replace(/-.*$/, "");
const aParts = aBase.split(".").map(Number);
const bParts = bBase.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if (aParts[i] > bParts[i])
return 1;
if (aParts[i] < bParts[i])
return -1;
}
const aTimestamp = parseSuffixTimestamp(a);
const bTimestamp = parseSuffixTimestamp(b);
if (aTimestamp > bTimestamp)
return 1;
if (aTimestamp < bTimestamp)
return -1;
return 0;
}
function parseSuffixTimestamp(version) {
const match = version.match(/^\d+\.\d+\.\d+-(?:alpha|beta)-(.+)$/);
if (!match)
return Infinity;
const suffix = match[1];
if (/^\d{4}-\d{2}-\d{2}$/.test(suffix))
return new Date(suffix).getTime();
return Number(suffix);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SocketConnection,
compareSemver
});