playwright 1.55.1 → 1.56.1

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 (105) hide show
  1. package/README.md +3 -3
  2. package/ThirdPartyNotices.txt +3 -3
  3. package/lib/agents/generateAgents.js +263 -0
  4. package/lib/agents/generator.md +102 -0
  5. package/lib/agents/healer.md +78 -0
  6. package/lib/agents/planner.md +135 -0
  7. package/lib/common/config.js +1 -1
  8. package/lib/common/expectBundle.js +3 -0
  9. package/lib/common/expectBundleImpl.js +51 -51
  10. package/lib/index.js +7 -8
  11. package/lib/isomorphic/testServerConnection.js +0 -7
  12. package/lib/isomorphic/testTree.js +35 -8
  13. package/lib/matchers/expect.js +8 -21
  14. package/lib/matchers/matcherHint.js +42 -18
  15. package/lib/matchers/matchers.js +12 -6
  16. package/lib/matchers/toBeTruthy.js +16 -14
  17. package/lib/matchers/toEqual.js +18 -13
  18. package/lib/matchers/toHaveURL.js +12 -27
  19. package/lib/matchers/toMatchAriaSnapshot.js +26 -30
  20. package/lib/matchers/toMatchSnapshot.js +15 -12
  21. package/lib/matchers/toMatchText.js +29 -35
  22. package/lib/mcp/browser/actions.d.js +16 -0
  23. package/lib/mcp/browser/browserContextFactory.js +296 -0
  24. package/lib/mcp/browser/browserServerBackend.js +76 -0
  25. package/lib/mcp/browser/codegen.js +66 -0
  26. package/lib/mcp/browser/config.js +383 -0
  27. package/lib/mcp/browser/context.js +284 -0
  28. package/lib/mcp/browser/response.js +228 -0
  29. package/lib/mcp/browser/sessionLog.js +160 -0
  30. package/lib/mcp/browser/tab.js +277 -0
  31. package/lib/mcp/browser/tools/common.js +63 -0
  32. package/lib/mcp/browser/tools/console.js +44 -0
  33. package/lib/mcp/browser/tools/dialogs.js +60 -0
  34. package/lib/mcp/browser/tools/evaluate.js +70 -0
  35. package/lib/mcp/browser/tools/files.js +58 -0
  36. package/lib/mcp/browser/tools/form.js +74 -0
  37. package/lib/mcp/browser/tools/install.js +69 -0
  38. package/lib/mcp/browser/tools/keyboard.js +85 -0
  39. package/lib/mcp/browser/tools/mouse.js +107 -0
  40. package/lib/mcp/browser/tools/navigate.js +62 -0
  41. package/lib/mcp/browser/tools/network.js +54 -0
  42. package/lib/mcp/browser/tools/pdf.js +59 -0
  43. package/lib/mcp/browser/tools/screenshot.js +88 -0
  44. package/lib/mcp/browser/tools/snapshot.js +182 -0
  45. package/lib/mcp/browser/tools/tabs.js +67 -0
  46. package/lib/mcp/browser/tools/tool.js +49 -0
  47. package/lib/mcp/browser/tools/tracing.js +74 -0
  48. package/lib/mcp/browser/tools/utils.js +100 -0
  49. package/lib/mcp/browser/tools/verify.js +154 -0
  50. package/lib/mcp/browser/tools/wait.js +63 -0
  51. package/lib/mcp/browser/tools.js +80 -0
  52. package/lib/mcp/browser/watchdog.js +44 -0
  53. package/lib/mcp/config.d.js +16 -0
  54. package/lib/mcp/extension/cdpRelay.js +351 -0
  55. package/lib/mcp/extension/extensionContextFactory.js +75 -0
  56. package/lib/mcp/extension/protocol.js +28 -0
  57. package/lib/mcp/index.js +61 -0
  58. package/lib/mcp/{tool.js → log.js} +12 -18
  59. package/lib/mcp/program.js +96 -0
  60. package/lib/mcp/{bundle.js → sdk/bundle.js} +24 -2
  61. package/lib/mcp/{exports.js → sdk/exports.js} +12 -10
  62. package/lib/mcp/{transport.js → sdk/http.js} +79 -60
  63. package/lib/mcp/sdk/mdb.js +208 -0
  64. package/lib/mcp/{proxyBackend.js → sdk/proxyBackend.js} +18 -13
  65. package/lib/mcp/sdk/server.js +190 -0
  66. package/lib/mcp/sdk/tool.js +51 -0
  67. package/lib/mcp/test/browserBackend.js +98 -0
  68. package/lib/mcp/test/generatorTools.js +122 -0
  69. package/lib/mcp/test/plannerTools.js +46 -0
  70. package/lib/mcp/test/seed.js +72 -0
  71. package/lib/mcp/test/streams.js +39 -0
  72. package/lib/mcp/test/testBackend.js +97 -0
  73. package/lib/mcp/test/testContext.js +176 -0
  74. package/lib/mcp/test/testTool.js +30 -0
  75. package/lib/mcp/test/testTools.js +115 -0
  76. package/lib/mcpBundleImpl.js +14 -67
  77. package/lib/plugins/webServerPlugin.js +2 -0
  78. package/lib/program.js +68 -0
  79. package/lib/reporters/base.js +15 -17
  80. package/lib/reporters/html.js +39 -26
  81. package/lib/reporters/list.js +8 -4
  82. package/lib/reporters/listModeReporter.js +6 -3
  83. package/lib/reporters/merge.js +3 -1
  84. package/lib/reporters/teleEmitter.js +3 -1
  85. package/lib/runner/dispatcher.js +9 -23
  86. package/lib/runner/failureTracker.js +12 -16
  87. package/lib/runner/loadUtils.js +39 -3
  88. package/lib/runner/projectUtils.js +8 -2
  89. package/lib/runner/tasks.js +18 -7
  90. package/lib/runner/testRunner.js +16 -28
  91. package/lib/runner/testServer.js +17 -23
  92. package/lib/runner/watchMode.js +1 -53
  93. package/lib/runner/workerHost.js +8 -10
  94. package/lib/transform/babelBundleImpl.js +10 -10
  95. package/lib/transform/compilationCache.js +22 -5
  96. package/lib/util.js +12 -16
  97. package/lib/utilsBundleImpl.js +1 -1
  98. package/lib/worker/fixtureRunner.js +15 -7
  99. package/lib/worker/testInfo.js +9 -24
  100. package/lib/worker/workerMain.js +12 -8
  101. package/package.json +7 -3
  102. package/types/test.d.ts +17 -8
  103. package/types/testReporter.d.ts +1 -1
  104. package/lib/mcp/server.js +0 -118
  105. /package/lib/mcp/{inProcessTransport.js → sdk/inProcessTransport.js} +0 -0
