program.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. function addTestCommand(program3) {
  50. const command = program3.command("test [test-filter...]");
  51. command.description("run tests with Playwright Test");
  52. const options = testOptions.sort((a, b) => a[0].replace(/-/g, "").localeCompare(b[0].replace(/-/g, "")));
  53. options.forEach(([name, { description, choices, preset }]) => {
  54. const option = command.createOption(name, description);
  55. if (choices)
  56. option.choices(choices);
  57. if (preset)
  58. option.preset(preset);
  59. command.addOption(option);
  60. return command;
  61. });
  62. command.action(async (args, opts) => {
  63. try {
  64. await runTests(args, opts);
  65. } catch (e) {
  66. console.error(e);
  67. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  68. }
  69. });
  70. command.addHelpText("afterAll", `
  71. Arguments [test-filter...]:
  72. Pass arguments to filter test files. Each argument is treated as a regular expression. Matching is performed against the absolute file paths.
  73. Examples:
  74. $ npx playwright test my.spec.ts
  75. $ npx playwright test some.spec.ts:42
  76. $ npx playwright test --headed
  77. $ npx playwright test --project=webkit`);
  78. }
  79. function addClearCacheCommand(program3) {
  80. const command = program3.command("clear-cache");
  81. command.description("clears build and test caches");
  82. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  83. command.action(async (opts) => {
  84. const runner = new import_testRunner.TestRunner((0, import_configLoader.resolveConfigLocation)(opts.config), {});
  85. const { status } = await runner.clearCache((0, import_reporters.createErrorCollectingReporter)(import_base.terminalScreen));
  86. const exitCode = status === "interrupted" ? 130 : status === "passed" ? 0 : 1;
  87. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode);
  88. });
  89. }
  90. function addDevServerCommand(program3) {
  91. const command = program3.command("dev-server", { hidden: true });
  92. command.description("start dev server");
  93. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  94. command.action(async (options) => {
  95. const runner = new import_testRunner.TestRunner((0, import_configLoader.resolveConfigLocation)(options.config), {});
  96. await runner.startDevServer((0, import_reporters.createErrorCollectingReporter)(import_base.terminalScreen), "in-process");
  97. });
  98. }
  99. function addTestServerCommand(program3) {
  100. const command = program3.command("test-server", { hidden: true });
  101. command.description("start test server");
  102. command.option("-c, --config <file>", `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  103. command.option("--host <host>", "Host to start the server on", "localhost");
  104. command.option("--port <port>", "Port to start the server on", "0");
  105. command.action((opts) => runTestServer(opts));
  106. }
  107. function addShowReportCommand(program3) {
  108. const command = program3.command("show-report [report]");
  109. command.description("show HTML report");
  110. command.action((report, options) => (0, import_html.showHTMLReport)(report, options.host, +options.port));
  111. command.option("--host <host>", "Host to serve report on", "localhost");
  112. command.option("--port <port>", "Port to serve report on", "9323");
  113. command.addHelpText("afterAll", `
  114. Arguments [report]:
  115. When specified, opens given report, otherwise opens last generated report.
  116. Examples:
  117. $ npx playwright show-report
  118. $ npx playwright show-report playwright-report`);
  119. }
  120. function addMergeReportsCommand(program3) {
  121. const command = program3.command("merge-reports [dir]");
  122. command.description("merge multiple blob reports (for sharded tests) into a single report");
  123. command.action(async (dir, options) => {
  124. try {
  125. await mergeReports(dir, options);
  126. } catch (e) {
  127. console.error(e);
  128. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  129. }
  130. });
  131. command.option("-c, --config <file>", `Configuration file. Can be used to specify additional configuration for the output report.`);
  132. command.option("--reporter <reporter>", `Reporter to use, comma-separated, can be ${import_config.builtInReporters.map((name) => `"${name}"`).join(", ")} (default: "${import_config.defaultReporter}")`);
  133. command.addHelpText("afterAll", `
  134. Arguments [dir]:
  135. Directory containing blob reports.
  136. Examples:
  137. $ npx playwright merge-reports playwright-report`);
  138. }
  139. async function runTests(args, opts) {
  140. await (0, import_utils.startProfiling)();
  141. const cliOverrides = overridesFromOptions(opts);
  142. const config = await (0, import_configLoader.loadConfigFromFile)(opts.config, cliOverrides, opts.deps === false);
  143. config.cliArgs = args;
  144. config.cliGrep = opts.grep;
  145. config.cliOnlyChanged = opts.onlyChanged === true ? "HEAD" : opts.onlyChanged;
  146. config.cliGrepInvert = opts.grepInvert;
  147. config.cliListOnly = !!opts.list;
  148. config.cliProjectFilter = opts.project || void 0;
  149. config.cliPassWithNoTests = !!opts.passWithNoTests;
  150. config.cliLastFailed = !!opts.lastFailed;
  151. (0, import_projectUtils.filterProjects)(config.projects, config.cliProjectFilter);
  152. if (opts.ui || opts.uiHost || opts.uiPort) {
  153. if (opts.onlyChanged)
  154. 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.`);
  155. const status2 = await testServer.runUIMode(opts.config, cliOverrides, {
  156. host: opts.uiHost,
  157. port: opts.uiPort ? +opts.uiPort : void 0,
  158. args,
  159. grep: opts.grep,
  160. grepInvert: opts.grepInvert,
  161. project: opts.project || void 0,
  162. reporter: Array.isArray(opts.reporter) ? opts.reporter : opts.reporter ? [opts.reporter] : void 0
  163. });
  164. await (0, import_utils.stopProfiling)("runner");
  165. const exitCode2 = status2 === "interrupted" ? 130 : status2 === "passed" ? 0 : 1;
  166. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode2);
  167. return;
  168. }
  169. if (process.env.PWTEST_WATCH) {
  170. if (opts.onlyChanged)
  171. 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.`);
  172. const status2 = await (0, import_watchMode.runWatchModeLoop)(
  173. (0, import_configLoader.resolveConfigLocation)(opts.config),
  174. {
  175. projects: opts.project,
  176. files: args,
  177. grep: opts.grep
  178. }
  179. );
  180. await (0, import_utils.stopProfiling)("runner");
  181. const exitCode2 = status2 === "interrupted" ? 130 : status2 === "passed" ? 0 : 1;
  182. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode2);
  183. return;
  184. }
  185. const status = await (0, import_testRunner.runAllTestsWithConfig)(config);
  186. await (0, import_utils.stopProfiling)("runner");
  187. const exitCode = status === "interrupted" ? 130 : status === "passed" ? 0 : 1;
  188. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode);
  189. }
  190. async function runTestServer(opts) {
  191. const host = opts.host || "localhost";
  192. const port = opts.port ? +opts.port : 0;
  193. const status = await testServer.runTestServer(opts.config, {}, { host, port });
  194. const exitCode = status === "interrupted" ? 130 : status === "passed" ? 0 : 1;
  195. (0, import_utils.gracefullyProcessExitDoNotHang)(exitCode);
  196. }
  197. async function mergeReports(reportDir, opts) {
  198. const configFile = opts.config;
  199. const config = configFile ? await (0, import_configLoader.loadConfigFromFile)(configFile) : await (0, import_configLoader.loadEmptyConfigForMergeReports)();
  200. const dir = import_path.default.resolve(process.cwd(), reportDir || "");
  201. const dirStat = await import_fs.default.promises.stat(dir).catch((e) => null);
  202. if (!dirStat)
  203. throw new Error("Directory does not exist: " + dir);
  204. if (!dirStat.isDirectory())
  205. throw new Error(`"${dir}" is not a directory`);
  206. let reporterDescriptions = resolveReporterOption(opts.reporter);
  207. if (!reporterDescriptions && configFile)
  208. reporterDescriptions = config.config.reporter;
  209. if (!reporterDescriptions)
  210. reporterDescriptions = [[import_config.defaultReporter]];
  211. const rootDirOverride = configFile ? config.config.rootDir : void 0;
  212. await (0, import_merge.createMergedReport)(config, dir, reporterDescriptions, rootDirOverride);
  213. (0, import_utils.gracefullyProcessExitDoNotHang)(0);
  214. }
  215. function overridesFromOptions(options) {
  216. const overrides = {
  217. failOnFlakyTests: options.failOnFlakyTests ? true : void 0,
  218. forbidOnly: options.forbidOnly ? true : void 0,
  219. fullyParallel: options.fullyParallel ? true : void 0,
  220. globalTimeout: options.globalTimeout ? parseInt(options.globalTimeout, 10) : void 0,
  221. maxFailures: options.x ? 1 : options.maxFailures ? parseInt(options.maxFailures, 10) : void 0,
  222. outputDir: options.output ? import_path.default.resolve(process.cwd(), options.output) : void 0,
  223. quiet: options.quiet ? options.quiet : void 0,
  224. repeatEach: options.repeatEach ? parseInt(options.repeatEach, 10) : void 0,
  225. retries: options.retries ? parseInt(options.retries, 10) : void 0,
  226. reporter: resolveReporterOption(options.reporter),
  227. shard: resolveShardOption(options.shard),
  228. timeout: options.timeout ? parseInt(options.timeout, 10) : void 0,
  229. tsconfig: options.tsconfig ? import_path.default.resolve(process.cwd(), options.tsconfig) : void 0,
  230. ignoreSnapshots: options.ignoreSnapshots ? !!options.ignoreSnapshots : void 0,
  231. updateSnapshots: options.updateSnapshots,
  232. updateSourceMethod: options.updateSourceMethod,
  233. workers: options.workers
  234. };
  235. if (options.browser) {
  236. const browserOpt = options.browser.toLowerCase();
  237. if (!["all", "chromium", "firefox", "webkit"].includes(browserOpt))
  238. throw new Error(`Unsupported browser "${options.browser}", must be one of "all", "chromium", "firefox" or "webkit"`);
  239. const browserNames = browserOpt === "all" ? ["chromium", "firefox", "webkit"] : [browserOpt];
  240. overrides.projects = browserNames.map((browserName) => {
  241. return {
  242. name: browserName,
  243. use: { browserName }
  244. };
  245. });
  246. }
  247. if (options.headed || options.debug)
  248. overrides.use = { headless: false };
  249. if (!options.ui && options.debug) {
  250. overrides.debug = true;
  251. process.env.PWDEBUG = "1";
  252. }
  253. if (!options.ui && options.trace) {
  254. overrides.use = overrides.use || {};
  255. overrides.use.trace = options.trace;
  256. }
  257. if (overrides.tsconfig && !import_fs.default.existsSync(overrides.tsconfig))
  258. throw new Error(`--tsconfig "${options.tsconfig}" does not exist`);
  259. return overrides;
  260. }
  261. function resolveReporterOption(reporter) {
  262. if (!reporter || !reporter.length)
  263. return void 0;
  264. return reporter.split(",").map((r) => [resolveReporter(r)]);
  265. }
  266. function resolveShardOption(shard) {
  267. if (!shard)
  268. return void 0;
  269. const shardPair = shard.split("/");
  270. if (shardPair.length !== 2) {
  271. throw new Error(
  272. `--shard "${shard}", expected format is "current/all", 1-based, for example "3/5".`
  273. );
  274. }
  275. const current = parseInt(shardPair[0], 10);
  276. const total = parseInt(shardPair[1], 10);
  277. if (isNaN(total) || total < 1)
  278. throw new Error(`--shard "${shard}" total must be a positive number`);
  279. if (isNaN(current) || current < 1 || current > total) {
  280. throw new Error(
  281. `--shard "${shard}" current must be a positive number, not greater than shard total`
  282. );
  283. }
  284. return { current, total };
  285. }
  286. function resolveReporter(id) {
  287. if (import_config.builtInReporters.includes(id))
  288. return id;
  289. const localPath = import_path.default.resolve(process.cwd(), id);
  290. if (import_fs.default.existsSync(localPath))
  291. return localPath;
  292. return require.resolve(id, { paths: [process.cwd()] });
  293. }
  294. const kTraceModes = ["on", "off", "on-first-retry", "on-all-retries", "retain-on-failure", "retain-on-first-failure"];
  295. const testOptions = [
  296. /* deprecated */
  297. ["--browser <browser>", { description: `Browser to use for tests, one of "all", "chromium", "firefox" or "webkit" (default: "chromium")` }],
  298. ["-c, --config <file>", { description: `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"` }],
  299. ["--debug", { description: `Run tests with Playwright Inspector. Shortcut for "PWDEBUG=1" environment variable and "--timeout=0 --max-failures=1 --headed --workers=1" options` }],
  300. ["--fail-on-flaky-tests", { description: `Fail if any test is flagged as flaky (default: false)` }],
  301. ["--forbid-only", { description: `Fail if test.only is called (default: false)` }],
  302. ["--fully-parallel", { description: `Run all tests in parallel (default: false)` }],
  303. ["--global-timeout <timeout>", { description: `Maximum time this test suite can run in milliseconds (default: unlimited)` }],
  304. ["-g, --grep <grep>", { description: `Only run tests matching this regular expression (default: ".*")` }],
  305. ["--grep-invert <grep>", { description: `Only run tests that do not match this regular expression` }],
  306. ["--headed", { description: `Run tests in headed browsers (default: headless)` }],
  307. ["--ignore-snapshots", { description: `Ignore screenshot and snapshot expectations` }],
  308. ["--last-failed", { description: `Only re-run the failures` }],
  309. ["--list", { description: `Collect all the tests and report them, but do not run` }],
  310. ["--max-failures <N>", { description: `Stop after the first N failures` }],
  311. ["--no-deps", { description: `Do not run project dependencies` }],
  312. ["--output <dir>", { description: `Folder for output artifacts (default: "test-results")` }],
  313. ["--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.` }],
  314. ["--pass-with-no-tests", { description: `Makes test run succeed even if no tests were found` }],
  315. ["--project <project-name...>", { description: `Only run tests from the specified list of projects, supports '*' wildcard (default: run all projects)` }],
  316. ["--quiet", { description: `Suppress stdio` }],
  317. ["--repeat-each <N>", { description: `Run each test N times (default: 1)` }],
  318. ["--reporter <reporter>", { description: `Reporter to use, comma-separated, can be ${import_config.builtInReporters.map((name) => `"${name}"`).join(", ")} (default: "${import_config.defaultReporter}")` }],
  319. ["--retries <retries>", { description: `Maximum retry count for flaky tests, zero for no retries (default: no retries)` }],
  320. ["--shard <shard>", { description: `Shard tests and execute only the selected shard, specify in the form "current/all", 1-based, for example "3/5"` }],
  321. ["--timeout <timeout>", { description: `Specify test timeout threshold in milliseconds, zero for unlimited (default: ${import_config.defaultTimeout})` }],
  322. ["--trace <mode>", { description: `Force tracing mode`, choices: kTraceModes }],
  323. ["--tsconfig <path>", { description: `Path to a single tsconfig applicable to all imported files (default: look up tsconfig for each imported file separately)` }],
  324. ["--ui", { description: `Run tests in interactive UI mode` }],
  325. ["--ui-host <host>", { description: `Host to serve UI on; specifying this option opens UI in a browser tab` }],
  326. ["--ui-port <port>", { description: `Port to serve UI on, 0 for any free port; specifying this option opens UI in a browser tab` }],
  327. ["-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" }],
  328. ["--update-source-method <method>", { description: `Chooses the way source is updated (default: "patch")`, choices: ["overwrite", "3way", "patch"] }],
  329. ["-j, --workers <workers>", { description: `Number of concurrent workers or percentage of logical CPU cores, use 1 to run in a single worker (default: 50%)` }],
  330. ["-x", { description: `Stop after the first failure` }]
  331. ];
  332. addTestCommand(import_program.program);
  333. addShowReportCommand(import_program.program);
  334. addMergeReportsCommand(import_program.program);
  335. addClearCacheCommand(import_program.program);
  336. addDevServerCommand(import_program.program);
  337. addTestServerCommand(import_program.program);
  338. // Annotate the CommonJS export names for ESM import in node:
  339. 0 && (module.exports = {
  340. program
  341. });