Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

launcher.js 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const chrome_launcher_1 = require("chrome-launcher");
  7. const puppeteer_core_1 = __importDefault(require("puppeteer-core"));
  8. const logger_1 = __importDefault(require("@wdio/logger"));
  9. const finder_1 = __importDefault(require("./finder"));
  10. const utils_1 = require("./utils");
  11. const constants_1 = require("./constants");
  12. const log = logger_1.default('devtools');
  13. const DEVICE_NAMES = Object.values(puppeteer_core_1.default.devices).map((device) => device.name);
  14. async function launchChrome(capabilities) {
  15. const chromeOptions = capabilities[constants_1.VENDOR_PREFIX.chrome] || {};
  16. const mobileEmulation = chromeOptions.mobileEmulation || {};
  17. const devtoolsOptions = capabilities['wdio:devtoolsOptions'];
  18. let ignoreDefaultArgs = capabilities.ignoreDefaultArgs;
  19. let headless = chromeOptions.headless;
  20. if (devtoolsOptions) {
  21. ignoreDefaultArgs = devtoolsOptions.ignoreDefaultArgs;
  22. headless = devtoolsOptions.headless;
  23. }
  24. if (typeof mobileEmulation.deviceName === 'string') {
  25. const deviceProperties = Object.values(puppeteer_core_1.default.devices).find(device => device.name === mobileEmulation.deviceName);
  26. if (!deviceProperties) {
  27. throw new Error(`Unknown device name "${mobileEmulation.deviceName}", available: ${DEVICE_NAMES.join(', ')}`);
  28. }
  29. mobileEmulation.userAgent = deviceProperties.userAgent;
  30. mobileEmulation.deviceMetrics = {
  31. width: deviceProperties.viewport.width,
  32. height: deviceProperties.viewport.height,
  33. pixelRatio: deviceProperties.viewport.deviceScaleFactor
  34. };
  35. }
  36. const defaultFlags = Array.isArray(ignoreDefaultArgs) ? constants_1.DEFAULT_FLAGS.filter(flag => !ignoreDefaultArgs.includes(flag)) : (!ignoreDefaultArgs) ? constants_1.DEFAULT_FLAGS : [];
  37. const deviceMetrics = mobileEmulation.deviceMetrics || {};
  38. const chromeFlags = [
  39. ...defaultFlags,
  40. ...[
  41. `--window-position=${constants_1.DEFAULT_X_POSITION},${constants_1.DEFAULT_Y_POSITION}`,
  42. `--window-size=${constants_1.DEFAULT_WIDTH},${constants_1.DEFAULT_HEIGHT}`
  43. ],
  44. ...(headless ? [
  45. '--headless',
  46. '--no-sandbox'
  47. ] : []),
  48. ...(chromeOptions.args || [])
  49. ];
  50. if (typeof deviceMetrics.pixelRatio === 'number') {
  51. chromeFlags.push(`--device-scale-factor=${deviceMetrics.pixelRatio}`);
  52. }
  53. if (typeof mobileEmulation.userAgent === 'string') {
  54. chromeFlags.push(`--user-agent=${mobileEmulation.userAgent}`);
  55. }
  56. log.info(`Launch Google Chrome with flags: ${chromeFlags.join(' ')}`);
  57. const chrome = await chrome_launcher_1.launch({
  58. chromePath: chromeOptions.binary,
  59. ignoreDefaultFlags: true,
  60. chromeFlags
  61. });
  62. log.info(`Connect Puppeteer with browser on port ${chrome.port}`);
  63. const browser = await puppeteer_core_1.default.connect({
  64. ...chromeOptions,
  65. browserURL: `http://localhost:${chrome.port}`,
  66. defaultViewport: null
  67. });
  68. const pages = await utils_1.getPages(browser);
  69. for (const page of pages.slice(0, -1)) {
  70. if (page.url() === 'about:blank') {
  71. await page.close();
  72. }
  73. }
  74. if (deviceMetrics.width && deviceMetrics.height) {
  75. await pages[0].setViewport(deviceMetrics);
  76. }
  77. return browser;
  78. }
  79. function launchBrowser(capabilities, browserType) {
  80. var _a;
  81. const product = browserType === constants_1.BROWSER_TYPE.firefox ? constants_1.BROWSER_TYPE.firefox : constants_1.BROWSER_TYPE.chrome;
  82. const vendorCapKey = constants_1.VENDOR_PREFIX[browserType];
  83. const devtoolsOptions = capabilities['wdio:devtoolsOptions'];
  84. let ignoreDefaultArgs = capabilities.ignoreDefaultArgs;
  85. let headless = capabilities.headless;
  86. if (devtoolsOptions) {
  87. ignoreDefaultArgs = devtoolsOptions.ignoreDefaultArgs;
  88. headless = devtoolsOptions.headless;
  89. }
  90. if (!capabilities[vendorCapKey]) {
  91. capabilities[vendorCapKey] = {};
  92. }
  93. const browserFinderMethod = finder_1.default(browserType, process.platform);
  94. const executablePath = (((_a = capabilities[vendorCapKey]) === null || _a === void 0 ? void 0 : _a.binary) ||
  95. browserFinderMethod()[0]);
  96. const puppeteerOptions = Object.assign({
  97. product,
  98. executablePath,
  99. ignoreDefaultArgs,
  100. headless: Boolean(headless),
  101. defaultViewport: {
  102. width: constants_1.DEFAULT_WIDTH,
  103. height: constants_1.DEFAULT_HEIGHT
  104. }
  105. }, capabilities[vendorCapKey] || {}, devtoolsOptions || {});
  106. if (!executablePath) {
  107. throw new Error('Couldn\'t find executable for browser');
  108. }
  109. else if (browserType === constants_1.BROWSER_TYPE.firefox &&
  110. executablePath !== 'firefox' &&
  111. !executablePath.toLowerCase().includes(constants_1.CHANNEL_FIREFOX_NIGHTLY) &&
  112. !executablePath.toLowerCase().includes(constants_1.CHANNEL_FIREFOX_TRUNK)) {
  113. throw new Error(constants_1.BROWSER_ERROR_MESSAGES.firefoxNightly);
  114. }
  115. log.info(`Launch ${executablePath} with config: ${JSON.stringify(puppeteerOptions)}`);
  116. return puppeteer_core_1.default.launch(puppeteerOptions);
  117. }
  118. function launch(capabilities) {
  119. var _a;
  120. const browserName = (_a = capabilities.browserName) === null || _a === void 0 ? void 0 : _a.toLowerCase();
  121. if (browserName && constants_1.CHROME_NAMES.includes(browserName)) {
  122. return launchChrome(capabilities);
  123. }
  124. if (browserName && constants_1.FIREFOX_NAMES.includes(browserName)) {
  125. return launchBrowser(capabilities, constants_1.BROWSER_TYPE.firefox);
  126. }
  127. if (browserName && constants_1.EDGE_NAMES.includes(browserName)) {
  128. return launchBrowser(capabilities, constants_1.BROWSER_TYPE.edge);
  129. }
  130. throw new Error(`Couldn't identify browserName "${browserName}"`);
  131. }
  132. exports.default = launch;