launchApp.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.launchApp = launchApp;
  6. exports.syncLocalStorageWithSettings = syncLocalStorageWithSettings;
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _path = _interopRequireDefault(require("path"));
  9. var _registry = require("./registry");
  10. var _utils = require("../utils");
  11. var _instrumentation = require("./instrumentation");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. /**
  14. * Copyright (c) Microsoft Corporation.
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License");
  17. * you may not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS,
  24. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. */
  28. async function launchApp(browserType, options) {
  29. var _options$persistentCo, _options$persistentCo2;
  30. const args = [...((_options$persistentCo = (_options$persistentCo2 = options.persistentContextOptions) === null || _options$persistentCo2 === void 0 ? void 0 : _options$persistentCo2.args) !== null && _options$persistentCo !== void 0 ? _options$persistentCo : [])];
  31. if (browserType.name() === 'chromium') {
  32. args.push('--app=data:text/html,', `--window-size=${options.windowSize.width},${options.windowSize.height}`, ...(options.windowPosition ? [`--window-position=${options.windowPosition.x},${options.windowPosition.y}`] : []), '--test-type=');
  33. }
  34. const context = await browserType.launchPersistentContext((0, _instrumentation.serverSideCallMetadata)(), '', {
  35. channel: (0, _registry.findChromiumChannel)(options.sdkLanguage),
  36. noDefaultViewport: true,
  37. ignoreDefaultArgs: ['--enable-automation'],
  38. colorScheme: 'no-override',
  39. acceptDownloads: (0, _utils.isUnderTest)() ? 'accept' : 'internal-browser-default',
  40. ...(options === null || options === void 0 ? void 0 : options.persistentContextOptions),
  41. args
  42. });
  43. const [page] = context.pages();
  44. // Chromium on macOS opens a new tab when clicking on the dock icon.
  45. // See https://github.com/microsoft/playwright/issues/9434
  46. if (browserType.name() === 'chromium' && process.platform === 'darwin') {
  47. context.on('page', async newPage => {
  48. if (newPage.mainFrame().url() === 'chrome://new-tab-page/') {
  49. await page.bringToFront();
  50. await newPage.close((0, _instrumentation.serverSideCallMetadata)());
  51. }
  52. });
  53. }
  54. if (browserType.name() === 'chromium') await installAppIcon(page);
  55. return {
  56. context,
  57. page
  58. };
  59. }
  60. async function installAppIcon(page) {
  61. const icon = await _fs.default.promises.readFile(require.resolve('./chromium/appIcon.png'));
  62. const crPage = page._delegate;
  63. await crPage._mainFrameSession._client.send('Browser.setDockTile', {
  64. image: icon.toString('base64')
  65. });
  66. }
  67. async function syncLocalStorageWithSettings(page, appName) {
  68. if ((0, _utils.isUnderTest)()) return;
  69. const settingsFile = _path.default.join(_registry.registryDirectory, '.settings', `${appName}.json`);
  70. await page.exposeBinding('_saveSerializedSettings', false, (_, settings) => {
  71. _fs.default.mkdirSync(_path.default.dirname(settingsFile), {
  72. recursive: true
  73. });
  74. _fs.default.writeFileSync(settingsFile, settings);
  75. });
  76. const settings = await _fs.default.promises.readFile(settingsFile, 'utf-8').catch(() => '{}');
  77. await page.addInitScript(`(${String(settings => {
  78. // iframes w/ snapshots, etc.
  79. if (location && location.protocol === 'data:') return;
  80. Object.entries(settings).map(([k, v]) => localStorage[k] = v);
  81. window.saveSettings = () => {
  82. window._saveSerializedSettings(JSON.stringify({
  83. ...localStorage
  84. }));
  85. };
  86. })})(${settings});
  87. `);
  88. }