firefox.js 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Firefox = void 0;
  6. var os = _interopRequireWildcard(require("os"));
  7. var _path = _interopRequireDefault(require("path"));
  8. var _ffBrowser = require("./ffBrowser");
  9. var _ffConnection = require("./ffConnection");
  10. var _browserType = require("../browserType");
  11. var _utils = require("../../utils");
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. 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); }
  14. 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; }
  15. /**
  16. * Copyright 2017 Google Inc. All rights reserved.
  17. * Modifications 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. class Firefox extends _browserType.BrowserType {
  32. constructor(parent) {
  33. super(parent, 'firefox');
  34. }
  35. _connectToTransport(transport, options) {
  36. return _ffBrowser.FFBrowser.connect(this.attribution.playwright, transport, options);
  37. }
  38. _doRewriteStartupLog(error) {
  39. if (!error.logs) return error;
  40. // https://github.com/microsoft/playwright/issues/6500
  41. if (error.logs.includes(`as root in a regular user's session is not supported.`)) error.logs = '\n' + (0, _utils.wrapInASCIIBox)(`Firefox is unable to launch if the $HOME folder isn't owned by the current user.\nWorkaround: Set the HOME=/root environment variable${process.env.GITHUB_ACTION ? ' in your GitHub Actions workflow file' : ''} when running Playwright.`, 1);
  42. if (error.logs.includes('no DISPLAY environment variable specified')) error.logs = '\n' + (0, _utils.wrapInASCIIBox)(_browserType.kNoXServerRunningError, 1);
  43. return error;
  44. }
  45. _amendEnvironment(env, userDataDir, executable, browserArguments) {
  46. if (!_path.default.isAbsolute(os.homedir())) throw new Error(`Cannot launch Firefox with relative home directory. Did you set ${os.platform() === 'win32' ? 'USERPROFILE' : 'HOME'} to a relative path?`);
  47. if (os.platform() === 'linux') {
  48. // Always remove SNAP_NAME and SNAP_INSTANCE_NAME env variables since they
  49. // confuse Firefox: in our case, builds never come from SNAP.
  50. // See https://github.com/microsoft/playwright/issues/20555
  51. return {
  52. ...env,
  53. SNAP_NAME: undefined,
  54. SNAP_INSTANCE_NAME: undefined
  55. };
  56. }
  57. return env;
  58. }
  59. _attemptToGracefullyCloseBrowser(transport) {
  60. const message = {
  61. method: 'Browser.close',
  62. params: {},
  63. id: _ffConnection.kBrowserCloseMessageId
  64. };
  65. transport.send(message);
  66. }
  67. _defaultArgs(options, isPersistent, userDataDir) {
  68. const {
  69. args = [],
  70. headless
  71. } = options;
  72. const userDataDirArg = args.find(arg => arg.startsWith('-profile') || arg.startsWith('--profile'));
  73. if (userDataDirArg) throw this._createUserDataDirArgMisuseError('--profile');
  74. if (args.find(arg => arg.startsWith('-juggler'))) throw new Error('Use the port parameter instead of -juggler argument');
  75. const firefoxArguments = ['-no-remote'];
  76. if (headless) {
  77. firefoxArguments.push('-headless');
  78. } else {
  79. firefoxArguments.push('-wait-for-browser');
  80. firefoxArguments.push('-foreground');
  81. }
  82. firefoxArguments.push(`-profile`, userDataDir);
  83. firefoxArguments.push('-juggler-pipe');
  84. firefoxArguments.push(...args);
  85. if (isPersistent) firefoxArguments.push('about:blank');else firefoxArguments.push('-silent');
  86. return firefoxArguments;
  87. }
  88. }
  89. exports.Firefox = Firefox;