browser.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.Browser = void 0;
  6. var _browserContext = require("./browserContext");
  7. var _page = require("./page");
  8. var _download = require("./download");
  9. var _instrumentation = require("./instrumentation");
  10. var _artifact = require("./artifact");
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class Browser extends _instrumentation.SdkObject {
  27. constructor(parent, options) {
  28. super(parent, 'browser');
  29. this.options = void 0;
  30. this._downloads = new Map();
  31. this._defaultContext = null;
  32. this._startedClosing = false;
  33. this._idToVideo = new Map();
  34. this._contextForReuse = void 0;
  35. this._closeReason = void 0;
  36. this._isCollocatedWithServer = true;
  37. this.attribution.browser = this;
  38. this.options = options;
  39. this.instrumentation.onBrowserOpen(this);
  40. }
  41. async newContext(metadata, options) {
  42. (0, _browserContext.validateBrowserContextOptions)(options, this.options);
  43. const context = await this.doCreateNewContext(options);
  44. if (options.storageState) await context.setStorageState(metadata, options.storageState);
  45. return context;
  46. }
  47. async newContextForReuse(params, metadata) {
  48. const hash = _browserContext.BrowserContext.reusableContextHash(params);
  49. if (!this._contextForReuse || hash !== this._contextForReuse.hash || !this._contextForReuse.context.canResetForReuse()) {
  50. if (this._contextForReuse) await this._contextForReuse.context.close({
  51. reason: 'Context reused'
  52. });
  53. this._contextForReuse = {
  54. context: await this.newContext(metadata, params),
  55. hash
  56. };
  57. return {
  58. context: this._contextForReuse.context,
  59. needsReset: false
  60. };
  61. }
  62. await this._contextForReuse.context.stopPendingOperations('Context recreated');
  63. return {
  64. context: this._contextForReuse.context,
  65. needsReset: true
  66. };
  67. }
  68. async stopPendingOperations(reason) {
  69. var _this$_contextForReus, _this$_contextForReus2;
  70. await ((_this$_contextForReus = this._contextForReuse) === null || _this$_contextForReus === void 0 ? void 0 : (_this$_contextForReus2 = _this$_contextForReus.context) === null || _this$_contextForReus2 === void 0 ? void 0 : _this$_contextForReus2.stopPendingOperations(reason));
  71. }
  72. _downloadCreated(page, uuid, url, suggestedFilename) {
  73. const download = new _download.Download(page, this.options.downloadsPath || '', uuid, url, suggestedFilename);
  74. this._downloads.set(uuid, download);
  75. }
  76. _downloadFilenameSuggested(uuid, suggestedFilename) {
  77. const download = this._downloads.get(uuid);
  78. if (!download) return;
  79. download._filenameSuggested(suggestedFilename);
  80. }
  81. _downloadFinished(uuid, error) {
  82. const download = this._downloads.get(uuid);
  83. if (!download) return;
  84. download.artifact.reportFinished(error ? new Error(error) : undefined);
  85. this._downloads.delete(uuid);
  86. }
  87. _videoStarted(context, videoId, path, pageOrError) {
  88. const artifact = new _artifact.Artifact(context, path);
  89. this._idToVideo.set(videoId, {
  90. context,
  91. artifact
  92. });
  93. pageOrError.then(page => {
  94. if (page instanceof _page.Page) {
  95. page._video = artifact;
  96. page.emitOnContext(_browserContext.BrowserContext.Events.VideoStarted, artifact);
  97. page.emit(_page.Page.Events.Video, artifact);
  98. }
  99. });
  100. }
  101. _takeVideo(videoId) {
  102. const video = this._idToVideo.get(videoId);
  103. this._idToVideo.delete(videoId);
  104. return video === null || video === void 0 ? void 0 : video.artifact;
  105. }
  106. _didClose() {
  107. for (const context of this.contexts()) context._browserClosed();
  108. if (this._defaultContext) this._defaultContext._browserClosed();
  109. this.emit(Browser.Events.Disconnected);
  110. this.instrumentation.onBrowserClose(this);
  111. }
  112. async close(options) {
  113. if (!this._startedClosing) {
  114. if (options.reason) this._closeReason = options.reason;
  115. this._startedClosing = true;
  116. await this.options.browserProcess.close();
  117. }
  118. if (this.isConnected()) await new Promise(x => this.once(Browser.Events.Disconnected, x));
  119. }
  120. async killForTests() {
  121. await this.options.browserProcess.kill();
  122. }
  123. }
  124. exports.Browser = Browser;
  125. Browser.Events = {
  126. Disconnected: 'disconnected'
  127. };