@@ -162,7 +162,7 @@ class SnapshotHelper {
162
162
  step?._attachToStep({ name: (0, import_util.addSuffixToFilePath)(this.attachmentBaseName, "-diff"), contentType: this.mimeType, path: this.diffPath });
163
163
  }
164
164
  if (log?.length)
165
- output.push((0, import_util.callLogText)(log));
165
+ output.push((0, import_matcherHint.callLogText)(log));
166
166
  else
167
167
  output.push("");
168
168
  return this.createMatcherResult(output.join("\n"), false, log);
@@ -216,8 +216,7 @@ function toMatchSnapshot(received, nameOrOptions = {}, optOptions = {}) {
216
216
  const result = helper.comparator(received, expected, helper.options);
217
217
  if (!result)
218
218
  return helper.handleMatching();
219
- const receiver = (0, import_utils.isString)(received) ? "string" : "Buffer";
220
- const header = (0, import_matcherHint.matcherHint)(this, void 0, "toMatchSnapshot", receiver, void 0, void 0, void 0);
219
+ const header = (0, import_matcherHint.formatMatcherMessage)(this, { matcherName: "toMatchSnapshot", receiver: (0, import_utils.isString)(received) ? "string" : "Buffer", expectation: "expected" });
221
220
  return helper.handleDifferent(received, expected, void 0, result.diff, header, result.errorMessage, void 0, this._stepInfo);
222
221
  }
