browser.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var browser_exports = {};
  20. __export(browser_exports, {
  21. Browser: () => Browser
  22. });
  23. module.exports = __toCommonJS(browser_exports);
  24. var import_artifact = require("./artifact");
  25. var import_browserContext = require("./browserContext");
  26. var import_cdpSession = require("./cdpSession");
  27. var import_channelOwner = require("./channelOwner");
  28. var import_errors = require("./errors");
  29. var import_events = require("./events");
  30. var import_fileUtils = require("./fileUtils");
  31. class Browser extends import_channelOwner.ChannelOwner {
  32. constructor(parent, type, guid, initializer) {
  33. super(parent, type, guid, initializer);
  34. this._contexts = /* @__PURE__ */ new Set();
  35. this._isConnected = true;
  36. this._shouldCloseConnectionOnClose = false;
  37. this._options = {};
  38. this._name = initializer.name;
  39. this._channel.on("context", ({ context }) => this._didCreateContext(import_browserContext.BrowserContext.from(context)));
  40. this._channel.on("close", () => this._didClose());
  41. this._closedPromise = new Promise((f) => this.once(import_events.Events.Browser.Disconnected, f));
  42. }
  43. static from(browser) {
  44. return browser._object;
  45. }
  46. browserType() {
  47. return this._browserType;
  48. }
  49. async newContext(options = {}) {
  50. return await this._innerNewContext(options, false);
  51. }
  52. async _newContextForReuse(options = {}) {
  53. return await this._innerNewContext(options, true);
  54. }
  55. async _disconnectFromReusedContext(reason) {
  56. const context = [...this._contexts].find((context2) => context2._forReuse);
  57. if (!context)
  58. return;
  59. await this._instrumentation.runBeforeCloseBrowserContext(context);
  60. for (const page of context.pages())
  61. page._onClose();
  62. context._onClose();
  63. await this._channel.disconnectFromReusedContext({ reason });
  64. }
  65. async _innerNewContext(userOptions = {}, forReuse) {
  66. const options = this._browserType._playwright.selectors._withSelectorOptions(userOptions);
  67. await this._instrumentation.runBeforeCreateBrowserContext(options);
  68. const contextOptions = await (0, import_browserContext.prepareBrowserContextParams)(this._platform, options);
  69. const response = forReuse ? await this._channel.newContextForReuse(contextOptions) : await this._channel.newContext(contextOptions);
  70. const context = import_browserContext.BrowserContext.from(response.context);
  71. if (forReuse)
  72. context._forReuse = true;
  73. if (options.logger)
  74. context._logger = options.logger;
  75. await context._initializeHarFromOptions(options.recordHar);
  76. await this._instrumentation.runAfterCreateBrowserContext(context);
  77. return context;
  78. }
  79. _connectToBrowserType(browserType, browserOptions, logger) {
  80. this._browserType = browserType;
  81. this._options = browserOptions;
  82. this._logger = logger;
  83. for (const context of this._contexts)
  84. this._setupBrowserContext(context);
  85. }
  86. _didCreateContext(context) {
  87. context._browser = this;
  88. this._contexts.add(context);
  89. if (this._browserType)
  90. this._setupBrowserContext(context);
  91. }
  92. _setupBrowserContext(context) {
  93. context._logger = this._logger;
  94. context.tracing._tracesDir = this._options.tracesDir;
  95. this._browserType._contexts.add(context);
  96. this._browserType._playwright.selectors._contextsForSelectors.add(context);
  97. context.setDefaultTimeout(this._browserType._playwright._defaultContextTimeout);
  98. context.setDefaultNavigationTimeout(this._browserType._playwright._defaultContextNavigationTimeout);
  99. }
  100. contexts() {
  101. return [...this._contexts];
  102. }
  103. version() {
  104. return this._initializer.version;
  105. }
  106. async newPage(options = {}) {
  107. return await this._wrapApiCall(async () => {
  108. const context = await this.newContext(options);
  109. const page = await context.newPage();
  110. page._ownedContext = context;
  111. context._ownerPage = page;
  112. return page;
  113. }, { title: "Create page" });
  114. }
  115. isConnected() {
  116. return this._isConnected;
  117. }
  118. async newBrowserCDPSession() {
  119. return import_cdpSession.CDPSession.from((await this._channel.newBrowserCDPSession()).session);
  120. }
  121. async startTracing(page, options = {}) {
  122. this._path = options.path;
  123. await this._channel.startTracing({ ...options, page: page ? page._channel : void 0 });
  124. }
  125. async stopTracing() {
  126. const artifact = import_artifact.Artifact.from((await this._channel.stopTracing()).artifact);
  127. const buffer = await artifact.readIntoBuffer();
  128. await artifact.delete();
  129. if (this._path) {
  130. await (0, import_fileUtils.mkdirIfNeeded)(this._platform, this._path);
  131. await this._platform.fs().promises.writeFile(this._path, buffer);
  132. this._path = void 0;
  133. }
  134. return buffer;
  135. }
  136. async [Symbol.asyncDispose]() {
  137. await this.close();
  138. }
  139. async close(options = {}) {
  140. this._closeReason = options.reason;
  141. try {
  142. if (this._shouldCloseConnectionOnClose)
  143. this._connection.close();
  144. else
  145. await this._channel.close(options);
  146. await this._closedPromise;
  147. } catch (e) {
  148. if ((0, import_errors.isTargetClosedError)(e))
  149. return;
  150. throw e;
  151. }
  152. }
  153. _didClose() {
  154. this._isConnected = false;
  155. this.emit(import_events.Events.Browser.Disconnected, this);
  156. }
  157. }
  158. // Annotate the CommonJS export names for ESM import in node:
  159. 0 && (module.exports = {
  160. Browser
  161. });