program.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 program_exports = {};
  30. __export(program_exports, {
  31. program: () => import_program2.program
  32. });
  33. module.exports = __toCommonJS(program_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_path = __toESM(require("path"));
  36. var import_program = require("playwright-core/lib/cli/program");
  37. var import_utils = require("playwright-core/lib/utils");
  38. var import_config = require("./common/config");
  39. var import_configLoader = require("./common/configLoader");
  40. var import_program2 = require("playwright-core/lib/cli/program");
  41. var import_base = require("./reporters/base");
  42. var import_html = require("./reporters/html");
  43. var import_merge = require("./reporters/merge");
  44. var import_projectUtils = require("./runner/projectUtils");
  45. var testServer = __toESM(require("./runner/testServer"));
  46. var import_watchMode = require("./runner/watchMode");
  47. var import_testRunner = require("./runner/testRunner");
  48. var import_reporters = require("./runner/reporters");
  49. var mcp = __toESM(require("./mcp/sdk/exports"));
  50. var import_testBackend = require("./mcp/test/testBackend");
  51. var import_program3 = require("./mcp/program");
  52. var import_watchdog = require("./mcp/browser/watchdog");
  53. var import_generateAgents = require("./agents/generateAgents");
  54. const packageJSON = require("../package.json");
  55. function addTestCommand(program3) {
  56. const command = program3.command("test [test-filter...]");
  57. command.description("run tests with Playwright Test");
  58. const options = testOptions.sort((a, b) => a[0].replace(/-/g, "").localeCompare(b[0].replace(/-/g, "")));
  59. options.forEach(([name, { description, choices, preset }]) => {
  60. const option = command.createOption(name, description);
  61. if (choices)
  62. option.choices(choices);
  63. if (preset)
  64. option.preset(preset);
  65. command.addOption(option);
  66. return command;
  67. });
  68. command.action(async (args, opts) => {
  69. try {
  70. await runTests(args, opts);
  71. } catch (e) {
  72. console.error(e);
  73. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  74. }
  75. });
  76. command.addHelpText("afterAll", `
  77. Arguments [test-filter...]:
  78. Pass arguments to filter test files. Each argument is treated as a regular expression. Matching is performed against the absolute file paths.
  79. Examples:
  80. $ npx playwright test my.spec.ts
  81. $ npx playwright test some.spec.ts:42
  82. $ npx playwright test --headed
  83. $ npx playwright test --project=webkit`);
  84. }
  85. function addClearCacheCommand(program3) {
  86. const command = program3.command("clear-cache");
  87. command.description("clears build and test caches");
  88. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  89. command.action(async (opts) => {
  90. const runner = new import_testRunner.TestRunner((0, import_configLoader.resolveConfigLocation)(opts.config), {});
  91. const { status } = await runner.clearCache((0, import_reporters.createErrorCollectingReporter)(import_base.terminalScreen));
  92. const exitCode = status === "interrupted" ? 130 : status === "passed" ? 0 : 1;
  93. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode);
  94. });
  95. }
  96. function addDevServerCommand(program3) {
  97. const command = program3.command("dev-server", { hidden: true });
  98. command.description("start dev server");
  99. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  100. command.action(async (options) => {
  101. const runner = new import_testRunner.TestRunner((0, import_configLoader.resolveConfigLocation)(options.config), {});
  102. await runner.startDevServer((0, import_reporters.createErrorCollectingReporter)(import_base.terminalScreen), "in-process");
  103. });
  104. }
  105. function addTestServerCommand(program3) {
  106. const command = program3.command("test-server", { hidden: true });
  107. command.description("start test server");
  108. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  109. command.option("--host <host>", "Host to start the server on", "localhost");
  110. command.option("--port <port>", "Port to start the server on", "0");
  111. command.action((opts) => runTestServer(opts));
  112. }
  113. function addShowReportCommand(program3) {
  114. const command = program3.command("show-report [report]");
  115. command.description("show HTML report");
  116. command.action((report, options) => (0, import_html.showHTMLReport)(report, options.host, +options.port));
  117. command.option("--host <host>", "Host to serve report on", "localhost");
  118. command.option("--port <port>", "Port to serve report on", "9323");
  119. command.addHelpText("afterAll", `
  120. Arguments [report]:
  121. When specified, opens given report, otherwise opens last generated report.
  122. Examples:
  123. $ npx playwright show-report
  124. $ npx playwright show-report playwright-report`);
  125. }
  126. function addMergeReportsCommand(program3) {
  127. const command = program3.command("merge-reports [dir]");
  128. command.description("merge multiple blob reports (for sharded tests) into a single report");
  129. command.action(async (dir, options) => {
  130. try {
  131. await mergeReports(dir, options);
  132. } catch (e) {
  133. console.error(e);
  134. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  135. }
  136. });
  137. command.option("-c, --config <file>", `Configuration file. Can be used to specify additional configuration for the output report.`);
  138. command.option("--reporter <reporter>", `Reporter to use, comma-separated, can be ${import_config.builtInReporters.map((name) => `"${name}"`).join(", ")} (default: "${import_config.defaultReporter}")`);
  139. command.addHelpText("afterAll", `
  140. Arguments [dir]:
  141. Directory containing blob reports.
  142. Examples:
  143. $ npx playwright merge-reports playwright-report`);
  144. }
  145. function addBrowserMCPServerCommand(program3) {
  146. const command = program3.command("run-mcp-server", { hidden: true });
  147. command.description("Interact with the browser over MCP");
  148. (0, import_program3.decorateCommand)(command, packageJSON.version);
  149. }
  150. function addTestMCPServerCommand(program3) {
  151. const command = program3.command("run-test-mcp-server", { hidden: true });
  152. command.description("Interact with the test runner over MCP");
  153. command.option("--headless", "run browser in headless mode, headed by default");
  154. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  155. command.option("--host <host>", "host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.");
  156. command.option("--port <port>", "port to listen on for SSE transport.");
  157. command.action(async (options) => {
  158. (0, import_watchdog.setupExitWatchdog)();
  159. const factory = {
  160. name: "Playwright Test Runner",
  161. nameInConfig: "playwright-test-runner",
  162. version: packageJSON.version,
  163. create: () => new import_testBackend.TestServerBackend(options.config, { muteConsole: options.port === void 0, headless: options.headless })
  164. };
  165. await mcp.start(factory, { port: options.port === void 0 ? void 0 : +options.port, host: options.host });
  166. });
  167. }
  168. function addInitAgentsCommand(program3) {
  169. const command = program3.command("init-agents");
  170. command.description("Initialize repository agents");
  171. const option = command.createOption("--loop <loop>", "Agentic loop provider");
  172. option.choices(["claude", "copilot", "opencode", "vscode", "vscode-legacy"]);
  173. command.addOption(option);
  174. command.option("-c, --config <file>", `Configuration file to find a project to use for seed test`);
  175. command.option("--project <project>", "Project to use for seed test");
  176. command.option("--prompts", "Whether to include prompts in the agent initialization");
  177. command.action(async (opts) => {
  178. const config = await (0, import_configLoader.loadConfigFromFile)(opts.config);
  179. if (opts.loop === "opencode") {
  180. await import_generateAgents.OpencodeGenerator.init(config, opts.project, opts.prompts);
  181. } else if (opts.loop === "vscode-legacy") {
  182. await import_generateAgents.VSCodeGenerator.init(config, opts.project);
  183. } else if (opts.loop === "claude") {
  184. await import_generateAgents.ClaudeGenerator.init(config, opts.project, opts.prompts);
  185. } else {
  186. await import_generateAgents.CopilotGenerator.init(config, opts.project, opts.prompts);
  187. return;
  188. }
  189. });
  190. }
  191. async function runTests(args, opts) {
  192. await (0, import_utils.startProfiling)();
  193. const cliOverrides = overridesFromOptions(opts);
  194. const config = await (0, import_configLoader.loadConfigFromFile)(opts.config, cliOverrides, opts.deps === false);
  195. config.cliArgs = args;
  196. config.cliGrep = opts.grep;
  197. config.cliOnlyChanged = opts.onlyChanged === true ? "HEAD" : opts.onlyChanged;
  198. config.cliGrepInvert = opts.grepInvert;
  199. config.cliListOnly = !!opts.list;
  200. config.cliProjectFilter = opts.project || void 0;
  201. config.cliPassWithNoTests = !!opts.passWithNoTests;
  202. config.cliLastFailed = !!opts.lastFailed;
  203. config.cliTestList = opts.testList ? import_path.default.resolve(process.cwd(), opts.testList) : void 0;
  204. config.cliTestListInvert = opts.testListInvert ? import_path.default.resolve(process.cwd(), opts.testListInvert) : void 0;
  205. (0, import_projectUtils.filterProjects)(config.projects, config.cliProjectFilter);
  206. if (opts.ui || opts.uiHost || opts.uiPort) {
  207. if (opts.onlyChanged)
  208. throw new Error(`--only-changed is not supported in UI mode. If you'd like that to change, see https://github.com/microsoft/playwright/issues/15075 for more details.`);
  209. const status2 = await testServer.runUIMode(opts.config, cliOverrides, {
  210. host: opts.uiHost,
  211. port: opts.uiPort ? +opts.uiPort : void 0,
  212. args,
  213. grep: opts.grep,
  214. grepInvert: opts.grepInvert,
  215. project: opts.project || void 0,
  216. reporter: Array.isArray(opts.reporter) ? opts.reporter : opts.reporter ? [opts.reporter] : void 0
  217. });
  218. await (0, import_utils.stopProfiling)("runner");
  219. const exitCode2 = status2 === "interrupted" ? 130 : status2 === "passed" ? 0 : 1;
  220. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode2);
  221. return;
  222. }
  223. if (process.env.PWTEST_WATCH) {
  224. if (opts.onlyChanged)
  225. throw new Error(`--only-changed is not supported in watch mode. If you'd like that to change, file an issue and let us know about your usecase for it.`);
  226. const status2 = await (0, import_watchMode.runWatchModeLoop)(
  227. (0, import_configLoader.resolveConfigLocation)(opts.config),
  228. {
  229. projects: opts.project,
  230. files: args,
  231. grep: opts.grep
  232. }
  233. );
  234. await (0, import_utils.stopProfiling)("runner");
  235. const exitCode2 = status2 === "interrupted" ? 130 : status2 === "passed" ? 0 : 1;
  236. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode2);
  237. return;
  238. }
  239. const status = await (0, import_testRunner.runAllTestsWithConfig)(config);
  240. await (0, import_utils.stopProfiling)("runner");
  241. const exitCode = status === "interrupted" ? 130 : status === "passed" ? 0 : 1;
  242. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode);
  243. }
  244. async function runTestServer(opts) {
  245. const host = opts.host || "localhost";
  246. const port = opts.port ? +opts.port : 0;
  247. const status = await testServer.runTestServer(opts.config, {}, { host, port });
  248. const exitCode = status === "interrupted" ? 130 : status === "passed" ? 0 : 1;
  249. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode);
  250. }
  251. async function mergeReports(reportDir, opts) {
  252. const configFile = opts.config;
  253. const config = configFile ? await (0, import_configLoader.loadConfigFromFile)(configFile) : await (0, import_configLoader.loadEmptyConfigForMergeReports)();
  254. const dir = import_path.default.resolve(process.cwd(), reportDir || "");
  255. const dirStat = await import_fs.default.promises.stat(dir).catch((e) => null);
  256. if (!dirStat)
  257. throw new Error("Directory does not exist: " + dir);
  258. if (!dirStat.isDirectory())
  259. throw new Error(`"${dir}" is not a directory`);
  260. let reporterDescriptions = resolveReporterOption(opts.reporter);
  261. if (!reporterDescriptions && configFile)
  262. reporterDescriptions = config.config.reporter;
  263. if (!reporterDescriptions)
  264. reporterDescriptions = [[import_config.defaultReporter]];
  265. const rootDirOverride = configFile ? config.config.rootDir : void 0;
  266. await (0, import_merge.createMergedReport)(config, dir, reporterDescriptions, rootDirOverride);
  267. (0, import_utils.gracefullyProcessExitDoNotHang)(0);
  268. }
  269. function overridesFromOptions(options) {
  270. const overrides = {
  271. failOnFlakyTests: options.failOnFlakyTests ? true : void 0,
  272. forbidOnly: options.forbidOnly ? true : void 0,
  273. fullyParallel: options.fullyParallel ? true : void 0,
  274. globalTimeout: options.globalTimeout ? parseInt(options.globalTimeout, 10) : void 0,
  275. maxFailures: options.x ? 1 : options.maxFailures ? parseInt(options.maxFailures, 10) : void 0,
  276. outputDir: options.output ? import_path.default.resolve(process.cwd(), options.output) : void 0,
  277. quiet: options.quiet ? options.quiet : void 0,
  278. repeatEach: options.repeatEach ? parseInt(options.repeatEach, 10) : void 0,
  279. retries: options.retries ? parseInt(options.retries, 10) : void 0,
  280. reporter: resolveReporterOption(options.reporter),
  281. shard: resolveShardOption(options.shard),
  282. timeout: options.timeout ? parseInt(options.timeout, 10) : void 0,
  283. tsconfig: options.tsconfig ? import_path.default.resolve(process.cwd(), options.tsconfig) : void 0,
  284. ignoreSnapshots: options.ignoreSnapshots ? !!options.ignoreSnapshots : void 0,
  285. updateSnapshots: options.updateSnapshots,
  286. updateSourceMethod: options.updateSourceMethod,
  287. workers: options.workers
  288. };
  289. if (options.browser) {
  290. const browserOpt = options.browser.toLowerCase();
  291. if (!["all", "chromium", "firefox", "webkit"].includes(browserOpt))
  292. throw new Error(`Unsupported browser "${options.browser}", must be one of "all", "chromium", "firefox" or "webkit"`);
  293. const browserNames = browserOpt === "all" ? ["chromium", "firefox", "webkit"] : [browserOpt];
  294. overrides.projects = browserNames.map((browserName) => {
  295. return {
  296. name: browserName,
  297. use: { browserName }
  298. };
  299. });
  300. }
  301. if (options.headed || options.debug)
  302. overrides.use = { headless: false };
  303. if (!options.ui && options.debug) {
  304. overrides.debug = true;
  305. process.env.PWDEBUG = "1";
  306. }
  307. if (!options.ui && options.trace) {
  308. overrides.use = overrides.use || {};
  309. overrides.use.trace = options.trace;
  310. }
  311. if (overrides.tsconfig && !import_fs.default.existsSync(overrides.tsconfig))
  312. throw new Error(`--tsconfig "${options.tsconfig}" does not exist`);
  313. return overrides;
  314. }
  315. function resolveReporterOption(reporter) {
  316. if (!reporter || !reporter.length)
  317. return void 0;
  318. return reporter.split(",").map((r) => [resolveReporter(r)]);
  319. }
  320. function resolveShardOption(shard) {
  321. if (!shard)
  322. return void 0;
  323. const shardPair = shard.split("/");
  324. if (shardPair.length !== 2) {
  325. throw new Error(
  326. `--shard "${shard}", expected format is "current/all", 1-based, for example "3/5".`
  327. );
  328. }
  329. const current = parseInt(shardPair[0], 10);
  330. const total = parseInt(shardPair[1], 10);
  331. if (isNaN(total) || total < 1)
  332. throw new Error(`--shard "${shard}" total must be a positive number`);
  333. if (isNaN(current) || current < 1 || current > total) {
  334. throw new Error(
  335. `--shard "${shard}" current must be a positive number, not greater than shard total`
  336. );
  337. }
  338. return { current, total };
  339. }
  340. function resolveReporter(id) {
  341. if (import_config.builtInReporters.includes(id))
  342. return id;
  343. const localPath = import_path.default.resolve(process.cwd(), id);
  344. if (import_fs.default.existsSync(localPath))
  345. return localPath;
  346. return require.resolve(id, { paths: [process.cwd()] });
  347. }
  348. const kTraceModes = ["on", "off", "on-first-retry", "on-all-retries", "retain-on-failure", "retain-on-first-failure"];
  349. const testOptions = [
  350. /* deprecated */
  351. ["--browser <browser>", { description: `Browser to use for tests, one of "all", "chromium", "firefox" or "webkit" (default: "chromium")` }],
  352. ["-c, --config <file>", { description: `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"` }],
  353. ["--debug", { description: `Run tests with Playwright Inspector. Shortcut for "PWDEBUG=1" environment variable and "--timeout=0 --max-failures=1 --headed --workers=1" options` }],
  354. ["--fail-on-flaky-tests", { description: `Fail if any test is flagged as flaky (default: false)` }],
  355. ["--forbid-only", { description: `Fail if test.only is called (default: false)` }],
  356. ["--fully-parallel", { description: `Run all tests in parallel (default: false)` }],
  357. ["--global-timeout <timeout>", { description: `Maximum time this test suite can run in milliseconds (default: unlimited)` }],
  358. ["-g, --grep <grep>", { description: `Only run tests matching this regular expression (default: ".*")` }],
  359. ["--grep-invert <grep>", { description: `Only run tests that do not match this regular expression` }],
  360. ["--headed", { description: `Run tests in headed browsers (default: headless)` }],
  361. ["--ignore-snapshots", { description: `Ignore screenshot and snapshot expectations` }],
  362. ["--last-failed", { description: `Only re-run the failures` }],
  363. ["--list", { description: `Collect all the tests and report them, but do not run` }],
  364. ["--max-failures <N>", { description: `Stop after the first N failures` }],
  365. ["--no-deps", { description: `Do not run project dependencies` }],
  366. ["--output <dir>", { description: `Folder for output artifacts (default: "test-results")` }],
  367. ["--only-changed [ref]", { description: `Only run test files that have been changed between 'HEAD' and 'ref'. Defaults to running all uncommitted changes. Only supports Git.` }],
  368. ["--pass-with-no-tests", { description: `Makes test run succeed even if no tests were found` }],
  369. ["--project <project-name...>", { description: `Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects)` }],
  370. ["--quiet", { description: `Suppress stdio` }],
  371. ["--repeat-each <N>", { description: `Run each test N times (default: 1)` }],
  372. ["--reporter <reporter>", { description: `Reporter to use, comma-separated, can be ${import_config.builtInReporters.map((name) => `"${name}"`).join(", ")} (default: "${import_config.defaultReporter}")` }],
  373. ["--retries <retries>", { description: `Maximum retry count for flaky tests, zero for no retries (default: no retries)` }],
  374. ["--shard <shard>", { description: `Shard tests and execute only the selected shard, specify in the form "current/all", 1-based, for example "3/5"` }],
  375. ["--test-list <file>", { description: `Path to a file containing a list of tests to run. See https://playwright.dev/docs/test-cli for more details.` }],
  376. ["--test-list-invert <file>", { description: `Path to a file containing a list of tests to skip. See https://playwright.dev/docs/test-cli for more details.` }],
  377. ["--timeout <timeout>", { description: `Specify test timeout threshold in milliseconds, zero for unlimited (default: ${import_config.defaultTimeout})` }],
  378. ["--trace <mode>", { description: `Force tracing mode`, choices: kTraceModes }],
  379. ["--tsconfig <path>", { description: `Path to a single tsconfig applicable to all imported files (default: look up tsconfig for each imported file separately)` }],
  380. ["--ui", { description: `Run tests in interactive UI mode` }],
  381. ["--ui-host <host>", { description: `Host to serve UI on; specifying this option opens UI in a browser tab` }],
  382. ["--ui-port <port>", { description: `Port to serve UI on, 0 for any free port; specifying this option opens UI in a browser tab` }],
  383. ["-u, --update-snapshots [mode]", { description: `Update snapshots with actual results. Running tests without the flag defaults to "missing"`, choices: ["all", "changed", "missing", "none"], preset: "changed" }],
  384. ["--update-source-method <method>", { description: `Chooses the way source is updated (default: "patch")`, choices: ["overwrite", "3way", "patch"] }],
  385. ["-j, --workers <workers>", { description: `Number of concurrent workers or percentage of logical CPU cores, use 1 to run in a single worker (default: 50%)` }],
  386. ["-x", { description: `Stop after the first failure` }]
  387. ];
  388. addTestCommand(import_program.program);
  389. addShowReportCommand(import_program.program);
  390. addMergeReportsCommand(import_program.program);
  391. addClearCacheCommand(import_program.program);
  392. addBrowserMCPServerCommand(import_program.program);
  393. addTestMCPServerCommand(import_program.program);
  394. addDevServerCommand(import_program.program);
  395. addTestServerCommand(import_program.program);
  396. addInitAgentsCommand(import_program.program);
  397. // Annotate the CommonJS export names for ESM import in node:
  398. 0 && (module.exports = {
  399. program
  400. });