223
222
  function toHaveScreenshotStepTitle(nameOrOptions = {}, optOptions = {}) {
@@ -271,11 +270,10 @@ async function toHaveScreenshot(pageOrLocator, nameOrOptions = {}, optOptions =
271
270
  }
272
271
  if (helper.updateSnapshots === "none" && !hasSnapshot)
273
272
  return helper.createMatcherResult(`A snapshot doesn't exist at ${helper.expectedPath}.`, false);
274
- const receiver = locator ? "locator" : "page";
275
273
  if (!hasSnapshot) {
276
274
  const { actual: actual2, previous: previous2, diff: diff2, errorMessage: errorMessage2, log: log2, timedOut: timedOut2 } = await page._expectScreenshot(expectScreenshotOptions);
277
275
  if (errorMessage2) {
278
- const header2 = (0, import_matcherHint.matcherHint)(this, locator, "toHaveScreenshot", receiver, void 0, void 0, timedOut2 ? timeout : void 0);
276
+ const header2 = (0, import_matcherHint.formatMatcherMessage)(this, { matcherName: "toHaveScreenshot", locator, expectation: "expected", timeout, timedOut: timedOut2 });
279
277
  return helper.handleDifferent(actual2, void 0, previous2, diff2, header2, errorMessage2, log2, this._stepInfo);
280
278
  }
281
279
  return helper.handleMissing(actual2, this._stepInfo);
@@ -283,22 +281,27 @@ async function toHaveScreenshot(pageOrLocator, nameOrOptions = {}, optOptions =
283
281
  const expected = await import_fs.default.promises.readFile(helper.expectedPath);
284
282
  expectScreenshotOptions.expected = helper.updateSnapshots === "all" ? void 0 : expected;
285
283
  const { actual, previous, diff, errorMessage, log, timedOut } = await page._expectScreenshot(expectScreenshotOptions);
286
- const writeFiles = () => {
287
- writeFileSync(helper.expectedPath, actual);
288
- writeFileSync(helper.actualPath, actual);
284
+ const writeFiles = (actualBuffer) => {
285
+ writeFileSync(helper.expectedPath, actualBuffer);
286
+ writeFileSync(helper.actualPath, actualBuffer);
289
287
  console.log(helper.expectedPath + " is re-generated, writing actual.");
290
288
  return helper.createMatcherResult(helper.expectedPath + " running with --update-snapshots, writing actual.", true);
291
289
  };
292
290
  if (!errorMessage) {
293
291
  if (helper.updateSnapshots === "all" && actual && (0, import_utils.compareBuffersOrStrings)(actual, expected)) {
294
292
  console.log(helper.expectedPath + " is re-generated, writing actual.");
295
- return writeFiles();
293
+ return writeFiles(actual);
296
294
  }
297
295
  return helper.handleMatching();
298
296
  }
299
- if (helper.updateSnapshots === "changed" || helper.updateSnapshots === "all")
300
- return writeFiles();
301
- const header = (0, import_matcherHint.matcherHint)(this, void 0, "toHaveScreenshot", receiver, void 0, void 0, timedOut ? timeout : void 0);
297
+ if (helper.updateSnapshots === "changed" || helper.updateSnapshots === "all") {
298
+ if (actual)
299
+ return writeFiles(actual);
300
+ let header2 = (0, import_matcherHint.formatMatcherMessage)(this, { matcherName: "toHaveScreenshot", locator, expectation: "expected", timeout, timedOut });
301
+ header2 += " Failed to re-generate expected.\n";
302
+ return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header2, errorMessage, log, this._stepInfo);
303
+ }
304
+ const header = (0, import_matcherHint.formatMatcherMessage)(this, { matcherName: "toHaveScreenshot", locator, expectation: "expected", timeout, timedOut });
302
305
  return helper.handleDifferent(actual, expectScreenshotOptions.expected, previous, diff, header, errorMessage, log, this._stepInfo);
303
306
  }
304
307
  function writeFileSync(aPath, content) {
@@ -21,26 +21,20 @@ __export(toMatchText_exports, {
21
21
  toMatchText: () => toMatchText
22
22
  });
23
23
  module.exports = __toCommonJS(toMatchText_exports);
24
- var import_utils = require("playwright-core/lib/utils");
25
24
  var import_util = require("../util");
26
25
  var import_expect = require("./expect");
27
26
  var import_matcherHint = require("./matcherHint");
28
27
  var import_expectBundle = require("../common/expectBundle");
29
28
  async function toMatchText(matcherName, receiver, receiverType, query, expected, options = {}) {
30
29
  (0, import_util.expectTypes)(receiver, [receiverType], matcherName);
31
- const matcherOptions = {
32
- isNot: this.isNot,
33
- promise: this.promise
34
- };
30
+ const locator = receiverType === "Locator" ? receiver : void 0;
35
31
  if (!(typeof expected === "string") && !(expected && typeof expected.test === "function")) {
36
- throw new Error([
37
- (0, import_matcherHint.matcherHint)(this, receiverType === "Locator" ? receiver : void 0, matcherName, options.receiverLabel ?? receiver, expected, matcherOptions, void 0, void 0, true),
38
- `${import_utils.colors.bold("Matcher error")}: ${(0, import_expectBundle.EXPECTED_COLOR)("expected")} value must be a string or regular expression`,
39
- this.utils.printWithType("Expected", expected, this.utils.printExpected)
40
- ].join("\n\n"));
32
+ const errorMessage2 = `Error: ${(0, import_expectBundle.EXPECTED_COLOR)("expected")} value must be a string or regular expression
33
+ ${this.utils.printWithType("Expected", expected, this.utils.printExpected)}`;
34
+ throw new Error((0, import_matcherHint.formatMatcherMessage)(this, { locator, matcherName, expectation: "expected", errorMessage: errorMessage2 }));
41
35
  }
42
36
  const timeout = options.timeout ?? this.timeout;
43
- const { matches: pass, received, log, timedOut } = await query(!!this.isNot, timeout);
37
+ const { matches: pass, received, log, timedOut, errorMessage } = await query(!!this.isNot, timeout);
44
38
  if (pass === !this.isNot) {
45
39
  return {
46
40
  name: matcherName,
@@ -49,45 +43,45 @@ async function toMatchText(matcherName, receiver, receiverType, query, expected,
49
43
  expected
50
44
  };
51
45
  }
52
- const stringSubstring = options.matchSubstring ? "substring" : "string";
46
+ const expectedSuffix = typeof expected === "string" ? options.matchSubstring ? " substring" : "" : " pattern";
47
+ const receivedSuffix = typeof expected === "string" ? options.matchSubstring ? " string" : "" : " string";
53
48
  const receivedString = received || "";
54
- const notFound = received === import_matcherHint.kNoElementsFoundError;
55
49
  let printedReceived;
56
50
  let printedExpected;
57
51
  let printedDiff;
58
52
  if (pass) {
59
53
  if (typeof expected === "string") {
60
- if (notFound) {
61
- printedExpected = `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}`;
62
- printedReceived = `Received: ${received}`;
63
- } else {
64
- printedExpected = `Expected ${stringSubstring}: not ${this.utils.printExpected(expected)}`;
54
+ printedExpected = `Expected${expectedSuffix}: not ${this.utils.printExpected(expected)}`;
55
+ if (!errorMessage) {
65
56
  const formattedReceived = (0, import_expect.printReceivedStringContainExpectedSubstring)(receivedString, receivedString.indexOf(expected), expected.length);
66
- printedReceived = `Received string: ${formattedReceived}`;
57
+ printedReceived = `Received${receivedSuffix}: ${formattedReceived}`;
67
58
  }
68
59
  } else {
69
- if (notFound) {
70
- printedExpected = `Expected pattern: not ${this.utils.printExpected(expected)}`;
71
- printedReceived = `Received: ${received}`;
72
- } else {
73
- printedExpected = `Expected pattern: not ${this.utils.printExpected(expected)}`;
60
+ printedExpected = `Expected${expectedSuffix}: not ${this.utils.printExpected(expected)}`;
61
+ if (!errorMessage) {
74
62
  const formattedReceived = (0, import_expect.printReceivedStringContainExpectedResult)(receivedString, typeof expected.exec === "function" ? expected.exec(receivedString) : null);
75
- printedReceived = `Received string: ${formattedReceived}`;
63
+ printedReceived = `Received${receivedSuffix}: ${formattedReceived}`;
76
64
  }
77
65
  }
78
66
  } else {
79
- const labelExpected = `Expected ${typeof expected === "string" ? stringSubstring : "pattern"}`;
80
- if (notFound) {
81
- printedExpected = `${labelExpected}: ${this.utils.printExpected(expected)}`;
82
- printedReceived = `Received: ${received}`;
83
- } else {
84
- printedDiff = this.utils.printDiffOrStringify(expected, receivedString, labelExpected, "Received string", false);
85
- }
67
+ if (errorMessage)
68
+ printedExpected = `Expected${expectedSuffix}: ${this.utils.printExpected(expected)}`;
69
+ else
70
+ printedDiff = this.utils.printDiffOrStringify(expected, receivedString, `Expected${expectedSuffix}`, `Received${receivedSuffix}`, false);
86
71
  }
87
72
  const message = () => {
88
- const resultDetails = printedDiff ? printedDiff : printedExpected + "\n" + printedReceived;
89
- const hints = (0, import_matcherHint.matcherHint)(this, receiverType === "Locator" ? receiver : void 0, matcherName, options.receiverLabel ?? "locator", void 0, matcherOptions, timedOut ? timeout : void 0, resultDetails, true);
90
- return hints + (0, import_util.callLogText)(log);
73
+ return (0, import_matcherHint.formatMatcherMessage)(this, {
74
+ matcherName,
75
+ expectation: "expected",
76
+ locator,
77
+ timeout,
78
+ timedOut,
79
+ printedExpected,
80
+ printedReceived,
81
+ printedDiff,
82
+ log,
83
+ errorMessage
84
+ });
91
85
  };
92
86
  return {
93
87
  name: matcherName,
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var actions_d_exports = {};
16
+ module.exports = __toCommonJS(actions_d_exports);
@@ -0,0 +1,296 @@
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 browserContextFactory_exports = {};
30
+ __export(browserContextFactory_exports, {
31
+ SharedContextFactory: () => SharedContextFactory,
32
+ contextFactory: () => contextFactory
33
+ });
34
+ module.exports = __toCommonJS(browserContextFactory_exports);
35
+ var import_crypto = __toESM(require("crypto"));
36
+ var import_fs = __toESM(require("fs"));
37
+ var import_net = __toESM(require("net"));
38
+ var import_path = __toESM(require("path"));
39
+ var playwright = __toESM(require("playwright-core"));
40
+ var import_registry = require("playwright-core/lib/server/registry/index");
41
+ var import_server = require("playwright-core/lib/server");
42
+ var import_log = require("../log");
43
+ var import_config = require("./config");
44
+ var import_server2 = require("../sdk/server");
45
+ function contextFactory(config) {
46
+ if (config.sharedBrowserContext)
47
+ return SharedContextFactory.create(config);
48
+ if (config.browser.remoteEndpoint)
49
+ return new RemoteContextFactory(config);
50
+ if (config.browser.cdpEndpoint)
51
+ return new CdpContextFactory(config);
52
+ if (config.browser.isolated)
53
+ return new IsolatedContextFactory(config);
54
+ return new PersistentContextFactory(config);
55
+ }
56
+ class BaseContextFactory {
57
+ constructor(name, config) {
58
+ this._logName = name;
59
+ this.config = config;
60
+ }
61
+ async _obtainBrowser(clientInfo) {
62
+ if (this._browserPromise)
63
+ return this._browserPromise;
64
+ (0, import_log.testDebug)(`obtain browser (${this._logName})`);
65
+ this._browserPromise = this._doObtainBrowser(clientInfo);
66
+ void this._browserPromise.then((browser) => {
67
+ browser.on("disconnected", () => {
68
+ this._browserPromise = void 0;
69
+ });
70
+ }).catch(() => {
71
+ this._browserPromise = void 0;
72
+ });
73
+ return this._browserPromise;
74
+ }
75
+ async _doObtainBrowser(clientInfo) {
76
+ throw new Error("Not implemented");
77
+ }
78
+ async createContext(clientInfo) {
79
+ (0, import_log.testDebug)(`create browser context (${this._logName})`);
80
+ const browser = await this._obtainBrowser(clientInfo);
81
+ const browserContext = await this._doCreateContext(browser);
82
+ await addInitScript(browserContext, this.config.browser.initScript);
83
+ return {
84
+ browserContext,
85
+ close: (afterClose) => this._closeBrowserContext(browserContext, browser, afterClose)
86
+ };
87
+ }
88
+ async _doCreateContext(browser) {
89
+ throw new Error("Not implemented");
90
+ }
91
+ async _closeBrowserContext(browserContext, browser, afterClose) {
92
+ (0, import_log.testDebug)(`close browser context (${this._logName})`);
93
+ if (browser.contexts().length === 1)
94
+ this._browserPromise = void 0;
95
+ await browserContext.close().catch(import_log.logUnhandledError);
96
+ await afterClose();
97
+ if (browser.contexts().length === 0) {
98
+ (0, import_log.testDebug)(`close browser (${this._logName})`);
99
+ await browser.close().catch(import_log.logUnhandledError);
100
+ }
101
+ }
102
+ }
103
+ class IsolatedContextFactory extends BaseContextFactory {
104
+ constructor(config) {
105
+ super("isolated", config);
106
+ }
107
+ async _doObtainBrowser(clientInfo) {
108
+ await injectCdpPort(this.config.browser);
109
+ const browserType = playwright[this.config.browser.browserName];
110
+ const tracesDir = await computeTracesDir(this.config, clientInfo);
111
+ if (tracesDir && this.config.saveTrace)
112
+ await startTraceServer(this.config, tracesDir);
113
+ return browserType.launch({
114
+ tracesDir,
115
+ ...this.config.browser.launchOptions,
116
+ handleSIGINT: false,
117
+ handleSIGTERM: false
118
+ }).catch((error) => {
119
+ if (error.message.includes("Executable doesn't exist"))
120
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
121
+ throw error;
122
+ });
123
+ }
124
+ async _doCreateContext(browser) {
125
+ return browser.newContext(this.config.browser.contextOptions);
126
+ }
127
+ }
128
+ class CdpContextFactory extends BaseContextFactory {
129
+ constructor(config) {
130
+ super("cdp", config);
131
+ }
132
+ async _doObtainBrowser() {
133
+ return playwright.chromium.connectOverCDP(this.config.browser.cdpEndpoint, { headers: this.config.browser.cdpHeaders });
134
+ }
135
+ async _doCreateContext(browser) {
136
+ return this.config.browser.isolated ? await browser.newContext() : browser.contexts()[0];
137
+ }
138
+ }
139
+ class RemoteContextFactory extends BaseContextFactory {
140
+ constructor(config) {
141
+ super("remote", config);
142
+ }
143
+ async _doObtainBrowser() {
144
+ const url = new URL(this.config.browser.remoteEndpoint);
145
+ url.searchParams.set("browser", this.config.browser.browserName);
146
+ if (this.config.browser.launchOptions)
147
+ url.searchParams.set("launch-options", JSON.stringify(this.config.browser.launchOptions));
148
+ return playwright[this.config.browser.browserName].connect(String(url));
149
+ }
150
+ async _doCreateContext(browser) {
151
+ return browser.newContext();
152
+ }
153
+ }
154
+ class PersistentContextFactory {
155
+ constructor(config) {
156
+ this.name = "persistent";
157
+ this.description = "Create a new persistent browser context";
158
+ this._userDataDirs = /* @__PURE__ */ new Set();
159
+ this.config = config;
160
+ }
161
+ async createContext(clientInfo) {
162
+ await injectCdpPort(this.config.browser);
163
+ (0, import_log.testDebug)("create browser context (persistent)");
164
+ const userDataDir = this.config.browser.userDataDir ?? await this._createUserDataDir(clientInfo);
165
+ const tracesDir = await computeTracesDir(this.config, clientInfo);
166
+ if (tracesDir && this.config.saveTrace)
167
+ await startTraceServer(this.config, tracesDir);
168
+ this._userDataDirs.add(userDataDir);
169
+ (0, import_log.testDebug)("lock user data dir", userDataDir);
170
+ const browserType = playwright[this.config.browser.browserName];
171
+ for (let i = 0; i < 5; i++) {
172
+ const launchOptions = {
173
+ tracesDir,
174
+ ...this.config.browser.launchOptions,
175
+ ...this.config.browser.contextOptions,
176
+ handleSIGINT: false,
177
+ handleSIGTERM: false,
178
+ ignoreDefaultArgs: [
179
+ "--disable-extensions"
180
+ ],
181
+ assistantMode: true
182
+ };
183
+ try {
184
+ const browserContext = await browserType.launchPersistentContext(userDataDir, launchOptions);
185
+ await addInitScript(browserContext, this.config.browser.initScript);
186
+ const close = (afterClose) => this._closeBrowserContext(browserContext, userDataDir, afterClose);
187
+ return { browserContext, close };
188
+ } catch (error) {
189
+ if (error.message.includes("Executable doesn't exist"))
190
+ throw new Error(`Browser specified in your config is not installed. Either install it (likely) or change the config.`);
191
+ if (error.message.includes("ProcessSingleton") || error.message.includes("Invalid URL")) {
192
+ await new Promise((resolve) => setTimeout(resolve, 1e3));
193
+ continue;
194
+ }
195
+ throw error;
196
+ }
197
+ }
198
+ throw new Error(`Browser is already in use for ${userDataDir}, use --isolated to run multiple instances of the same browser`);
199
+ }
200
+ async _closeBrowserContext(browserContext, userDataDir, afterClose) {
201
+ (0, import_log.testDebug)("close browser context (persistent)");
202
+ (0, import_log.testDebug)("release user data dir", userDataDir);
203
+ await browserContext.close().catch(() => {
204
+ });
205
+ await afterClose();
206
+ this._userDataDirs.delete(userDataDir);
207
+ (0, import_log.testDebug)("close browser context complete (persistent)");
208
+ }
209
+ async _createUserDataDir(clientInfo) {
210
+ const dir = process.env.PWMCP_PROFILES_DIR_FOR_TEST ?? import_registry.registryDirectory;
211
+ const browserToken = this.config.browser.launchOptions?.channel ?? this.config.browser?.browserName;
212
+ const rootPath = (0, import_server2.firstRootPath)(clientInfo);
213
+ const rootPathToken = rootPath ? `-${createHash(rootPath)}` : "";
214
+ const result = import_path.default.join(dir, `mcp-${browserToken}${rootPathToken}`);
215
+ await import_fs.default.promises.mkdir(result, { recursive: true });
216
+ return result;
217
+ }
218
+ }
219
+ async function injectCdpPort(browserConfig) {
220
+ if (browserConfig.browserName === "chromium")
221
+ browserConfig.launchOptions.cdpPort = await findFreePort();
222
+ }
223
+ async function findFreePort() {
224
+ return new Promise((resolve, reject) => {
225
+ const server = import_net.default.createServer();
226
+ server.listen(0, () => {
227
+ const { port } = server.address();
228
+ server.close(() => resolve(port));
229
+ });
230
+ server.on("error", reject);
231
+ });
232
+ }
233
+ async function startTraceServer(config, tracesDir) {
234
+ if (!config.saveTrace)
235
+ return;
236
+ const server = await (0, import_server.startTraceViewerServer)();
237
+ const urlPrefix = server.urlPrefix("human-readable");
238
+ const url = urlPrefix + "/trace/index.html?trace=" + tracesDir + "/trace.json";
239
+ console.error("\nTrace viewer listening on " + url);
240
+ }
241
+ function createHash(data) {
242
+ return import_crypto.default.createHash("sha256").update(data).digest("hex").slice(0, 7);
243
+ }
244
+ async function addInitScript(browserContext, initScript) {
245
+ for (const scriptPath of initScript ?? [])
246
+ await browserContext.addInitScript({ path: import_path.default.resolve(scriptPath) });
247
+ }
248
+ class SharedContextFactory {
249
+ static create(config) {
250
+ if (SharedContextFactory._instance)
251
+ throw new Error("SharedContextFactory already exists");
252
+ const baseConfig = { ...config, sharedBrowserContext: false };
253
+ const baseFactory = contextFactory(baseConfig);
254
+ SharedContextFactory._instance = new SharedContextFactory(baseFactory);
255
+ return SharedContextFactory._instance;
256
+ }
257
+ constructor(baseFactory) {
258
+ this._baseFactory = baseFactory;
259
+ }
260
+ async createContext(clientInfo, abortSignal, toolName) {
261
+ if (!this._contextPromise) {
262
+ (0, import_log.testDebug)("create shared browser context");
263
+ this._contextPromise = this._baseFactory.createContext(clientInfo, abortSignal, toolName);
264
+ }
265
+ const { browserContext } = await this._contextPromise;
266
+ (0, import_log.testDebug)(`shared context client connected`);
267
+ return {
268
+ browserContext,
269
+ close: async () => {
270
+ (0, import_log.testDebug)(`shared context client disconnected`);
271
+ }
272
+ };
273
+ }
274
+ static async dispose() {
275
+ await SharedContextFactory._instance?._dispose();
276
+ }
277
+ async _dispose() {
278
+ const contextPromise = this._contextPromise;
279
+ this._contextPromise = void 0;
280
+ if (!contextPromise)
281
+ return;
282
+ const { close } = await contextPromise;
283
+ await close(async () => {
284
+ });
285
+ }
286
+ }
287
+ async function computeTracesDir(config, clientInfo) {
288
+ if (!config.saveTrace && !config.capabilities?.includes("tracing"))
289
+ return;
290
+ return await (0, import_config.outputFile)(config, clientInfo, `traces`, { origin: "code", reason: "Collecting trace" });
291
+ }
292
+ // Annotate the CommonJS export names for ESM import in node:
293
+ 0 && (module.exports = {
294
+ SharedContextFactory,
295
+ contextFactory
296
+ });
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var browserServerBackend_exports = {};
20
+ __export(browserServerBackend_exports, {
21
+ BrowserServerBackend: () => BrowserServerBackend
22
+ });
23
+ module.exports = __toCommonJS(browserServerBackend_exports);
24
+ var import_context = require("./context");
25
+ var import_log = require("../log");
26
+ var import_response = require("./response");
27
+ var import_sessionLog = require("./sessionLog");
28
+ var import_tools = require("./tools");
29
+ var import_tool = require("../sdk/tool");
30
+ class BrowserServerBackend {
31
+ constructor(config, factory) {
32
+ this._config = config;
33
+ this._browserContextFactory = factory;
34
+ this._tools = (0, import_tools.filteredTools)(config);
35
+ }
36
+ async initialize(server, clientInfo) {
37
+ this._sessionLog = this._config.saveSession ? await import_sessionLog.SessionLog.create(this._config, clientInfo) : void 0;
38
+ this._context = new import_context.Context({
39
+ config: this._config,
40
+ browserContextFactory: this._browserContextFactory,
41
+ sessionLog: this._sessionLog,
42
+ clientInfo
43
+ });
44
+ }
45
+ async listTools() {
46
+ return this._tools.map((tool) => (0, import_tool.toMcpTool)(tool.schema));
47
+ }
48
+ async callTool(name, rawArguments) {
49
+ const tool = this._tools.find((tool2) => tool2.schema.name === name);
50
+ if (!tool)
51
+ throw new Error(`Tool "${name}" not found`);
52
+ const parsedArguments = tool.schema.inputSchema.parse(rawArguments || {});
53
+ const context = this._context;
54
+ const response = new import_response.Response(context, name, parsedArguments);
55
+ response.logBegin();
56
+ context.setRunningTool(name);
57
+ try {
58
+ await tool.handle(context, parsedArguments, response);
59
+ await response.finish();
60
+ this._sessionLog?.logResponse(response);
61
+ } catch (error) {
62
+ response.addError(String(error));
63
+ } finally {
64
+ context.setRunningTool(void 0);
65
+ }
66
+ response.logEnd();
67
+ return response.serialize();
68
+ }
69
+ serverClosed() {
70
+ void this._context?.dispose().catch(import_log.logUnhandledError);
71
+ }
72
+ }
73
+ // Annotate the CommonJS export names for ESM import in node:
74
+ 0 && (module.exports = {
75
+ BrowserServerBackend
76
+ });
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var codegen_exports = {};
20
+ __export(codegen_exports, {
21
+ escapeWithQuotes: () => escapeWithQuotes,
22
+ formatObject: () => formatObject,
23
+ quote: () => quote
24
+ });
25
+ module.exports = __toCommonJS(codegen_exports);
26
+ function escapeWithQuotes(text, char = "'") {
27
+ const stringified = JSON.stringify(text);
28
+ const escapedText = stringified.substring(1, stringified.length - 1).replace(/\\"/g, '"');
29
+ if (char === "'")
30
+ return char + escapedText.replace(/[']/g, "\\'") + char;
31
+ if (char === '"')
32
+ return char + escapedText.replace(/["]/g, '\\"') + char;
33
+ if (char === "`")
34
+ return char + escapedText.replace(/[`]/g, "\\`") + char;
35
+ throw new Error("Invalid escape char");
36
+ }
37
+ function quote(text) {
38
+ return escapeWithQuotes(text, "'");
39
+ }
40
+ function formatObject(value, indent = " ", mode = "multiline") {
41
+ if (typeof value === "string")
42
+ return quote(value);
43
+ if (Array.isArray(value))
44
+ return `[${value.map((o) => formatObject(o)).join(", ")}]`;
45
+ if (typeof value === "object") {
46
+ const keys = Object.keys(value).filter((key) => value[key] !== void 0).sort();
47
+ if (!keys.length)
48
+ return "{}";
49
+ const tokens = [];
50
+ for (const key of keys)
51
+ tokens.push(`${key}: ${formatObject(value[key])}`);
52
+ if (mode === "multiline")
53
+ return `{
54
+ ${tokens.join(`,
55
+ ${indent}`)}
56
+ }`;
57
+ return `{ ${tokens.join(", ")} }`;
58
+ }
59
+ return String(value);
60
+ }
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ escapeWithQuotes,
64
+ formatObject,
65
+ quote
66
+ });