patchright-bun 1.58.1 → 1.59.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/README.md +225 -225
  2. package/ThirdPartyNotices.txt +3918 -5041
  3. package/cli.js +19 -19
  4. package/index.d.ts +17 -17
  5. package/index.js +17 -17
  6. package/index.mjs +18 -18
  7. package/jsx-runtime.js +42 -42
  8. package/jsx-runtime.mjs +21 -21
  9. package/lib/agents/agentParser.js +89 -89
  10. package/lib/agents/copilot-setup-steps.yml +34 -34
  11. package/lib/agents/generateAgents.js +346 -348
  12. package/lib/agents/playwright-test-coverage.prompt.md +31 -31
  13. package/lib/agents/playwright-test-generate.prompt.md +8 -8
  14. package/lib/agents/playwright-test-generator.agent.md +88 -88
  15. package/lib/agents/playwright-test-heal.prompt.md +6 -6
  16. package/lib/agents/playwright-test-healer.agent.md +55 -55
  17. package/lib/agents/playwright-test-plan.prompt.md +9 -9
  18. package/lib/agents/playwright-test-planner.agent.md +73 -73
  19. package/lib/common/config.js +281 -282
  20. package/lib/common/configLoader.js +344 -344
  21. package/lib/common/esmLoaderHost.js +104 -104
  22. package/lib/common/expectBundle.js +28 -28
  23. package/lib/common/expectBundleImpl.js +407 -407
  24. package/lib/common/fixtures.js +302 -302
  25. package/lib/common/globals.js +58 -58
  26. package/lib/common/ipc.js +60 -60
  27. package/lib/common/poolBuilder.js +85 -85
  28. package/lib/common/process.js +133 -132
  29. package/lib/common/suiteUtils.js +140 -140
  30. package/lib/common/test.js +330 -321
  31. package/lib/common/testLoader.js +101 -101
  32. package/lib/common/testType.js +301 -298
  33. package/lib/common/validators.js +68 -68
  34. package/lib/errorContext.js +121 -0
  35. package/lib/fsWatcher.js +67 -67
  36. package/lib/index.js +764 -726
  37. package/lib/internalsForTest.js +42 -42
  38. package/lib/isomorphic/events.js +77 -77
  39. package/lib/isomorphic/folders.js +30 -30
  40. package/lib/isomorphic/stringInternPool.js +69 -69
  41. package/lib/isomorphic/teleReceiver.js +538 -521
  42. package/lib/isomorphic/teleSuiteUpdater.js +157 -157
  43. package/lib/isomorphic/testServerConnection.js +223 -225
  44. package/lib/isomorphic/testServerInterface.js +16 -16
  45. package/lib/isomorphic/testTree.js +329 -329
  46. package/lib/isomorphic/types.d.js +16 -16
  47. package/lib/loader/loaderMain.js +59 -59
  48. package/lib/matchers/expect.js +311 -311
  49. package/lib/matchers/matcherHint.js +44 -44
  50. package/lib/matchers/matchers.js +385 -383
  51. package/lib/matchers/toBeTruthy.js +75 -75
  52. package/lib/matchers/toEqual.js +100 -100
  53. package/lib/matchers/toHaveURL.js +101 -101
  54. package/lib/matchers/toMatchAriaSnapshot.js +163 -159
  55. package/lib/matchers/toMatchSnapshot.js +349 -342
  56. package/lib/matchers/toMatchText.js +99 -99
  57. package/lib/mcp/test/browserBackend.js +121 -98
  58. package/lib/mcp/test/generatorTools.js +122 -122
  59. package/lib/mcp/test/plannerTools.js +145 -145
  60. package/lib/mcp/test/seed.js +82 -82
  61. package/lib/mcp/test/streams.js +44 -44
  62. package/lib/mcp/test/testBackend.js +99 -99
  63. package/lib/mcp/test/testContext.js +283 -285
  64. package/lib/mcp/test/testTool.js +30 -30
  65. package/lib/mcp/test/testTools.js +108 -108
  66. package/lib/plugins/gitCommitInfoPlugin.js +198 -198
  67. package/lib/plugins/index.js +28 -28
  68. package/lib/plugins/webServerPlugin.js +238 -237
  69. package/lib/program.js +239 -417
  70. package/lib/reportActions.js +80 -0
  71. package/lib/reporters/base.js +633 -634
  72. package/lib/reporters/blob.js +138 -138
  73. package/lib/reporters/dot.js +99 -99
  74. package/lib/reporters/empty.js +32 -32
  75. package/lib/reporters/github.js +127 -128
  76. package/lib/reporters/html.js +666 -633
  77. package/lib/reporters/internalReporter.js +138 -138
  78. package/lib/reporters/json.js +254 -254
  79. package/lib/reporters/junit.js +321 -232
  80. package/lib/reporters/line.js +131 -131
  81. package/lib/reporters/list.js +252 -253
  82. package/lib/reporters/listModeReporter.js +69 -69
  83. package/lib/reporters/markdown.js +144 -144
  84. package/lib/reporters/merge.js +579 -558
  85. package/lib/reporters/multiplexer.js +116 -112
  86. package/lib/reporters/reporterV2.js +102 -102
  87. package/lib/reporters/teleEmitter.js +319 -317
  88. package/lib/reporters/versions/blobV1.js +16 -16
  89. package/lib/runner/dispatcher.js +522 -530
  90. package/lib/runner/failureTracker.js +72 -72
  91. package/lib/runner/lastRun.js +77 -77
  92. package/lib/runner/loadUtils.js +340 -334
  93. package/lib/runner/loaderHost.js +89 -89
  94. package/lib/runner/processHost.js +180 -180
  95. package/lib/runner/projectUtils.js +241 -241
  96. package/lib/runner/rebase.js +189 -189
  97. package/lib/runner/reporters.js +143 -138
  98. package/lib/runner/sigIntWatcher.js +96 -96
  99. package/lib/runner/taskRunner.js +127 -127
  100. package/lib/runner/tasks.js +413 -410
  101. package/lib/runner/testGroups.js +125 -125
  102. package/lib/runner/testRunner.js +398 -398
  103. package/lib/runner/testServer.js +269 -269
  104. package/lib/runner/uiModeReporter.js +30 -30
  105. package/lib/runner/vcs.js +72 -72
  106. package/lib/runner/watchMode.js +396 -396
  107. package/lib/runner/workerHost.js +101 -104
  108. package/lib/testActions.js +220 -0
  109. package/lib/third_party/pirates.js +62 -62
  110. package/lib/third_party/tsconfig-loader.js +103 -103
  111. package/lib/transform/babelBundle.js +43 -46
  112. package/lib/transform/babelBundleImpl.js +461 -461
  113. package/lib/transform/compilationCache.js +272 -274
  114. package/lib/transform/esmLoader.js +105 -103
  115. package/lib/transform/portTransport.js +67 -67
  116. package/lib/transform/transform.js +296 -303
  117. package/lib/util.js +400 -400
  118. package/lib/utilsBundle.js +43 -50
  119. package/lib/utilsBundleImpl.js +100 -103
  120. package/lib/worker/fixtureRunner.js +262 -262
  121. package/lib/worker/testInfo.js +532 -536
  122. package/lib/worker/testTracing.js +351 -345
  123. package/lib/worker/timeoutManager.js +185 -174
  124. package/lib/worker/util.js +31 -31
  125. package/lib/worker/workerMain.js +533 -530
  126. package/package.json +2 -6
  127. package/test.d.ts +18 -18
  128. package/test.js +24 -24
  129. package/test.mjs +34 -34
  130. package/types/test.d.ts +10322 -10251
  131. package/types/testReporter.d.ts +822 -822
  132. package/lib/mcp/browser/browserContextFactory.js +0 -329
  133. package/lib/mcp/browser/browserServerBackend.js +0 -84
  134. package/lib/mcp/browser/config.js +0 -421
  135. package/lib/mcp/browser/context.js +0 -244
  136. package/lib/mcp/browser/response.js +0 -278
  137. package/lib/mcp/browser/sessionLog.js +0 -75
  138. package/lib/mcp/browser/tab.js +0 -343
  139. package/lib/mcp/browser/tools/common.js +0 -65
  140. package/lib/mcp/browser/tools/console.js +0 -46
  141. package/lib/mcp/browser/tools/dialogs.js +0 -60
  142. package/lib/mcp/browser/tools/evaluate.js +0 -61
  143. package/lib/mcp/browser/tools/files.js +0 -58
  144. package/lib/mcp/browser/tools/form.js +0 -63
  145. package/lib/mcp/browser/tools/install.js +0 -72
  146. package/lib/mcp/browser/tools/keyboard.js +0 -107
  147. package/lib/mcp/browser/tools/mouse.js +0 -107
  148. package/lib/mcp/browser/tools/navigate.js +0 -71
  149. package/lib/mcp/browser/tools/network.js +0 -63
  150. package/lib/mcp/browser/tools/open.js +0 -57
  151. package/lib/mcp/browser/tools/pdf.js +0 -49
  152. package/lib/mcp/browser/tools/runCode.js +0 -78
  153. package/lib/mcp/browser/tools/screenshot.js +0 -93
  154. package/lib/mcp/browser/tools/snapshot.js +0 -173
  155. package/lib/mcp/browser/tools/tabs.js +0 -67
  156. package/lib/mcp/browser/tools/tool.js +0 -47
  157. package/lib/mcp/browser/tools/tracing.js +0 -74
  158. package/lib/mcp/browser/tools/utils.js +0 -94
  159. package/lib/mcp/browser/tools/verify.js +0 -143
  160. package/lib/mcp/browser/tools/wait.js +0 -63
  161. package/lib/mcp/browser/tools.js +0 -84
  162. package/lib/mcp/browser/watchdog.js +0 -44
  163. package/lib/mcp/config.d.js +0 -16
  164. package/lib/mcp/extension/cdpRelay.js +0 -351
  165. package/lib/mcp/extension/extensionContextFactory.js +0 -76
  166. package/lib/mcp/extension/protocol.js +0 -28
  167. package/lib/mcp/index.js +0 -61
  168. package/lib/mcp/log.js +0 -35
  169. package/lib/mcp/program.js +0 -111
  170. package/lib/mcp/sdk/exports.js +0 -28
  171. package/lib/mcp/sdk/http.js +0 -152
  172. package/lib/mcp/sdk/inProcessTransport.js +0 -71
  173. package/lib/mcp/sdk/server.js +0 -223
  174. package/lib/mcp/sdk/tool.js +0 -47
  175. package/lib/mcp/terminal/cli.js +0 -296
  176. package/lib/mcp/terminal/command.js +0 -56
  177. package/lib/mcp/terminal/commands.js +0 -333
  178. package/lib/mcp/terminal/daemon.js +0 -129
  179. package/lib/mcp/terminal/help.json +0 -32
  180. package/lib/mcp/terminal/helpGenerator.js +0 -88
  181. package/lib/mcp/terminal/socketConnection.js +0 -80
  182. package/lib/runner/storage.js +0 -91
  183. package/lib/transform/md.js +0 -221
