cli.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. "use strict";
  2. var _fs = _interopRequireDefault(require("fs"));
  3. var _path = _interopRequireDefault(require("path"));
  4. var _runner = require("./runner/runner");
  5. var _utils = require("playwright-core/lib/utils");
  6. var _util = require("./util");
  7. var _html = require("./reporters/html");
  8. var _merge = require("./reporters/merge");
  9. var _configLoader = require("./common/configLoader");
  10. var _config = require("./common/config");
  11. var _program = _interopRequireDefault(require("playwright-core/lib/cli/program"));
  12. var _base = require("./reporters/base");
  13. var _esmLoaderHost = require("./common/esmLoaderHost");
  14. var _esmUtils = require("./transform/esmUtils");
  15. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16. /**
  17. * Copyright (c) Microsoft Corporation.
  18. *
  19. * Licensed under the Apache License, Version 2.0 (the 'License");
  20. * you may not use this file except in compliance with the License.
  21. * You may obtain a copy of the License at
  22. *
  23. * http://www.apache.org/licenses/LICENSE-2.0
  24. *
  25. * Unless required by applicable law or agreed to in writing, software
  26. * distributed under the License is distributed on an "AS IS" BASIS,
  27. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28. * See the License for the specific language governing permissions and
  29. * limitations under the License.
  30. */
  31. /* eslint-disable no-console */
  32. function addTestCommand(program) {
  33. const command = program.command('test [test-filter...]');
  34. command.description('run tests with Playwright Test');
  35. const options = testOptions.sort((a, b) => a[0].replace(/-/g, '').localeCompare(b[0].replace(/-/g, '')));
  36. options.forEach(([name, description]) => command.option(name, description));
  37. command.action(async (args, opts) => {
  38. try {
  39. await runTests(args, opts);
  40. } catch (e) {
  41. console.error(e);
  42. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  43. }
  44. });
  45. command.addHelpText('afterAll', `
  46. Arguments [test-filter...]:
  47. Pass arguments to filter test files. Each argument is treated as a regular expression. Matching is performed against the absolute file paths.
  48. Examples:
  49. $ npx playwright test my.spec.ts
  50. $ npx playwright test some.spec.ts:42
  51. $ npx playwright test --headed
  52. $ npx playwright test --project=webkit`);
  53. }
  54. function addListFilesCommand(program) {
  55. const command = program.command('list-files [file-filter...]', {
  56. hidden: true
  57. });
  58. command.description('List files with Playwright Test tests');
  59. command.option('-c, --config <file>', `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`);
  60. command.option('--project <project-name...>', `Only run tests from the specified list of projects (default: list all projects)`);
  61. command.action(async (args, opts) => {
  62. try {
  63. await listTestFiles(opts);
  64. } catch (e) {
  65. console.error(e);
  66. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  67. }
  68. });
  69. }
  70. function addShowReportCommand(program) {
  71. const command = program.command('show-report [report]');
  72. command.description('show HTML report');
  73. command.action((report, options) => (0, _html.showHTMLReport)(report, options.host, +options.port));
  74. command.option('--host <host>', 'Host to serve report on', 'localhost');
  75. command.option('--port <port>', 'Port to serve report on', '9323');
  76. command.addHelpText('afterAll', `
  77. Arguments [report]:
  78. When specified, opens given report, otherwise opens last generated report.
  79. Examples:
  80. $ npx playwright show-report
  81. $ npx playwright show-report playwright-report`);
  82. }
  83. function addMergeReportsCommand(program) {
  84. const command = program.command('merge-reports [dir]', {
  85. hidden: true
  86. });
  87. command.description('merge multiple blob reports (for sharded tests) into a single report');
  88. command.action(async (dir, options) => {
  89. try {
  90. await mergeReports(dir, options);
  91. } catch (e) {
  92. console.error(e);
  93. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  94. }
  95. });
  96. command.option('-c, --config <file>', `Configuration file. Can be used to specify additional configuration for the output report.`);
  97. command.option('--reporter <reporter>', `Reporter to use, comma-separated, can be ${_config.builtInReporters.map(name => `"${name}"`).join(', ')} (default: "${_config.defaultReporter}")`);
  98. command.addHelpText('afterAll', `
  99. Arguments [dir]:
  100. Directory containing blob reports.
  101. Examples:
  102. $ npx playwright merge-reports playwright-report`);
  103. }
  104. async function runTests(args, opts) {
  105. await (0, _utils.startProfiling)();
  106. // When no --config option is passed, let's look for the config file in the current directory.
  107. const configFileOrDirectory = opts.config ? _path.default.resolve(process.cwd(), opts.config) : process.cwd();
  108. const resolvedConfigFile = (0, _configLoader.resolveConfigFile)(configFileOrDirectory);
  109. if (restartWithExperimentalTsEsm(resolvedConfigFile)) return;
  110. const overrides = overridesFromOptions(opts);
  111. const configLoader = new _configLoader.ConfigLoader(overrides);
  112. let config;
  113. if (resolvedConfigFile) config = await configLoader.loadConfigFile(resolvedConfigFile, opts.deps === false);else config = await configLoader.loadEmptyConfig(configFileOrDirectory);
  114. config.cliArgs = args;
  115. config.cliGrep = opts.grep;
  116. config.cliGrepInvert = opts.grepInvert;
  117. config.cliListOnly = !!opts.list;
  118. config.cliProjectFilter = opts.project || undefined;
  119. config.cliPassWithNoTests = !!opts.passWithNoTests;
  120. const runner = new _runner.Runner(config);
  121. let status;
  122. if (opts.ui || opts.uiHost || opts.uiPort) status = await runner.uiAllTests({
  123. host: opts.uiHost,
  124. port: opts.uiPort ? +opts.uiPort : undefined
  125. });else if (process.env.PWTEST_WATCH) status = await runner.watchAllTests();else status = await runner.runAllTests();
  126. await (0, _utils.stopProfiling)('runner');
  127. const exitCode = status === 'interrupted' ? 130 : status === 'passed' ? 0 : 1;
  128. (0, _utils.gracefullyProcessExitDoNotHang)(exitCode);
  129. }
  130. async function listTestFiles(opts) {
  131. // Redefine process.stdout.write in case config decides to pollute stdio.
  132. const stdoutWrite = process.stdout.write.bind(process.stdout);
  133. process.stdout.write = () => {};
  134. process.stderr.write = () => {};
  135. const configFileOrDirectory = opts.config ? _path.default.resolve(process.cwd(), opts.config) : process.cwd();
  136. const resolvedConfigFile = (0, _configLoader.resolveConfigFile)(configFileOrDirectory);
  137. if (restartWithExperimentalTsEsm(resolvedConfigFile)) return;
  138. try {
  139. const configLoader = new _configLoader.ConfigLoader();
  140. const config = await configLoader.loadConfigFile(resolvedConfigFile);
  141. const runner = new _runner.Runner(config);
  142. const report = await runner.listTestFiles(opts.project);
  143. stdoutWrite(JSON.stringify(report), () => {
  144. (0, _utils.gracefullyProcessExitDoNotHang)(0);
  145. });
  146. } catch (e) {
  147. const error = (0, _util.serializeError)(e);
  148. error.location = (0, _base.prepareErrorStack)(e.stack).location;
  149. stdoutWrite(JSON.stringify({
  150. error
  151. }), () => {
  152. (0, _utils.gracefullyProcessExitDoNotHang)(0);
  153. });
  154. }
  155. }
  156. async function mergeReports(reportDir, opts) {
  157. let configFile = opts.config;
  158. if (configFile) {
  159. configFile = _path.default.resolve(process.cwd(), configFile);
  160. if (!_fs.default.existsSync(configFile)) throw new Error(`${configFile} does not exist`);
  161. if (!_fs.default.statSync(configFile).isFile()) throw new Error(`${configFile} is not a file`);
  162. }
  163. if (restartWithExperimentalTsEsm(configFile)) return;
  164. const configLoader = new _configLoader.ConfigLoader();
  165. const config = await (configFile ? configLoader.loadConfigFile(configFile) : configLoader.loadEmptyConfig(process.cwd()));
  166. const dir = _path.default.resolve(process.cwd(), reportDir || '');
  167. const dirStat = await _fs.default.promises.stat(dir).catch(e => null);
  168. if (!dirStat) throw new Error('Directory does not exist: ' + dir);
  169. if (!dirStat.isDirectory()) throw new Error(`"${dir}" is not a directory`);
  170. let reporterDescriptions = resolveReporterOption(opts.reporter);
  171. if (!reporterDescriptions && configFile) reporterDescriptions = config.config.reporter;
  172. if (!reporterDescriptions) reporterDescriptions = [[_config.defaultReporter]];
  173. const rootDirOverride = configFile ? config.config.rootDir : undefined;
  174. await (0, _merge.createMergedReport)(config, dir, reporterDescriptions, rootDirOverride);
  175. (0, _utils.gracefullyProcessExitDoNotHang)(0);
  176. }
  177. function overridesFromOptions(options) {
  178. const shardPair = options.shard ? options.shard.split('/').map(t => parseInt(t, 10)) : undefined;
  179. const overrides = {
  180. forbidOnly: options.forbidOnly ? true : undefined,
  181. fullyParallel: options.fullyParallel ? true : undefined,
  182. globalTimeout: options.globalTimeout ? parseInt(options.globalTimeout, 10) : undefined,
  183. maxFailures: options.x ? 1 : options.maxFailures ? parseInt(options.maxFailures, 10) : undefined,
  184. outputDir: options.output ? _path.default.resolve(process.cwd(), options.output) : undefined,
  185. quiet: options.quiet ? options.quiet : undefined,
  186. repeatEach: options.repeatEach ? parseInt(options.repeatEach, 10) : undefined,
  187. retries: options.retries ? parseInt(options.retries, 10) : undefined,
  188. reporter: resolveReporterOption(options.reporter),
  189. shard: shardPair ? {
  190. current: shardPair[0],
  191. total: shardPair[1]
  192. } : undefined,
  193. timeout: options.timeout ? parseInt(options.timeout, 10) : undefined,
  194. ignoreSnapshots: options.ignoreSnapshots ? !!options.ignoreSnapshots : undefined,
  195. updateSnapshots: options.updateSnapshots ? 'all' : undefined,
  196. workers: options.workers
  197. };
  198. if (options.browser) {
  199. const browserOpt = options.browser.toLowerCase();
  200. if (!['all', 'chromium', 'firefox', 'webkit'].includes(browserOpt)) throw new Error(`Unsupported browser "${options.browser}", must be one of "all", "chromium", "firefox" or "webkit"`);
  201. const browserNames = browserOpt === 'all' ? ['chromium', 'firefox', 'webkit'] : [browserOpt];
  202. overrides.projects = browserNames.map(browserName => {
  203. return {
  204. name: browserName,
  205. use: {
  206. browserName
  207. }
  208. };
  209. });
  210. }
  211. if (options.headed || options.debug) overrides.use = {
  212. headless: false
  213. };
  214. if (!options.ui && options.debug) {
  215. overrides.maxFailures = 1;
  216. overrides.timeout = 0;
  217. overrides.workers = 1;
  218. process.env.PWDEBUG = '1';
  219. }
  220. if (!options.ui && options.trace) {
  221. if (!kTraceModes.includes(options.trace)) throw new Error(`Unsupported trace mode "${options.trace}", must be one of ${kTraceModes.map(mode => `"${mode}"`).join(', ')}`);
  222. overrides.use = overrides.use || {};
  223. overrides.use.trace = options.trace;
  224. }
  225. return overrides;
  226. }
  227. function resolveReporterOption(reporter) {
  228. if (!reporter || !reporter.length) return undefined;
  229. return reporter.split(',').map(r => [resolveReporter(r)]);
  230. }
  231. function resolveReporter(id) {
  232. if (_config.builtInReporters.includes(id)) return id;
  233. const localPath = _path.default.resolve(process.cwd(), id);
  234. if (_fs.default.existsSync(localPath)) return localPath;
  235. return require.resolve(id, {
  236. paths: [process.cwd()]
  237. });
  238. }
  239. function restartWithExperimentalTsEsm(configFile) {
  240. const nodeVersion = +process.versions.node.split('.')[0];
  241. // New experimental loader is only supported on Node 16+.
  242. if (nodeVersion < 16) return false;
  243. if (!configFile) return false;
  244. if (process.env.PW_DISABLE_TS_ESM) return false;
  245. // Node.js < 20
  246. if (globalThis.__esmLoaderPortPreV20) {
  247. // clear execArgv after restart, so that childProcess.fork in user code does not inherit our loader.
  248. process.execArgv = (0, _esmUtils.execArgvWithoutExperimentalLoaderOptions)();
  249. return false;
  250. }
  251. if (!(0, _util.fileIsModule)(configFile)) return false;
  252. // Node.js < 20
  253. if (!require('node:module').register) {
  254. const innerProcess = require('child_process').fork(require.resolve('./cli'), process.argv.slice(2), {
  255. env: {
  256. ...process.env,
  257. PW_TS_ESM_LEGACY_LOADER_ON: '1'
  258. },
  259. execArgv: (0, _esmUtils.execArgvWithExperimentalLoaderOptions)()
  260. });
  261. innerProcess.on('close', code => {
  262. if (code !== 0 && code !== null) (0, _utils.gracefullyProcessExitDoNotHang)(code);
  263. });
  264. return true;
  265. }
  266. // Nodejs >= 21
  267. (0, _esmLoaderHost.registerESMLoader)();
  268. return false;
  269. }
  270. const kTraceModes = ['on', 'off', 'on-first-retry', 'on-all-retries', 'retain-on-failure'];
  271. const testOptions = [['--browser <browser>', `Browser to use for tests, one of "all", "chromium", "firefox" or "webkit" (default: "chromium")`], ['-c, --config <file>', `Configuration file, or a test directory with optional "playwright.config.{m,c}?{js,ts}"`], ['--debug', `Run tests with Playwright Inspector. Shortcut for "PWDEBUG=1" environment variable and "--timeout=0 --max-failures=1 --headed --workers=1" options`], ['--forbid-only', `Fail if test.only is called (default: false)`], ['--fully-parallel', `Run all tests in parallel (default: false)`], ['--global-timeout <timeout>', `Maximum time this test suite can run in milliseconds (default: unlimited)`], ['-g, --grep <grep>', `Only run tests matching this regular expression (default: ".*")`], ['-gv, --grep-invert <grep>', `Only run tests that do not match this regular expression`], ['--headed', `Run tests in headed browsers (default: headless)`], ['--ignore-snapshots', `Ignore screenshot and snapshot expectations`], ['--list', `Collect all the tests and report them, but do not run`], ['--max-failures <N>', `Stop after the first N failures`], ['--no-deps', 'Do not run project dependencies'], ['--output <dir>', `Folder for output artifacts (default: "test-results")`], ['--pass-with-no-tests', `Makes test run succeed even if no tests were found`], ['--project <project-name...>', `Only run tests from the specified list of projects (default: run all projects)`], ['--quiet', `Suppress stdio`], ['--repeat-each <N>', `Run each test N times (default: 1)`], ['--reporter <reporter>', `Reporter to use, comma-separated, can be ${_config.builtInReporters.map(name => `"${name}"`).join(', ')} (default: "${_config.defaultReporter}")`], ['--retries <retries>', `Maximum retry count for flaky tests, zero for no retries (default: no retries)`], ['--shard <shard>', `Shard tests and execute only the selected shard, specify in the form "current/all", 1-based, for example "3/5"`], ['--timeout <timeout>', `Specify test timeout threshold in milliseconds, zero for unlimited (default: ${_config.defaultTimeout})`], ['--trace <mode>', `Force tracing mode, can be ${kTraceModes.map(mode => `"${mode}"`).join(', ')}`], ['--ui', `Run tests in interactive UI mode`], ['--ui-host <host>', 'Host to serve UI on; specifying this option opens UI in a browser tab'], ['--ui-port <port>', 'Port to serve UI on, 0 for any free port; specifying this option opens UI in a browser tab'], ['-u, --update-snapshots', `Update snapshots with actual results (default: only create missing snapshots)`], ['-j, --workers <workers>', `Number of concurrent workers or percentage of logical CPU cores, use 1 to run in a single worker (default: 50%)`], ['-x', `Stop after the first failure`]];
  272. addTestCommand(_program.default);
  273. addShowReportCommand(_program.default);
  274. addListFilesCommand(_program.default);
  275. addMergeReportsCommand(_program.default);
  276. _program.default.parse(process.argv);