program.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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_utilsBundle2.program
  32. });
  33. module.exports = __toCommonJS(program_exports);
  34. var import_fs = __toESM(require("fs"));
  35. var import_os = __toESM(require("os"));
  36. var import_path = __toESM(require("path"));
  37. var playwright = __toESM(require("../.."));
  38. var import_driver = require("./driver");
  39. var import_server = require("../server");
  40. var import_utils = require("../utils");
  41. var import_traceViewer = require("../server/trace/viewer/traceViewer");
  42. var import_utils2 = require("../utils");
  43. var import_ascii = require("../server/utils/ascii");
  44. var import_utilsBundle = require("../utilsBundle");
  45. var import_utilsBundle2 = require("../utilsBundle");
  46. const packageJSON = require("../../package.json");
  47. import_utilsBundle.program.version("Version " + (process.env.PW_CLI_DISPLAY_VERSION || packageJSON.version)).name(buildBasePlaywrightCLICommand(process.env.PW_LANG_NAME));
  48. import_utilsBundle.program.command("mark-docker-image [dockerImageNameTemplate]", { hidden: true }).description("mark docker image").allowUnknownOption(true).action(function(dockerImageNameTemplate) {
  49. (0, import_utils2.assert)(dockerImageNameTemplate, "dockerImageNameTemplate is required");
  50. (0, import_server.writeDockerVersion)(dockerImageNameTemplate).catch(logErrorAndExit);
  51. });
  52. commandWithOpenOptions("open [url]", "open page in browser specified via -b, --browser", []).action(function(url, options) {
  53. open(options, url).catch(logErrorAndExit);
  54. }).addHelpText("afterAll", `
  55. Examples:
  56. $ open
  57. $ open -b webkit https://example.com`);
  58. commandWithOpenOptions(
  59. "codegen [url]",
  60. "open page and generate code for user actions",
  61. [
  62. ["-o, --output <file name>", "saves the generated script to a file"],
  63. ["--target <language>", `language to generate, one of javascript, playwright-test, python, python-async, python-pytest, csharp, csharp-mstest, csharp-nunit, java, java-junit`, codegenId()],
  64. ["--test-id-attribute <attributeName>", "use the specified attribute to generate data test ID selectors"]
  65. ]
  66. ).action(async function(url, options) {
  67. await codegen(options, url);
  68. }).addHelpText("afterAll", `
  69. Examples:
  70. $ codegen
  71. $ codegen --target=python
  72. $ codegen -b webkit https://example.com`);
  73. function suggestedBrowsersToInstall() {
  74. return import_server.registry.executables().filter((e) => e.installType !== "none" && e.type !== "tool").map((e) => e.name).join(", ");
  75. }
  76. function defaultBrowsersToInstall(options) {
  77. let executables = import_server.registry.defaultExecutables();
  78. if (options.noShell)
  79. executables = executables.filter((e) => e.name !== "chromium-headless-shell");
  80. if (options.onlyShell)
  81. executables = executables.filter((e) => e.name !== "chromium");
  82. return executables;
  83. }
  84. function checkBrowsersToInstall(args, options) {
  85. if (options.noShell && options.onlyShell)
  86. throw new Error(`Only one of --no-shell and --only-shell can be specified`);
  87. const faultyArguments = [];
  88. const executables = [];
  89. const handleArgument = (arg) => {
  90. const executable = import_server.registry.findExecutable(arg);
  91. if (!executable || executable.installType === "none")
  92. faultyArguments.push(arg);
  93. else
  94. executables.push(executable);
  95. if (executable?.browserName === "chromium")
  96. executables.push(import_server.registry.findExecutable("ffmpeg"));
  97. };
  98. for (const arg of args) {
  99. if (arg === "chromium") {
  100. if (!options.onlyShell)
  101. handleArgument("chromium");
  102. if (!options.noShell)
  103. handleArgument("chromium-headless-shell");
  104. } else {
  105. handleArgument(arg);
  106. }
  107. }
  108. if (process.platform === "win32")
  109. executables.push(import_server.registry.findExecutable("winldd"));
  110. if (faultyArguments.length)
  111. throw new Error(`Invalid installation targets: ${faultyArguments.map((name) => `'${name}'`).join(", ")}. Expecting one of: ${suggestedBrowsersToInstall()}`);
  112. return executables;
  113. }
  114. function printInstalledBrowsers(browsers2) {
  115. const browserPaths = /* @__PURE__ */ new Set();
  116. for (const browser of browsers2)
  117. browserPaths.add(browser.browserPath);
  118. console.log(` Browsers:`);
  119. for (const browserPath of [...browserPaths].sort())
  120. console.log(` ${browserPath}`);
  121. console.log(` References:`);
  122. const references = /* @__PURE__ */ new Set();
  123. for (const browser of browsers2)
  124. references.add(browser.referenceDir);
  125. for (const reference of [...references].sort())
  126. console.log(` ${reference}`);
  127. }
  128. function printGroupedByPlaywrightVersion(browsers2) {
  129. const dirToVersion = /* @__PURE__ */ new Map();
  130. for (const browser of browsers2) {
  131. if (dirToVersion.has(browser.referenceDir))
  132. continue;
  133. const packageJSON2 = require(import_path.default.join(browser.referenceDir, "package.json"));
  134. const version = packageJSON2.version;
  135. dirToVersion.set(browser.referenceDir, version);
  136. }
  137. const groupedByPlaywrightMinorVersion = /* @__PURE__ */ new Map();
  138. for (const browser of browsers2) {
  139. const version = dirToVersion.get(browser.referenceDir);
  140. let entries = groupedByPlaywrightMinorVersion.get(version);
  141. if (!entries) {
  142. entries = [];
  143. groupedByPlaywrightMinorVersion.set(version, entries);
  144. }
  145. entries.push(browser);
  146. }
  147. const sortedVersions = [...groupedByPlaywrightMinorVersion.keys()].sort((a, b) => {
  148. const aComponents = a.split(".");
  149. const bComponents = b.split(".");
  150. const aMajor = parseInt(aComponents[0], 10);
  151. const bMajor = parseInt(bComponents[0], 10);
  152. if (aMajor !== bMajor)
  153. return aMajor - bMajor;
  154. const aMinor = parseInt(aComponents[1], 10);
  155. const bMinor = parseInt(bComponents[1], 10);
  156. if (aMinor !== bMinor)
  157. return aMinor - bMinor;
  158. return aComponents.slice(2).join(".").localeCompare(bComponents.slice(2).join("."));
  159. });
  160. for (const version of sortedVersions) {
  161. console.log(`
  162. Playwright version: ${version}`);
  163. printInstalledBrowsers(groupedByPlaywrightMinorVersion.get(version));
  164. }
  165. }
  166. import_utilsBundle.program.command("install [browser...]").description("ensure browsers necessary for this version of Playwright are installed").option("--with-deps", "install system dependencies for browsers").option("--dry-run", "do not execute installation, only print information").option("--list", "prints list of browsers from all playwright installations").option("--force", "force reinstall of stable browser channels").option("--only-shell", "only install headless shell when installing chromium").option("--no-shell", "do not install chromium headless shell").action(async function(args, options) {
  167. if (options.shell === false)
  168. options.noShell = true;
  169. if ((0, import_utils.isLikelyNpxGlobal)()) {
  170. console.error((0, import_ascii.wrapInASCIIBox)([
  171. `WARNING: It looks like you are running 'npx playwright install' without first`,
  172. `installing your project's dependencies.`,
  173. ``,
  174. `To avoid unexpected behavior, please install your dependencies first, and`,
  175. `then run Playwright's install command:`,
  176. ``,
  177. ` npm install`,
  178. ` npx playwright install`,
  179. ``,
  180. `If your project does not yet depend on Playwright, first install the`,
  181. `applicable npm package (most commonly @playwright/test), and`,
  182. `then run Playwright's install command to download the browsers:`,
  183. ``,
  184. ` npm install @playwright/test`,
  185. ` npx playwright install`,
  186. ``
  187. ].join("\n"), 1));
  188. }
  189. try {
  190. const hasNoArguments = !args.length;
  191. const executables = hasNoArguments ? defaultBrowsersToInstall(options) : checkBrowsersToInstall(args, options);
  192. if (options.withDeps)
  193. await import_server.registry.installDeps(executables, !!options.dryRun);
  194. if (options.dryRun && options.list)
  195. throw new Error(`Only one of --dry-run and --list can be specified`);
  196. if (options.dryRun) {
  197. for (const executable of executables) {
  198. const version = executable.browserVersion ? `version ` + executable.browserVersion : "";
  199. console.log(`browser: ${executable.name}${version ? " " + version : ""}`);
  200. console.log(` Install location: ${executable.directory ?? "<system>"}`);
  201. if (executable.downloadURLs?.length) {
  202. const [url, ...fallbacks] = executable.downloadURLs;
  203. console.log(` Download url: ${url}`);
  204. for (let i = 0; i < fallbacks.length; ++i)
  205. console.log(` Download fallback ${i + 1}: ${fallbacks[i]}`);
  206. }
  207. console.log(``);
  208. }
  209. } else if (options.list) {
  210. const browsers2 = await import_server.registry.listInstalledBrowsers();
  211. printGroupedByPlaywrightVersion(browsers2);
  212. } else {
  213. const forceReinstall = hasNoArguments ? false : !!options.force;
  214. await import_server.registry.install(executables, forceReinstall);
  215. await import_server.registry.validateHostRequirementsForExecutablesIfNeeded(executables, process.env.PW_LANG_NAME || "javascript").catch((e) => {
  216. e.name = "Playwright Host validation warning";
  217. console.error(e);
  218. });
  219. }
  220. } catch (e) {
  221. console.log(`Failed to install browsers
  222. ${e}`);
  223. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  224. }
  225. }).addHelpText("afterAll", `
  226. Examples:
  227. - $ install
  228. Install default browsers.
  229. - $ install chrome firefox
  230. Install custom browsers, supports ${suggestedBrowsersToInstall()}.`);
  231. import_utilsBundle.program.command("uninstall").description("Removes browsers used by this installation of Playwright from the system (chromium, firefox, webkit, ffmpeg). This does not include branded channels.").option("--all", "Removes all browsers used by any Playwright installation from the system.").action(async (options) => {
  232. delete process.env.PLAYWRIGHT_SKIP_BROWSER_GC;
  233. await import_server.registry.uninstall(!!options.all).then(({ numberOfBrowsersLeft }) => {
  234. if (!options.all && numberOfBrowsersLeft > 0) {
  235. console.log("Successfully uninstalled Playwright browsers for the current Playwright installation.");
  236. console.log(`There are still ${numberOfBrowsersLeft} browsers left, used by other Playwright installations.
  237. To uninstall Playwright browsers for all installations, re-run with --all flag.`);
  238. }
  239. }).catch(logErrorAndExit);
  240. });
  241. import_utilsBundle.program.command("install-deps [browser...]").description("install dependencies necessary to run browsers (will ask for sudo permissions)").option("--dry-run", "Do not execute installation commands, only print them").action(async function(args, options) {
  242. try {
  243. if (!args.length)
  244. await import_server.registry.installDeps(defaultBrowsersToInstall({}), !!options.dryRun);
  245. else
  246. await import_server.registry.installDeps(checkBrowsersToInstall(args, {}), !!options.dryRun);
  247. } catch (e) {
  248. console.log(`Failed to install browser dependencies
  249. ${e}`);
  250. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  251. }
  252. }).addHelpText("afterAll", `
  253. Examples:
  254. - $ install-deps
  255. Install dependencies for default browsers.
  256. - $ install-deps chrome firefox
  257. Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`);
  258. const browsers = [
  259. { alias: "cr", name: "Chromium", type: "chromium" },
  260. { alias: "ff", name: "Firefox", type: "firefox" },
  261. { alias: "wk", name: "WebKit", type: "webkit" }
  262. ];
  263. for (const { alias, name, type } of browsers) {
  264. commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, []).action(function(url, options) {
  265. open({ ...options, browser: type }, url).catch(logErrorAndExit);
  266. }).addHelpText("afterAll", `
  267. Examples:
  268. $ ${alias} https://example.com`);
  269. }
  270. commandWithOpenOptions(
  271. "screenshot <url> <filename>",
  272. "capture a page screenshot",
  273. [
  274. ["--wait-for-selector <selector>", "wait for selector before taking a screenshot"],
  275. ["--wait-for-timeout <timeout>", "wait for timeout in milliseconds before taking a screenshot"],
  276. ["--full-page", "whether to take a full page screenshot (entire scrollable area)"]
  277. ]
  278. ).action(function(url, filename, command) {
  279. screenshot(command, command, url, filename).catch(logErrorAndExit);
  280. }).addHelpText("afterAll", `
  281. Examples:
  282. $ screenshot -b webkit https://example.com example.png`);
  283. commandWithOpenOptions(
  284. "pdf <url> <filename>",
  285. "save page as pdf",
  286. [
  287. ["--paper-format <format>", "paper format: Letter, Legal, Tabloid, Ledger, A0, A1, A2, A3, A4, A5, A6"],
  288. ["--wait-for-selector <selector>", "wait for given selector before saving as pdf"],
  289. ["--wait-for-timeout <timeout>", "wait for given timeout in milliseconds before saving as pdf"]
  290. ]
  291. ).action(function(url, filename, options) {
  292. pdf(options, options, url, filename).catch(logErrorAndExit);
  293. }).addHelpText("afterAll", `
  294. Examples:
  295. $ pdf https://example.com example.pdf`);
  296. import_utilsBundle.program.command("run-driver", { hidden: true }).action(function(options) {
  297. (0, import_driver.runDriver)();
  298. });
  299. import_utilsBundle.program.command("run-server").option("--port <port>", "Server port").option("--host <host>", "Server host").option("--path <path>", "Endpoint Path", "/").option("--max-clients <maxClients>", "Maximum clients").option("--mode <mode>", 'Server mode, either "default" or "extension"').action(function(options) {
  300. (0, import_driver.runServer)({
  301. port: options.port ? +options.port : void 0,
  302. host: options.host,
  303. path: options.path,
  304. maxConnections: options.maxClients ? +options.maxClients : Infinity,
  305. extension: options.mode === "extension" || !!process.env.PW_EXTENSION_MODE
  306. }).catch(logErrorAndExit);
  307. });
  308. import_utilsBundle.program.command("print-api-json", { hidden: true }).action(function(options) {
  309. (0, import_driver.printApiJson)();
  310. });
  311. import_utilsBundle.program.command("launch-server", { hidden: true }).requiredOption("--browser <browserName>", 'Browser name, one of "chromium", "firefox" or "webkit"').option("--config <path-to-config-file>", "JSON file with launchServer options").action(function(options) {
  312. (0, import_driver.launchBrowserServer)(options.browser, options.config);
  313. });
  314. import_utilsBundle.program.command("show-trace [trace...]").option("-b, --browser <browserType>", "browser to use, one of cr, chromium, ff, firefox, wk, webkit", "chromium").option("-h, --host <host>", "Host to serve trace on; specifying this option opens trace in a browser tab").option("-p, --port <port>", "Port to serve trace on, 0 for any free port; specifying this option opens trace in a browser tab").option("--stdin", "Accept trace URLs over stdin to update the viewer").description("show trace viewer").action(function(traces, options) {
  315. if (options.browser === "cr")
  316. options.browser = "chromium";
  317. if (options.browser === "ff")
  318. options.browser = "firefox";
  319. if (options.browser === "wk")
  320. options.browser = "webkit";
  321. const openOptions = {
  322. host: options.host,
  323. port: +options.port,
  324. isServer: !!options.stdin
  325. };
  326. if (options.port !== void 0 || options.host !== void 0)
  327. (0, import_traceViewer.runTraceInBrowser)(traces, openOptions).catch(logErrorAndExit);
  328. else
  329. (0, import_traceViewer.runTraceViewerApp)(traces, options.browser, openOptions, true).catch(logErrorAndExit);
  330. }).addHelpText("afterAll", `
  331. Examples:
  332. $ show-trace https://example.com/trace.zip`);
  333. async function launchContext(options, extraOptions) {
  334. validateOptions(options);
  335. const browserType = lookupBrowserType(options);
  336. const launchOptions = extraOptions;
  337. if (options.channel)
  338. launchOptions.channel = options.channel;
  339. launchOptions.handleSIGINT = false;
  340. const contextOptions = (
  341. // Copy the device descriptor since we have to compare and modify the options.
  342. options.device ? { ...playwright.devices[options.device] } : {}
  343. );
  344. if (!extraOptions.headless)
  345. contextOptions.deviceScaleFactor = import_os.default.platform() === "darwin" ? 2 : 1;
  346. if (browserType.name() === "webkit" && process.platform === "linux") {
  347. delete contextOptions.hasTouch;
  348. delete contextOptions.isMobile;
  349. }
  350. if (contextOptions.isMobile && browserType.name() === "firefox")
  351. contextOptions.isMobile = void 0;
  352. if (options.blockServiceWorkers)
  353. contextOptions.serviceWorkers = "block";
  354. if (options.proxyServer) {
  355. launchOptions.proxy = {
  356. server: options.proxyServer
  357. };
  358. if (options.proxyBypass)
  359. launchOptions.proxy.bypass = options.proxyBypass;
  360. }
  361. if (options.viewportSize) {
  362. try {
  363. const [width, height] = options.viewportSize.split(",").map((n) => +n);
  364. if (isNaN(width) || isNaN(height))
  365. throw new Error("bad values");
  366. contextOptions.viewport = { width, height };
  367. } catch (e) {
  368. throw new Error('Invalid viewport size format: use "width,height", for example --viewport-size="800,600"');
  369. }
  370. }
  371. if (options.geolocation) {
  372. try {
  373. const [latitude, longitude] = options.geolocation.split(",").map((n) => parseFloat(n.trim()));
  374. contextOptions.geolocation = {
  375. latitude,
  376. longitude
  377. };
  378. } catch (e) {
  379. throw new Error('Invalid geolocation format, should be "lat,long". For example --geolocation="37.819722,-122.478611"');
  380. }
  381. contextOptions.permissions = ["geolocation"];
  382. }
  383. if (options.userAgent)
  384. contextOptions.userAgent = options.userAgent;
  385. if (options.lang)
  386. contextOptions.locale = options.lang;
  387. if (options.colorScheme)
  388. contextOptions.colorScheme = options.colorScheme;
  389. if (options.timezone)
  390. contextOptions.timezoneId = options.timezone;
  391. if (options.loadStorage)
  392. contextOptions.storageState = options.loadStorage;
  393. if (options.ignoreHttpsErrors)
  394. contextOptions.ignoreHTTPSErrors = true;
  395. if (options.saveHar) {
  396. contextOptions.recordHar = { path: import_path.default.resolve(process.cwd(), options.saveHar), mode: "minimal" };
  397. if (options.saveHarGlob)
  398. contextOptions.recordHar.urlFilter = options.saveHarGlob;
  399. contextOptions.serviceWorkers = "block";
  400. }
  401. let browser;
  402. let context;
  403. if (options.userDataDir) {
  404. context = await browserType.launchPersistentContext(options.userDataDir, { ...launchOptions, ...contextOptions });
  405. browser = context.browser();
  406. } else {
  407. browser = await browserType.launch(launchOptions);
  408. context = await browser.newContext(contextOptions);
  409. }
  410. let closingBrowser = false;
  411. async function closeBrowser() {
  412. if (closingBrowser)
  413. return;
  414. closingBrowser = true;
  415. if (options.saveStorage)
  416. await context.storageState({ path: options.saveStorage }).catch((e) => null);
  417. if (options.saveHar)
  418. await context.close();
  419. await browser.close();
  420. }
  421. context.on("page", (page) => {
  422. page.on("dialog", () => {
  423. });
  424. page.on("close", () => {
  425. const hasPage = browser.contexts().some((context2) => context2.pages().length > 0);
  426. if (hasPage)
  427. return;
  428. closeBrowser().catch(() => {
  429. });
  430. });
  431. });
  432. process.on("SIGINT", async () => {
  433. await closeBrowser();
  434. (0, import_utils.gracefullyProcessExitDoNotHang)(130);
  435. });
  436. const timeout = options.timeout ? parseInt(options.timeout, 10) : 0;
  437. context.setDefaultTimeout(timeout);
  438. context.setDefaultNavigationTimeout(timeout);
  439. delete launchOptions.headless;
  440. delete launchOptions.executablePath;
  441. delete launchOptions.handleSIGINT;
  442. delete contextOptions.deviceScaleFactor;
  443. return { browser, browserName: browserType.name(), context, contextOptions, launchOptions, closeBrowser };
  444. }
  445. async function openPage(context, url) {
  446. let page = context.pages()[0];
  447. if (!page)
  448. page = await context.newPage();
  449. if (url) {
  450. if (import_fs.default.existsSync(url))
  451. url = "file://" + import_path.default.resolve(url);
  452. else if (!url.startsWith("http") && !url.startsWith("file://") && !url.startsWith("about:") && !url.startsWith("data:"))
  453. url = "http://" + url;
  454. await page.goto(url);
  455. }
  456. return page;
  457. }
  458. async function open(options, url) {
  459. const { context } = await launchContext(options, { headless: !!process.env.PWTEST_CLI_HEADLESS, executablePath: process.env.PWTEST_CLI_EXECUTABLE_PATH });
  460. await openPage(context, url);
  461. }
  462. async function codegen(options, url) {
  463. const { target: language, output: outputFile, testIdAttribute: testIdAttributeName } = options;
  464. const tracesDir = import_path.default.join(import_os.default.tmpdir(), `playwright-recorder-trace-${Date.now()}`);
  465. const { context, browser, launchOptions, contextOptions, closeBrowser } = await launchContext(options, {
  466. headless: !!process.env.PWTEST_CLI_HEADLESS,
  467. executablePath: process.env.PWTEST_CLI_EXECUTABLE_PATH,
  468. tracesDir
  469. });
  470. const donePromise = new import_utils.ManualPromise();
  471. maybeSetupTestHooks(browser, closeBrowser, donePromise);
  472. import_utilsBundle.dotenv.config({ path: "playwright.env" });
  473. await context._enableRecorder({
  474. language,
  475. launchOptions,
  476. contextOptions,
  477. device: options.device,
  478. saveStorage: options.saveStorage,
  479. mode: "recording",
  480. testIdAttributeName,
  481. outputFile: outputFile ? import_path.default.resolve(outputFile) : void 0,
  482. handleSIGINT: false
  483. });
  484. await openPage(context, url);
  485. donePromise.resolve();
  486. }
  487. async function maybeSetupTestHooks(browser, closeBrowser, donePromise) {
  488. if (!process.env.PWTEST_CLI_IS_UNDER_TEST)
  489. return;
  490. const logs = [];
  491. require("playwright-core/lib/utilsBundle").debug.log = (...args) => {
  492. const line = require("util").format(...args) + "\n";
  493. logs.push(line);
  494. process.stderr.write(line);
  495. };
  496. browser.on("disconnected", () => {
  497. const hasCrashLine = logs.some((line) => line.includes("process did exit:") && !line.includes("process did exit: exitCode=0, signal=null"));
  498. if (hasCrashLine) {
  499. process.stderr.write("Detected browser crash.\n");
  500. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  501. }
  502. });
  503. const close = async () => {
  504. await donePromise;
  505. await closeBrowser();
  506. };
  507. if (process.env.PWTEST_CLI_EXIT_AFTER_TIMEOUT) {
  508. setTimeout(close, +process.env.PWTEST_CLI_EXIT_AFTER_TIMEOUT);
  509. return;
  510. }
  511. let stdin = "";
  512. process.stdin.on("data", (data) => {
  513. stdin += data.toString();
  514. if (stdin.startsWith("exit")) {
  515. process.stdin.destroy();
  516. close();
  517. }
  518. });
  519. }
  520. async function waitForPage(page, captureOptions) {
  521. if (captureOptions.waitForSelector) {
  522. console.log(`Waiting for selector ${captureOptions.waitForSelector}...`);
  523. await page.waitForSelector(captureOptions.waitForSelector);
  524. }
  525. if (captureOptions.waitForTimeout) {
  526. console.log(`Waiting for timeout ${captureOptions.waitForTimeout}...`);
  527. await page.waitForTimeout(parseInt(captureOptions.waitForTimeout, 10));
  528. }
  529. }
  530. async function screenshot(options, captureOptions, url, path2) {
  531. const { context } = await launchContext(options, { headless: true });
  532. console.log("Navigating to " + url);
  533. const page = await openPage(context, url);
  534. await waitForPage(page, captureOptions);
  535. console.log("Capturing screenshot into " + path2);
  536. await page.screenshot({ path: path2, fullPage: !!captureOptions.fullPage });
  537. await page.close();
  538. }
  539. async function pdf(options, captureOptions, url, path2) {
  540. if (options.browser !== "chromium")
  541. throw new Error("PDF creation is only working with Chromium");
  542. const { context } = await launchContext({ ...options, browser: "chromium" }, { headless: true });
  543. console.log("Navigating to " + url);
  544. const page = await openPage(context, url);
  545. await waitForPage(page, captureOptions);
  546. console.log("Saving as pdf into " + path2);
  547. await page.pdf({ path: path2, format: captureOptions.paperFormat });
  548. await page.close();
  549. }
  550. function lookupBrowserType(options) {
  551. let name = options.browser;
  552. if (options.device) {
  553. const device = playwright.devices[options.device];
  554. name = device.defaultBrowserType;
  555. }
  556. let browserType;
  557. switch (name) {
  558. case "chromium":
  559. browserType = playwright.chromium;
  560. break;
  561. case "webkit":
  562. browserType = playwright.webkit;
  563. break;
  564. case "firefox":
  565. browserType = playwright.firefox;
  566. break;
  567. case "cr":
  568. browserType = playwright.chromium;
  569. break;
  570. case "wk":
  571. browserType = playwright.webkit;
  572. break;
  573. case "ff":
  574. browserType = playwright.firefox;
  575. break;
  576. }
  577. if (browserType)
  578. return browserType;
  579. import_utilsBundle.program.help();
  580. }
  581. function validateOptions(options) {
  582. if (options.device && !(options.device in playwright.devices)) {
  583. const lines = [`Device descriptor not found: '${options.device}', available devices are:`];
  584. for (const name in playwright.devices)
  585. lines.push(` "${name}"`);
  586. throw new Error(lines.join("\n"));
  587. }
  588. if (options.colorScheme && !["light", "dark"].includes(options.colorScheme))
  589. throw new Error('Invalid color scheme, should be one of "light", "dark"');
  590. }
  591. function logErrorAndExit(e) {
  592. if (process.env.PWDEBUGIMPL)
  593. console.error(e);
  594. else
  595. console.error(e.name + ": " + e.message);
  596. (0, import_utils.gracefullyProcessExitDoNotHang)(1);
  597. }
  598. function codegenId() {
  599. return process.env.PW_LANG_NAME || "playwright-test";
  600. }
  601. function commandWithOpenOptions(command, description, options) {
  602. let result = import_utilsBundle.program.command(command).description(description);
  603. for (const option of options)
  604. result = result.option(option[0], ...option.slice(1));
  605. return result.option("-b, --browser <browserType>", "browser to use, one of cr, chromium, ff, firefox, wk, webkit", "chromium").option("--block-service-workers", "block service workers").option("--channel <channel>", 'Chromium distribution channel, "chrome", "chrome-beta", "msedge-dev", etc').option("--color-scheme <scheme>", 'emulate preferred color scheme, "light" or "dark"').option("--device <deviceName>", 'emulate device, for example "iPhone 11"').option("--geolocation <coordinates>", 'specify geolocation coordinates, for example "37.819722,-122.478611"').option("--ignore-https-errors", "ignore https errors").option("--load-storage <filename>", "load context storage state from the file, previously saved with --save-storage").option("--lang <language>", 'specify language / locale, for example "en-GB"').option("--proxy-server <proxy>", 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"').option("--proxy-bypass <bypass>", 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"').option("--save-har <filename>", "save HAR file with all network activity at the end").option("--save-har-glob <glob pattern>", "filter entries in the HAR by matching url against this glob pattern").option("--save-storage <filename>", "save context storage state at the end, for later use with --load-storage").option("--timezone <time zone>", 'time zone to emulate, for example "Europe/Rome"').option("--timeout <timeout>", "timeout for Playwright actions in milliseconds, no timeout by default").option("--user-agent <ua string>", "specify user agent string").option("--user-data-dir <directory>", "use the specified user data directory instead of a new context").option("--viewport-size <size>", 'specify browser viewport size in pixels, for example "1280, 720"');
  606. }
  607. function buildBasePlaywrightCLICommand(cliTargetLang) {
  608. switch (cliTargetLang) {
  609. case "python":
  610. return `playwright`;
  611. case "java":
  612. return `mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="...options.."`;
  613. case "csharp":
  614. return `pwsh bin/Debug/netX/playwright.ps1`;
  615. default: {
  616. const packageManagerCommand = (0, import_utils2.getPackageManagerExecCommand)();
  617. return `${packageManagerCommand} playwright`;
  618. }
  619. }
  620. }
  621. // Annotate the CommonJS export names for ESM import in node:
  622. 0 && (module.exports = {
  623. program
  624. });