program.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _fs = _interopRequireDefault(require("fs"));
  7. var _os = _interopRequireDefault(require("os"));
  8. var _path = _interopRequireDefault(require("path"));
  9. var _utilsBundle = require("../utilsBundle");
  10. var _driver = require("./driver");
  11. var _traceViewer = require("../server/trace/viewer/traceViewer");
  12. var playwright = _interopRequireWildcard(require("../.."));
  13. var _child_process = require("child_process");
  14. var _utils = require("../utils");
  15. var _server = require("../server");
  16. var _errors = require("../client/errors");
  17. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  18. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  19. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  20. /**
  21. * Copyright (c) Microsoft Corporation.
  22. *
  23. * Licensed under the Apache License, Version 2.0 (the "License");
  24. * you may not use this file except in compliance with the License.
  25. * You may obtain a copy of the License at
  26. *
  27. * http://www.apache.org/licenses/LICENSE-2.0
  28. *
  29. * Unless required by applicable law or agreed to in writing, software
  30. * distributed under the License is distributed on an "AS IS" BASIS,
  31. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  32. * See the License for the specific language governing permissions and
  33. * limitations under the License.
  34. */
  35. /* eslint-disable no-console */
  36. const packageJSON = require('../../package.json');
  37. _utilsBundle.program.version('Version ' + (process.env.PW_CLI_DISPLAY_VERSION || packageJSON.version)).name(buildBasePlaywrightCLICommand(process.env.PW_LANG_NAME));
  38. _utilsBundle.program.command('mark-docker-image [dockerImageNameTemplate]', {
  39. hidden: true
  40. }).description('mark docker image').allowUnknownOption(true).action(function (dockerImageNameTemplate) {
  41. (0, _utils.assert)(dockerImageNameTemplate, 'dockerImageNameTemplate is required');
  42. (0, _server.writeDockerVersion)(dockerImageNameTemplate).catch(logErrorAndExit);
  43. });
  44. commandWithOpenOptions('open [url]', 'open page in browser specified via -b, --browser', []).action(function (url, options) {
  45. open(options, url, codegenId()).catch(logErrorAndExit);
  46. }).addHelpText('afterAll', `
  47. Examples:
  48. $ open
  49. $ open -b webkit https://example.com`);
  50. commandWithOpenOptions('codegen [url]', 'open page and generate code for user actions', [['-o, --output <file name>', 'saves the generated script to a file'], ['--target <language>', `language to generate, one of javascript, playwright-test, python, python-async, python-pytest, csharp, csharp-mstest, csharp-nunit, java`, codegenId()], ['--save-trace <filename>', 'record a trace for the session and save it to a file'], ['--test-id-attribute <attributeName>', 'use the specified attribute to generate data test ID selectors']]).action(function (url, options) {
  51. codegen(options, url).catch(logErrorAndExit);
  52. }).addHelpText('afterAll', `
  53. Examples:
  54. $ codegen
  55. $ codegen --target=python
  56. $ codegen -b webkit https://example.com`);
  57. _utilsBundle.program.command('debug <app> [args...]', {
  58. hidden: true
  59. }).description('run command in debug mode: disable timeout, open inspector').allowUnknownOption(true).action(function (app, options) {
  60. (0, _child_process.spawn)(app, options, {
  61. env: {
  62. ...process.env,
  63. PWDEBUG: '1'
  64. },
  65. stdio: 'inherit'
  66. });
  67. }).addHelpText('afterAll', `
  68. Examples:
  69. $ debug node test.js
  70. $ debug npm run test`);
  71. function suggestedBrowsersToInstall() {
  72. return _server.registry.executables().filter(e => e.installType !== 'none' && e.type !== 'tool').map(e => e.name).join(', ');
  73. }
  74. function checkBrowsersToInstall(args) {
  75. const faultyArguments = [];
  76. const executables = [];
  77. for (const arg of args) {
  78. const executable = _server.registry.findExecutable(arg);
  79. if (!executable || executable.installType === 'none') faultyArguments.push(arg);else executables.push(executable);
  80. }
  81. if (faultyArguments.length) throw new Error(`Invalid installation targets: ${faultyArguments.map(name => `'${name}'`).join(', ')}. Expecting one of: ${suggestedBrowsersToInstall()}`);
  82. return executables;
  83. }
  84. _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('--force', 'force reinstall of stable browser channels').action(async function (args, options) {
  85. if ((0, _utils.isLikelyNpxGlobal)()) {
  86. console.error((0, _utils.wrapInASCIIBox)([`WARNING: It looks like you are running 'npx playwright install' without first`, `installing your project's dependencies.`, ``, `To avoid unexpected behavior, please install your dependencies first, and`, `then run Playwright's install command:`, ``, ` npm install`, ` npx playwright install`, ``, `If your project does not yet depend on Playwright, first install the`, `applicable npm package (most commonly @playwright/test), and`, `then run Playwright's install command to download the browsers:`, ``, ` npm install @playwright/test`, ` npx playwright install`, ``].join('\n'), 1));
  87. }
  88. try {
  89. const hasNoArguments = !args.length;
  90. const executables = hasNoArguments ? _server.registry.defaultExecutables() : checkBrowsersToInstall(args);
  91. if (options.withDeps) await _server.registry.installDeps(executables, !!options.dryRun);
  92. if (options.dryRun) {
  93. for (const executable of executables) {
  94. var _executable$directory, _executable$downloadU;
  95. const version = executable.browserVersion ? `version ` + executable.browserVersion : '';
  96. console.log(`browser: ${executable.name}${version ? ' ' + version : ''}`);
  97. console.log(` Install location: ${(_executable$directory = executable.directory) !== null && _executable$directory !== void 0 ? _executable$directory : '<system>'}`);
  98. if ((_executable$downloadU = executable.downloadURLs) !== null && _executable$downloadU !== void 0 && _executable$downloadU.length) {
  99. const [url, ...fallbacks] = executable.downloadURLs;
  100. console.log(` Download url: ${url}`);
  101. for (let i = 0; i < fallbacks.length; ++i) console.log(` Download fallback ${i + 1}: ${fallbacks[i]}`);
  102. }
  103. console.log(``);
  104. }
  105. } else {
  106. const forceReinstall = hasNoArguments ? false : !!options.force;
  107. await _server.registry.install(executables, forceReinstall);
  108. }
  109. } catch (e) {
  110. console.log(`Failed to install browsers\n${e}`);
  111. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  112. }
  113. }).addHelpText('afterAll', `
  114. Examples:
  115. - $ install
  116. Install default browsers.
  117. - $ install chrome firefox
  118. Install custom browsers, supports ${suggestedBrowsersToInstall()}.`);
  119. _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 => {
  120. delete process.env.PLAYWRIGHT_SKIP_BROWSER_GC;
  121. await _server.registry.uninstall(!!options.all).then(({
  122. numberOfBrowsersLeft
  123. }) => {
  124. if (!options.all && numberOfBrowsersLeft > 0) {
  125. console.log('Successfully uninstalled Playwright browsers for the current Playwright installation.');
  126. console.log(`There are still ${numberOfBrowsersLeft} browsers left, used by other Playwright installations.\nTo uninstall Playwright browsers for all installations, re-run with --all flag.`);
  127. }
  128. }).catch(logErrorAndExit);
  129. });
  130. _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) {
  131. try {
  132. if (!args.length) await _server.registry.installDeps(_server.registry.defaultExecutables(), !!options.dryRun);else await _server.registry.installDeps(checkBrowsersToInstall(args), !!options.dryRun);
  133. } catch (e) {
  134. console.log(`Failed to install browser dependencies\n${e}`);
  135. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  136. }
  137. }).addHelpText('afterAll', `
  138. Examples:
  139. - $ install-deps
  140. Install dependencies for default browsers.
  141. - $ install-deps chrome firefox
  142. Install dependencies for specific browsers, supports ${suggestedBrowsersToInstall()}.`);
  143. const browsers = [{
  144. alias: 'cr',
  145. name: 'Chromium',
  146. type: 'chromium'
  147. }, {
  148. alias: 'ff',
  149. name: 'Firefox',
  150. type: 'firefox'
  151. }, {
  152. alias: 'wk',
  153. name: 'WebKit',
  154. type: 'webkit'
  155. }];
  156. for (const {
  157. alias,
  158. name,
  159. type
  160. } of browsers) {
  161. commandWithOpenOptions(`${alias} [url]`, `open page in ${name}`, []).action(function (url, options) {
  162. open({
  163. ...options,
  164. browser: type
  165. }, url, options.target).catch(logErrorAndExit);
  166. }).addHelpText('afterAll', `
  167. Examples:
  168. $ ${alias} https://example.com`);
  169. }
  170. commandWithOpenOptions('screenshot <url> <filename>', 'capture a page screenshot', [['--wait-for-selector <selector>', 'wait for selector before taking a screenshot'], ['--wait-for-timeout <timeout>', 'wait for timeout in milliseconds before taking a screenshot'], ['--full-page', 'whether to take a full page screenshot (entire scrollable area)']]).action(function (url, filename, command) {
  171. screenshot(command, command, url, filename).catch(logErrorAndExit);
  172. }).addHelpText('afterAll', `
  173. Examples:
  174. $ screenshot -b webkit https://example.com example.png`);
  175. commandWithOpenOptions('pdf <url> <filename>', 'save page as pdf', [['--wait-for-selector <selector>', 'wait for given selector before saving as pdf'], ['--wait-for-timeout <timeout>', 'wait for given timeout in milliseconds before saving as pdf']]).action(function (url, filename, options) {
  176. pdf(options, options, url, filename).catch(logErrorAndExit);
  177. }).addHelpText('afterAll', `
  178. Examples:
  179. $ pdf https://example.com example.pdf`);
  180. _utilsBundle.program.command('run-driver', {
  181. hidden: true
  182. }).action(function (options) {
  183. (0, _driver.runDriver)();
  184. });
  185. _utilsBundle.program.command('run-server', {
  186. hidden: true
  187. }).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) {
  188. (0, _driver.runServer)({
  189. port: options.port ? +options.port : undefined,
  190. host: options.host,
  191. path: options.path,
  192. maxConnections: options.maxClients ? +options.maxClients : Infinity,
  193. extension: options.mode === 'extension' || !!process.env.PW_EXTENSION_MODE
  194. }).catch(logErrorAndExit);
  195. });
  196. _utilsBundle.program.command('print-api-json', {
  197. hidden: true
  198. }).action(function (options) {
  199. (0, _driver.printApiJson)();
  200. });
  201. _utilsBundle.program.command('launch-server', {
  202. hidden: true
  203. }).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) {
  204. (0, _driver.launchBrowserServer)(options.browser, options.config);
  205. });
  206. _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) {
  207. if (options.browser === 'cr') options.browser = 'chromium';
  208. if (options.browser === 'ff') options.browser = 'firefox';
  209. if (options.browser === 'wk') options.browser = 'webkit';
  210. const openOptions = {
  211. headless: false,
  212. host: options.host,
  213. port: +options.port,
  214. isServer: !!options.stdin
  215. };
  216. if (options.port !== undefined || options.host !== undefined) {
  217. (0, _traceViewer.openTraceInBrowser)(traces, openOptions).catch(logErrorAndExit);
  218. } else {
  219. (0, _traceViewer.openTraceViewerApp)(traces, options.browser, openOptions).then(page => {
  220. page.on('close', () => (0, _utils.gracefullyProcessExitDoNotHang)(0));
  221. }).catch(logErrorAndExit);
  222. }
  223. }).addHelpText('afterAll', `
  224. Examples:
  225. $ show-trace https://example.com/trace.zip`);
  226. async function launchContext(options, headless, executablePath) {
  227. validateOptions(options);
  228. const browserType = lookupBrowserType(options);
  229. const launchOptions = {
  230. headless,
  231. executablePath
  232. };
  233. if (options.channel) launchOptions.channel = options.channel;
  234. launchOptions.handleSIGINT = false;
  235. const contextOptions =
  236. // Copy the device descriptor since we have to compare and modify the options.
  237. options.device ? {
  238. ...playwright.devices[options.device]
  239. } : {};
  240. // In headful mode, use host device scale factor for things to look nice.
  241. // In headless, keep things the way it works in Playwright by default.
  242. // Assume high-dpi on MacOS. TODO: this is not perfect.
  243. if (!headless) contextOptions.deviceScaleFactor = _os.default.platform() === 'darwin' ? 2 : 1;
  244. // Work around the WebKit GTK scrolling issue.
  245. if (browserType.name() === 'webkit' && process.platform === 'linux') {
  246. delete contextOptions.hasTouch;
  247. delete contextOptions.isMobile;
  248. }
  249. if (contextOptions.isMobile && browserType.name() === 'firefox') contextOptions.isMobile = undefined;
  250. if (options.blockServiceWorkers) contextOptions.serviceWorkers = 'block';
  251. // Proxy
  252. if (options.proxyServer) {
  253. launchOptions.proxy = {
  254. server: options.proxyServer
  255. };
  256. if (options.proxyBypass) launchOptions.proxy.bypass = options.proxyBypass;
  257. }
  258. const browser = await browserType.launch(launchOptions);
  259. if (process.env.PWTEST_CLI_IS_UNDER_TEST) {
  260. process._didSetSourcesForTest = text => {
  261. process.stdout.write('\n-------------8<-------------\n');
  262. process.stdout.write(text);
  263. process.stdout.write('\n-------------8<-------------\n');
  264. const autoExitCondition = process.env.PWTEST_CLI_AUTO_EXIT_WHEN;
  265. if (autoExitCondition && text.includes(autoExitCondition)) Promise.all(context.pages().map(async p => p.close()));
  266. };
  267. // Make sure we exit abnormally when browser crashes.
  268. const logs = [];
  269. require('playwright-core/lib/utilsBundle').debug.log = (...args) => {
  270. const line = require('util').format(...args) + '\n';
  271. logs.push(line);
  272. process.stderr.write(line);
  273. };
  274. browser.on('disconnected', () => {
  275. const hasCrashLine = logs.some(line => line.includes('process did exit:') && !line.includes('process did exit: exitCode=0, signal=null'));
  276. if (hasCrashLine) {
  277. process.stderr.write('Detected browser crash.\n');
  278. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  279. }
  280. });
  281. }
  282. // Viewport size
  283. if (options.viewportSize) {
  284. try {
  285. const [width, height] = options.viewportSize.split(',').map(n => parseInt(n, 10));
  286. contextOptions.viewport = {
  287. width,
  288. height
  289. };
  290. } catch (e) {
  291. throw new Error('Invalid viewport size format: use "width, height", for example --viewport-size=800,600');
  292. }
  293. }
  294. // Geolocation
  295. if (options.geolocation) {
  296. try {
  297. const [latitude, longitude] = options.geolocation.split(',').map(n => parseFloat(n.trim()));
  298. contextOptions.geolocation = {
  299. latitude,
  300. longitude
  301. };
  302. } catch (e) {
  303. throw new Error('Invalid geolocation format, should be "lat,long". For example --geolocation="37.819722,-122.478611"');
  304. }
  305. contextOptions.permissions = ['geolocation'];
  306. }
  307. // User agent
  308. if (options.userAgent) contextOptions.userAgent = options.userAgent;
  309. // Lang
  310. if (options.lang) contextOptions.locale = options.lang;
  311. // Color scheme
  312. if (options.colorScheme) contextOptions.colorScheme = options.colorScheme;
  313. // Timezone
  314. if (options.timezone) contextOptions.timezoneId = options.timezone;
  315. // Storage
  316. if (options.loadStorage) contextOptions.storageState = options.loadStorage;
  317. if (options.ignoreHttpsErrors) contextOptions.ignoreHTTPSErrors = true;
  318. // HAR
  319. if (options.saveHar) {
  320. contextOptions.recordHar = {
  321. path: _path.default.resolve(process.cwd(), options.saveHar),
  322. mode: 'minimal'
  323. };
  324. if (options.saveHarGlob) contextOptions.recordHar.urlFilter = options.saveHarGlob;
  325. contextOptions.serviceWorkers = 'block';
  326. }
  327. // Close app when the last window closes.
  328. const context = await browser.newContext(contextOptions);
  329. let closingBrowser = false;
  330. async function closeBrowser() {
  331. // We can come here multiple times. For example, saving storage creates
  332. // a temporary page and we call closeBrowser again when that page closes.
  333. if (closingBrowser) return;
  334. closingBrowser = true;
  335. if (options.saveTrace) await context.tracing.stop({
  336. path: options.saveTrace
  337. });
  338. if (options.saveStorage) await context.storageState({
  339. path: options.saveStorage
  340. }).catch(e => null);
  341. if (options.saveHar) await context.close();
  342. await browser.close();
  343. }
  344. context.on('page', page => {
  345. page.on('dialog', () => {}); // Prevent dialogs from being automatically dismissed.
  346. page.on('close', () => {
  347. const hasPage = browser.contexts().some(context => context.pages().length > 0);
  348. if (hasPage) return;
  349. // Avoid the error when the last page is closed because the browser has been closed.
  350. closeBrowser().catch(e => null);
  351. });
  352. });
  353. process.on('SIGINT', async () => {
  354. await closeBrowser();
  355. (0, _utils.gracefullyProcessExitDoNotHang)(130);
  356. });
  357. const timeout = options.timeout ? parseInt(options.timeout, 10) : 0;
  358. context.setDefaultTimeout(timeout);
  359. context.setDefaultNavigationTimeout(timeout);
  360. if (options.saveTrace) await context.tracing.start({
  361. screenshots: true,
  362. snapshots: true
  363. });
  364. // Omit options that we add automatically for presentation purpose.
  365. delete launchOptions.headless;
  366. delete launchOptions.executablePath;
  367. delete launchOptions.handleSIGINT;
  368. delete contextOptions.deviceScaleFactor;
  369. return {
  370. browser,
  371. browserName: browserType.name(),
  372. context,
  373. contextOptions,
  374. launchOptions
  375. };
  376. }
  377. async function openPage(context, url) {
  378. const page = await context.newPage();
  379. if (url) {
  380. if (_fs.default.existsSync(url)) url = 'file://' + _path.default.resolve(url);else if (!url.startsWith('http') && !url.startsWith('file://') && !url.startsWith('about:') && !url.startsWith('data:')) url = 'http://' + url;
  381. await page.goto(url).catch(error => {
  382. if (process.env.PWTEST_CLI_AUTO_EXIT_WHEN && (0, _errors.isTargetClosedError)(error)) {
  383. // Tests with PWTEST_CLI_AUTO_EXIT_WHEN might close page too fast, resulting
  384. // in a stray navigation aborted error. We should ignore it.
  385. } else {
  386. throw error;
  387. }
  388. });
  389. }
  390. return page;
  391. }
  392. async function open(options, url, language) {
  393. const {
  394. context,
  395. launchOptions,
  396. contextOptions
  397. } = await launchContext(options, !!process.env.PWTEST_CLI_HEADLESS, process.env.PWTEST_CLI_EXECUTABLE_PATH);
  398. await context._enableRecorder({
  399. language,
  400. launchOptions,
  401. contextOptions,
  402. device: options.device,
  403. saveStorage: options.saveStorage
  404. });
  405. await openPage(context, url);
  406. }
  407. async function codegen(options, url) {
  408. const {
  409. target: language,
  410. output: outputFile,
  411. testIdAttribute: testIdAttributeName
  412. } = options;
  413. const {
  414. context,
  415. launchOptions,
  416. contextOptions
  417. } = await launchContext(options, !!process.env.PWTEST_CLI_HEADLESS, process.env.PWTEST_CLI_EXECUTABLE_PATH);
  418. await context._enableRecorder({
  419. language,
  420. launchOptions,
  421. contextOptions,
  422. device: options.device,
  423. saveStorage: options.saveStorage,
  424. mode: 'recording',
  425. testIdAttributeName,
  426. outputFile: outputFile ? _path.default.resolve(outputFile) : undefined,
  427. handleSIGINT: false
  428. });
  429. await openPage(context, url);
  430. }
  431. async function waitForPage(page, captureOptions) {
  432. if (captureOptions.waitForSelector) {
  433. console.log(`Waiting for selector ${captureOptions.waitForSelector}...`);
  434. await page.waitForSelector(captureOptions.waitForSelector);
  435. }
  436. if (captureOptions.waitForTimeout) {
  437. console.log(`Waiting for timeout ${captureOptions.waitForTimeout}...`);
  438. await page.waitForTimeout(parseInt(captureOptions.waitForTimeout, 10));
  439. }
  440. }
  441. async function screenshot(options, captureOptions, url, path) {
  442. const {
  443. context
  444. } = await launchContext(options, true);
  445. console.log('Navigating to ' + url);
  446. const page = await openPage(context, url);
  447. await waitForPage(page, captureOptions);
  448. console.log('Capturing screenshot into ' + path);
  449. await page.screenshot({
  450. path,
  451. fullPage: !!captureOptions.fullPage
  452. });
  453. // launchContext takes care of closing the browser.
  454. await page.close();
  455. }
  456. async function pdf(options, captureOptions, url, path) {
  457. if (options.browser !== 'chromium') throw new Error('PDF creation is only working with Chromium');
  458. const {
  459. context
  460. } = await launchContext({
  461. ...options,
  462. browser: 'chromium'
  463. }, true);
  464. console.log('Navigating to ' + url);
  465. const page = await openPage(context, url);
  466. await waitForPage(page, captureOptions);
  467. console.log('Saving as pdf into ' + path);
  468. await page.pdf({
  469. path
  470. });
  471. // launchContext takes care of closing the browser.
  472. await page.close();
  473. }
  474. function lookupBrowserType(options) {
  475. let name = options.browser;
  476. if (options.device) {
  477. const device = playwright.devices[options.device];
  478. name = device.defaultBrowserType;
  479. }
  480. let browserType;
  481. switch (name) {
  482. case 'chromium':
  483. browserType = playwright.chromium;
  484. break;
  485. case 'webkit':
  486. browserType = playwright.webkit;
  487. break;
  488. case 'firefox':
  489. browserType = playwright.firefox;
  490. break;
  491. case 'cr':
  492. browserType = playwright.chromium;
  493. break;
  494. case 'wk':
  495. browserType = playwright.webkit;
  496. break;
  497. case 'ff':
  498. browserType = playwright.firefox;
  499. break;
  500. }
  501. if (browserType) return browserType;
  502. _utilsBundle.program.help();
  503. }
  504. function validateOptions(options) {
  505. if (options.device && !(options.device in playwright.devices)) {
  506. const lines = [`Device descriptor not found: '${options.device}', available devices are:`];
  507. for (const name in playwright.devices) lines.push(` "${name}"`);
  508. throw new Error(lines.join('\n'));
  509. }
  510. if (options.colorScheme && !['light', 'dark'].includes(options.colorScheme)) throw new Error('Invalid color scheme, should be one of "light", "dark"');
  511. }
  512. function logErrorAndExit(e) {
  513. if (process.env.PWDEBUGIMPL) console.error(e);else console.error(e.name + ': ' + e.message);
  514. (0, _utils.gracefullyProcessExitDoNotHang)(1);
  515. }
  516. function codegenId() {
  517. return process.env.PW_LANG_NAME || 'playwright-test';
  518. }
  519. function commandWithOpenOptions(command, description, options) {
  520. let result = _utilsBundle.program.command(command).description(description);
  521. for (const option of options) result = result.option(option[0], ...option.slice(1));
  522. 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('--viewport-size <size>', 'specify browser viewport size in pixels, for example "1280, 720"');
  523. }
  524. function buildBasePlaywrightCLICommand(cliTargetLang) {
  525. switch (cliTargetLang) {
  526. case 'python':
  527. return `playwright`;
  528. case 'java':
  529. return `mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="...options.."`;
  530. case 'csharp':
  531. return `pwsh bin/Debug/netX/playwright.ps1`;
  532. default:
  533. {
  534. const packageManagerCommand = (0, _utils.getPackageManagerExecCommand)();
  535. return `${packageManagerCommand} playwright`;
  536. }
  537. }
  538. }
  539. var _default = exports.default = _utilsBundle.program;