@@ -1,421 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var config_exports = {};
30
- __export(config_exports, {
31
- commaSeparatedList: () => commaSeparatedList,
32
- configFromCLIOptions: () => configFromCLIOptions,
33
- defaultConfig: () => defaultConfig,
34
- dotenvFileLoader: () => dotenvFileLoader,
35
- enumParser: () => enumParser,
36
- headerParser: () => headerParser,
37
- numberParser: () => numberParser,
38
- outputDir: () => outputDir,
39
- outputFile: () => outputFile,
40
- resolutionParser: () => resolutionParser,
41
- resolveCLIConfig: () => resolveCLIConfig,
42
- resolveConfig: () => resolveConfig,
43
- semicolonSeparatedList: () => semicolonSeparatedList
44
- });
45
- module.exports = __toCommonJS(config_exports);
46
- var import_fs = __toESM(require("fs"));
47
- var import_os = __toESM(require("os"));
48
- var import_path = __toESM(require("path"));
49
- var import_playwright_core = require("patchright-core");
50
- var import_utilsBundle = require("patchright-core/lib/utilsBundle");
51
- var import_util = require("../../util");
52
- var import_server = require("../sdk/server");
53
- const defaultConfig = {
54
- browser: {
55
- browserName: "chromium",
56
- launchOptions: {
57
- channel: "chrome",
58
- headless: import_os.default.platform() === "linux" && !process.env.DISPLAY,
59
- chromiumSandbox: true
60
- },
61
- contextOptions: {
62
- viewport: null
63
- }
64
- },
65
- console: {
66
- level: "info"
67
- },
68
- network: {
69
- allowedOrigins: void 0,
70
- blockedOrigins: void 0
71
- },
72
- server: {},
73
- saveTrace: false,
74
- snapshot: {
75
- mode: "incremental",
76
- output: "stdout"
77
- },
78
- timeouts: {
79
- action: 5e3,
80
- navigation: 6e4
81
- }
82
- };
83
- async function resolveConfig(config) {
84
- return mergeConfig(defaultConfig, config);
85
- }
86
- async function resolveCLIConfig(cliOptions) {
87
- const configInFile = await loadConfig(cliOptions.config);
88
- const envOverrides = configFromEnv();
89
- const cliOverrides = configFromCLIOptions(cliOptions);
90
- let result = defaultConfig;
91
- result = mergeConfig(result, configInFile);
92
- result = mergeConfig(result, envOverrides);
93
- result = mergeConfig(result, cliOverrides);
94
- await validateConfig(result);
95
- return result;
96
- }
97
- async function validateConfig(config) {
98
- if (config.browser.initScript) {
99
- for (const script of config.browser.initScript) {
100
- if (!await (0, import_util.fileExistsAsync)(script))
101
- throw new Error(`Init script file does not exist: ${script}`);
102
- }
103
- }
104
- if (config.browser.initPage) {
105
- for (const page of config.browser.initPage) {
106
- if (!await (0, import_util.fileExistsAsync)(page))
107
- throw new Error(`Init page file does not exist: ${page}`);
108
- }
109
- }
110
- if (config.sharedBrowserContext && config.saveVideo)
111
- throw new Error("saveVideo is not supported when sharedBrowserContext is true");
112
- }
113
- function configFromCLIOptions(cliOptions) {
114
- let browserName;
115
- let channel;
116
- switch (cliOptions.browser) {
117
- case "chrome":
118
- case "chrome-beta":
119
- case "chrome-canary":
120
- case "chrome-dev":
121
- case "chromium":
122
- case "msedge":
123
- case "msedge-beta":
124
- case "msedge-canary":
125
- case "msedge-dev":
126
- browserName = "chromium";
127
- channel = cliOptions.browser;
128
- break;
129
- case "firefox":
130
- browserName = "firefox";
131
- break;
132
- case "webkit":
133
- browserName = "webkit";
134
- break;
135
- }
136
- const launchOptions = {
137
- channel,
138
- executablePath: cliOptions.executablePath,
139
- headless: cliOptions.headless
140
- };
141
- if (cliOptions.sandbox === false)
142
- launchOptions.chromiumSandbox = false;
143
- if (cliOptions.proxyServer) {
144
- launchOptions.proxy = {
145
- server: cliOptions.proxyServer
146
- };
147
- if (cliOptions.proxyBypass)
148
- launchOptions.proxy.bypass = cliOptions.proxyBypass;
149
- }
150
- if (cliOptions.device && cliOptions.cdpEndpoint)
151
- throw new Error("Device emulation is not supported with cdpEndpoint.");
152
- const contextOptions = cliOptions.device ? import_playwright_core.devices[cliOptions.device] : {};
153
- if (cliOptions.storageState)
154
- contextOptions.storageState = cliOptions.storageState;
155
- if (cliOptions.userAgent)
156
- contextOptions.userAgent = cliOptions.userAgent;
157
- if (cliOptions.viewportSize)
158
- contextOptions.viewport = cliOptions.viewportSize;
159
- if (cliOptions.ignoreHttpsErrors)
160
- contextOptions.ignoreHTTPSErrors = true;
161
- if (cliOptions.blockServiceWorkers)
162
- contextOptions.serviceWorkers = "block";
163
- if (cliOptions.grantPermissions)
164
- contextOptions.permissions = cliOptions.grantPermissions;
165
- const result = {
166
- browser: {
167
- browserName,
168
- isolated: cliOptions.isolated,
169
- userDataDir: cliOptions.userDataDir,
170
- launchOptions,
171
- contextOptions,
172
- cdpEndpoint: cliOptions.cdpEndpoint,
173
- cdpHeaders: cliOptions.cdpHeader,
174
- initPage: cliOptions.initPage,
175
- initScript: cliOptions.initScript
176
- },
177
- server: {
178
- port: cliOptions.port,
179
- host: cliOptions.host,
180
- allowedHosts: cliOptions.allowedHosts
181
- },
182
- capabilities: cliOptions.caps,
183
- console: {
184
- level: cliOptions.consoleLevel
185
- },
186
- network: {
187
- allowedOrigins: cliOptions.allowedOrigins,
188
- blockedOrigins: cliOptions.blockedOrigins
189
- },
190
- allowUnrestrictedFileAccess: cliOptions.allowUnrestrictedFileAccess,
191
- codegen: cliOptions.codegen,
192
- saveSession: cliOptions.saveSession,
193
- saveTrace: cliOptions.saveTrace,
194
- saveVideo: cliOptions.saveVideo,
195
- secrets: cliOptions.secrets,
196
- sharedBrowserContext: cliOptions.sharedBrowserContext,
197
- snapshot: cliOptions.snapshotMode ? { mode: cliOptions.snapshotMode } : void 0,
198
- outputMode: cliOptions.outputMode,
199
- outputDir: cliOptions.outputDir,
200
- imageResponses: cliOptions.imageResponses,
201
- testIdAttribute: cliOptions.testIdAttribute,
202
- timeouts: {
203
- action: cliOptions.timeoutAction,
204
- navigation: cliOptions.timeoutNavigation
205
- }
206
- };
207
- return result;
208
- }
209
- function configFromEnv() {
210
- const options = {};
211
- options.allowedHosts = commaSeparatedList(process.env.PLAYWRIGHT_MCP_ALLOWED_HOSTNAMES);
212
- options.allowedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_ALLOWED_ORIGINS);
213
- options.allowUnrestrictedFileAccess = envToBoolean(process.env.PLAYWRIGHT_MCP_ALLOW_UNRESTRICTED_FILE_ACCESS);
214
- options.blockedOrigins = semicolonSeparatedList(process.env.PLAYWRIGHT_MCP_BLOCKED_ORIGINS);
215
- options.blockServiceWorkers = envToBoolean(process.env.PLAYWRIGHT_MCP_BLOCK_SERVICE_WORKERS);
216
- options.browser = envToString(process.env.PLAYWRIGHT_MCP_BROWSER);
217
- options.caps = commaSeparatedList(process.env.PLAYWRIGHT_MCP_CAPS);
218
- options.cdpEndpoint = envToString(process.env.PLAYWRIGHT_MCP_CDP_ENDPOINT);
219
- options.cdpHeader = headerParser(process.env.PLAYWRIGHT_MCP_CDP_HEADERS, {});
220
- options.config = envToString(process.env.PLAYWRIGHT_MCP_CONFIG);
221
- if (process.env.PLAYWRIGHT_MCP_CONSOLE_LEVEL)
222
- options.consoleLevel = enumParser("--console-level", ["error", "warning", "info", "debug"], process.env.PLAYWRIGHT_MCP_CONSOLE_LEVEL);
223
- options.device = envToString(process.env.PLAYWRIGHT_MCP_DEVICE);
224
- options.executablePath = envToString(process.env.PLAYWRIGHT_MCP_EXECUTABLE_PATH);
225
- options.grantPermissions = commaSeparatedList(process.env.PLAYWRIGHT_MCP_GRANT_PERMISSIONS);
226
- options.headless = envToBoolean(process.env.PLAYWRIGHT_MCP_HEADLESS);
227
- options.host = envToString(process.env.PLAYWRIGHT_MCP_HOST);
228
- options.ignoreHttpsErrors = envToBoolean(process.env.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);
229
- const initPage = envToString(process.env.PLAYWRIGHT_MCP_INIT_PAGE);
230
- if (initPage)
231
- options.initPage = [initPage];
232
- const initScript = envToString(process.env.PLAYWRIGHT_MCP_INIT_SCRIPT);
233
- if (initScript)
234
- options.initScript = [initScript];
235
- options.isolated = envToBoolean(process.env.PLAYWRIGHT_MCP_ISOLATED);
236
- if (process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES)
237
- options.imageResponses = enumParser("--image-responses", ["allow", "omit"], process.env.PLAYWRIGHT_MCP_IMAGE_RESPONSES);
238
- options.sandbox = envToBoolean(process.env.PLAYWRIGHT_MCP_SANDBOX);
239
- options.outputDir = envToString(process.env.PLAYWRIGHT_MCP_OUTPUT_DIR);
240
- options.port = numberParser(process.env.PLAYWRIGHT_MCP_PORT);
241
- options.proxyBypass = envToString(process.env.PLAYWRIGHT_MCP_PROXY_BYPASS);
242
- options.proxyServer = envToString(process.env.PLAYWRIGHT_MCP_PROXY_SERVER);
243
- options.saveTrace = envToBoolean(process.env.PLAYWRIGHT_MCP_SAVE_TRACE);
244
- options.saveVideo = resolutionParser("--save-video", process.env.PLAYWRIGHT_MCP_SAVE_VIDEO);
245
- options.secrets = dotenvFileLoader(process.env.PLAYWRIGHT_MCP_SECRETS_FILE);
246
- options.storageState = envToString(process.env.PLAYWRIGHT_MCP_STORAGE_STATE);
247
- options.testIdAttribute = envToString(process.env.PLAYWRIGHT_MCP_TEST_ID_ATTRIBUTE);
248
- options.timeoutAction = numberParser(process.env.PLAYWRIGHT_MCP_TIMEOUT_ACTION);
249
- options.timeoutNavigation = numberParser(process.env.PLAYWRIGHT_MCP_TIMEOUT_NAVIGATION);
250
- options.userAgent = envToString(process.env.PLAYWRIGHT_MCP_USER_AGENT);
251
- options.userDataDir = envToString(process.env.PLAYWRIGHT_MCP_USER_DATA_DIR);
252
- options.viewportSize = resolutionParser("--viewport-size", process.env.PLAYWRIGHT_MCP_VIEWPORT_SIZE);
253
- return configFromCLIOptions(options);
254
- }
255
- async function loadConfig(configFile) {
256
- if (!configFile)
257
- return {};
258
- try {
259
- return JSON.parse(await import_fs.default.promises.readFile(configFile, "utf8"));
260
- } catch (error) {
261
- throw new Error(`Failed to load config file: ${configFile}, ${error}`);
262
- }
263
- }
264
- function tmpDir() {
265
- return import_path.default.join(process.env.PW_TMPDIR_FOR_TEST ?? import_os.default.tmpdir(), "playwright-mcp-output");
266
- }
267
- function outputDir(config, clientInfo) {
268
- const rootPath = (0, import_server.firstRootPath)(clientInfo);
269
- return config.outputDir ?? (rootPath ? import_path.default.join(rootPath, ".playwright-mcp") : void 0) ?? import_path.default.join(tmpDir(), String(clientInfo.timestamp));
270
- }
271
- async function outputFile(config, clientInfo, fileName, options) {
272
- const file = await resolveFile(config, clientInfo, fileName, options);
273
- await import_fs.default.promises.mkdir(import_path.default.dirname(file), { recursive: true });
274
- (0, import_utilsBundle.debug)("pw:mcp:file")(options.title, file);
275
- return file;
276
- }
277
- async function resolveFile(config, clientInfo, fileName, options) {
278
- const dir = outputDir(config, clientInfo);
279
- if (options.origin === "code")
280
- return import_path.default.resolve(dir, fileName);
281
- if (options.origin === "llm") {
282
- fileName = fileName.split("\\").join("/");
283
- const resolvedFile = import_path.default.resolve(dir, fileName);
284
- if (!resolvedFile.startsWith(import_path.default.resolve(dir) + import_path.default.sep))
285
- throw new Error(`Resolved file path ${resolvedFile} is outside of the output directory ${dir}. Use relative file names to stay within the output directory.`);
286
- return resolvedFile;
287
- }
288
- return import_path.default.join(dir, sanitizeForFilePath(fileName));
289
- }
290
- function pickDefined(obj) {
291
- return Object.fromEntries(
292
- Object.entries(obj ?? {}).filter(([_, v]) => v !== void 0)
293
- );
294
- }
295
- function mergeConfig(base, overrides) {
296
- const browser = {
297
- ...pickDefined(base.browser),
298
- ...pickDefined(overrides.browser),
299
- browserName: overrides.browser?.browserName ?? base.browser?.browserName ?? "chromium",
300
- isolated: overrides.browser?.isolated ?? base.browser?.isolated ?? false,
301
- launchOptions: {
302
- ...pickDefined(base.browser?.launchOptions),
303
- ...pickDefined(overrides.browser?.launchOptions),
304
- ...{ assistantMode: true }
305
- },
306
- contextOptions: {
307
- ...pickDefined(base.browser?.contextOptions),
308
- ...pickDefined(overrides.browser?.contextOptions)
309
- }
310
- };
311
- if (browser.browserName !== "chromium" && browser.launchOptions)
312
- delete browser.launchOptions.channel;
313
- return {
314
- ...pickDefined(base),
315
- ...pickDefined(overrides),
316
- browser,
317
- console: {
318
- ...pickDefined(base.console),
319
- ...pickDefined(overrides.console)
320
- },
321
- network: {
322
- ...pickDefined(base.network),
323
- ...pickDefined(overrides.network)
324
- },
325
- server: {
326
- ...pickDefined(base.server),
327
- ...pickDefined(overrides.server)
328
- },
329
- snapshot: {
330
- ...pickDefined(base.snapshot),
331
- ...pickDefined(overrides.snapshot)
332
- },
333
- timeouts: {
334
- ...pickDefined(base.timeouts),
335
- ...pickDefined(overrides.timeouts)
336
- }
337
- };
338
- }
339
- function semicolonSeparatedList(value) {
340
- if (!value)
341
- return void 0;
342
- return value.split(";").map((v) => v.trim());
343
- }
344
- function commaSeparatedList(value) {
345
- if (!value)
346
- return void 0;
347
- return value.split(",").map((v) => v.trim());
348
- }
349
- function dotenvFileLoader(value) {
350
- if (!value)
351
- return void 0;
352
- return import_utilsBundle.dotenv.parse(import_fs.default.readFileSync(value, "utf8"));
353
- }
354
- function numberParser(value) {
355
- if (!value)
356
- return void 0;
357
- return +value;
358
- }
359
- function resolutionParser(name, value) {
360
- if (!value)
361
- return void 0;
362
- if (value.includes("x")) {
363
- const [width, height] = value.split("x").map((v) => +v);
364
- if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0)
365
- throw new Error(`Invalid resolution format: use ${name}="800x600"`);
366
- return { width, height };
367
- }
368
- if (value.includes(",")) {
369
- const [width, height] = value.split(",").map((v) => +v);
370
- if (isNaN(width) || isNaN(height) || width <= 0 || height <= 0)
371
- throw new Error(`Invalid resolution format: use ${name}="800x600"`);
372
- return { width, height };
373
- }
374
- throw new Error(`Invalid resolution format: use ${name}="800x600"`);
375
- }
376
- function headerParser(arg, previous) {
377
- if (!arg)
378
- return previous || {};
379
- const result = previous || {};
380
- const [name, value] = arg.split(":").map((v) => v.trim());
381
- result[name] = value;
382
- return result;
383
- }
384
- function enumParser(name, options, value) {
385
- if (!options.includes(value))
386
- throw new Error(`Invalid ${name}: ${value}. Valid values are: ${options.join(", ")}`);
387
- return value;
388
- }
389
- function envToBoolean(value) {
390
- if (value === "true" || value === "1")
391
- return true;
392
- if (value === "false" || value === "0")
393
- return false;
394
- return void 0;
395
- }
396
- function envToString(value) {
397
- return value ? value.trim() : void 0;
398
- }
399
- function sanitizeForFilePath(s) {
400
- const sanitize = (s2) => s2.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, "-");
401
- const separator = s.lastIndexOf(".");
402
- if (separator === -1)
403
- return sanitize(s);
404
- return sanitize(s.substring(0, separator)) + "." + sanitize(s.substring(separator + 1));
405
- }
406
- // Annotate the CommonJS export names for ESM import in node:
407
- 0 && (module.exports = {
408
- commaSeparatedList,
409
- configFromCLIOptions,
410
- defaultConfig,
411
- dotenvFileLoader,
412
- enumParser,
413
- headerParser,
414
- numberParser,
415
- outputDir,
416
- outputFile,
417
- resolutionParser,
418
- resolveCLIConfig,
419
- resolveConfig,
420
- semicolonSeparatedList
421
- });
@@ -1,244 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
- var context_exports = {};
30
- __export(context_exports, {
31
- Context: () => Context
32
- });
33
- module.exports = __toCommonJS(context_exports);
34
- var import_utilsBundle = require("patchright-core/lib/utilsBundle");
35
- var import_utils = require("patchright-core/lib/utils");
36
- var import_playwright_core = require("patchright-core");
37
- var import_url = require("url");
38
- var import_os = __toESM(require("os"));
39
- var import_log = require("../log");
40
- var import_tab = require("./tab");
41
- var import_config = require("./config");
42
- const testDebug = (0, import_utilsBundle.debug)("pw:mcp:test");
43
- class Context {
44
- constructor(options) {
45
- this._tabs = [];
46
- this._abortController = new AbortController();
47
- this.config = options.config;
48
- this.sessionLog = options.sessionLog;
49
- this.options = options;
50
- this._browserContextFactory = options.browserContextFactory;
51
- this._clientInfo = options.clientInfo;
52
- testDebug("create context");
53
- Context._allContexts.add(this);
54
- }
55
- static {
56
- this._allContexts = /* @__PURE__ */ new Set();
57
- }
58
- static async disposeAll() {
59
- await Promise.all([...Context._allContexts].map((context) => context.dispose()));
60
- }
61
- tabs() {
62
- return this._tabs;
63
- }
64
- currentTab() {
65
- return this._currentTab;
66
- }
67
- currentTabOrDie() {
68
- if (!this._currentTab)
69
- throw new Error("No open pages available.");
70
- return this._currentTab;
71
- }
72
- async newTab() {
73
- const { browserContext } = await this._ensureBrowserContext({});
74
- const page = await browserContext.newPage();
75
- this._currentTab = this._tabs.find((t) => t.page === page);
76
- return this._currentTab;
77
- }
78
- async selectTab(index) {
79
- const tab = this._tabs[index];
80
- if (!tab)
81
- throw new Error(`Tab ${index} not found`);
82
- await tab.page.bringToFront();
83
- this._currentTab = tab;
84
- return tab;
85
- }
86
- async ensureTab(options = {}) {
87
- const { browserContext } = await this._ensureBrowserContext(options);
88
- if (!this._currentTab)
89
- await browserContext.newPage();
90
- return this._currentTab;
91
- }
92
- async closeTab(index) {
93
- const tab = index === void 0 ? this._currentTab : this._tabs[index];
94
- if (!tab)
95
- throw new Error(`Tab ${index} not found`);
96
- const url = tab.page.url();
97
- await tab.page.close();
98
- return url;
99
- }
100
- async outputFile(fileName, options) {
101
- return (0, import_config.outputFile)(this.config, this._clientInfo, fileName, options);
102
- }
103
- _onPageCreated(page) {
104
- const tab = new import_tab.Tab(this, page, (tab2) => this._onPageClosed(tab2));
105
- this._tabs.push(tab);
106
- if (!this._currentTab)
107
- this._currentTab = tab;
108
- }
109
- _onPageClosed(tab) {
110
- const index = this._tabs.indexOf(tab);
111
- if (index === -1)
112
- return;
113
- this._tabs.splice(index, 1);
114
- if (this._currentTab === tab)
115
- this._currentTab = this._tabs[Math.min(index, this._tabs.length - 1)];
116
- if (!this._tabs.length)
117
- void this.closeBrowserContext();
118
- }
119
- async closeBrowserContext() {
120
- if (!this._closeBrowserContextPromise)
121
- this._closeBrowserContextPromise = this._closeBrowserContextImpl().catch(import_log.logUnhandledError);
122
- await this._closeBrowserContextPromise;
123
- this._closeBrowserContextPromise = void 0;
124
- }
125
- isRunningTool() {
126
- return this._runningToolName !== void 0;
127
- }
128
- setRunningTool(name) {
129
- this._runningToolName = name;
130
- }
131
- async _closeBrowserContextImpl() {
132
- if (!this._browserContextPromise)
133
- return;
134
- testDebug("close context");
135
- const promise = this._browserContextPromise;
136
- this._browserContextPromise = void 0;
137
- this._browserContextOption = void 0;
138
- await promise.then(async ({ browserContext, close }) => {
139
- if (this.config.saveTrace)
140
- await browserContext.tracing.stop();
141
- await close();
142
- });
143
- }
144
- async dispose() {
145
- this._abortController.abort("MCP context disposed");
146
- await this.closeBrowserContext();
147
- Context._allContexts.delete(this);
148
- }
149
- async _setupRequestInterception(context) {
150
- if (this.config.network?.allowedOrigins?.length) {
151
- await context.route("**", (route) => route.abort("blockedbyclient"));
152
- for (const origin of this.config.network.allowedOrigins)
153
- await context.route(originOrHostGlob(origin), (route) => route.continue());
154
- }
155
- if (this.config.network?.blockedOrigins?.length) {
156
- for (const origin of this.config.network.blockedOrigins)
157
- await context.route(originOrHostGlob(origin), (route) => route.abort("blockedbyclient"));
158
- }
159
- }
160
- async ensureBrowserContext(options = {}) {
161
- const { browserContext } = await this._ensureBrowserContext(options);
162
- return browserContext;
163
- }
164
- _ensureBrowserContext(options) {
165
- if (this._browserContextPromise && (options.forceHeadless === void 0 || this._browserContextOption?.forceHeadless === options.forceHeadless))
166
- return this._browserContextPromise;
167
- const closePrework = this._browserContextPromise ? this.closeBrowserContext() : Promise.resolve();
168
- this._browserContextPromise = closePrework.then(() => this._setupBrowserContext(options));
169
- this._browserContextPromise.catch(() => {
170
- this._browserContextPromise = void 0;
171
- this._browserContextOption = void 0;
172
- });
173
- this._browserContextOption = options;
174
- return this._browserContextPromise;
175
- }
176
- async _setupBrowserContext(options) {
177
- if (this._closeBrowserContextPromise)
178
- throw new Error("Another browser context is being closed.");
179
- if (this.config.testIdAttribute)
180
- import_playwright_core.selectors.setTestIdAttribute(this.config.testIdAttribute);
181
- const result = await this._browserContextFactory.createContext(this._clientInfo, this._abortController.signal, { toolName: this._runningToolName, ...options });
182
- const { browserContext } = result;
183
- if (!this.config.allowUnrestrictedFileAccess) {
184
- browserContext._setAllowedProtocols(["http:", "https:", "about:", "data:"]);
185
- browserContext._setAllowedDirectories(allRootPaths(this._clientInfo));
186
- }
187
- await this._setupRequestInterception(browserContext);
188
- for (const page of browserContext.pages())
189
- this._onPageCreated(page);
190
- browserContext.on("page", (page) => this._onPageCreated(page));
191
- if (this.config.saveTrace) {
192
- await browserContext.tracing.start({
193
- name: "trace-" + Date.now(),
194
- screenshots: true,
195
- snapshots: true,
196
- _live: true
197
- });
198
- }
199
- return result;
200
- }
201
- lookupSecret(secretName) {
202
- if (!this.config.secrets?.[secretName])
203
- return { value: secretName, code: (0, import_utils.escapeWithQuotes)(secretName, "'") };
204
- return {
205
- value: this.config.secrets[secretName],
206
- code: `process.env['${secretName}']`
207
- };
208
- }
209
- firstRootPath() {
210
- return allRootPaths(this._clientInfo)[0];
211
- }
212
- }
213
- function allRootPaths(clientInfo) {
214
- const paths = [];
215
- for (const root of clientInfo.roots) {
216
- const url = new URL(root.uri);
217
- let rootPath;
218
- try {
219
- rootPath = (0, import_url.fileURLToPath)(url);
220
- } catch (e) {
221
- if (e.code === "ERR_INVALID_FILE_URL_PATH" && import_os.default.platform() === "win32")
222
- rootPath = decodeURIComponent(url.pathname);
223
- }
224
- if (!rootPath)
225
- continue;
226
- paths.push(rootPath);
227
- }
228
- if (paths.length === 0)
229
- paths.push(process.cwd());
230
- return paths;
231
- }
232
- function originOrHostGlob(originOrHost) {
233
- try {
234
- const url = new URL(originOrHost);
235
- if (url.origin !== "null")
236
- return `${url.origin}/**`;
237
- } catch {
238
- }
239
- return `*://${originOrHost}/**`;
240
- }
241
- // Annotate the CommonJS export names for ESM import in node:
242
- 0 && (module.exports = {
243
- Context
244
- });