browser.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(options = {}, forReuse) {
  66. options = this._browserType._playwright.selectors._withSelectorOptions({
  67. ...this._browserType._playwright._defaultContextOptions,
  68. ...options
  69. });
  70. const contextOptions = await (0, import_browserContext.prepareBrowserContextParams)(this._platform, options);
  71. const response = forReuse ? await this._channel.newContextForReuse(contextOptions) : await this._channel.newContext(contextOptions);
  72. const context = import_browserContext.BrowserContext.from(response.context);
  73. if (forReuse)
  74. context._forReuse = true;
  75. if (options.logger)
  76. context._logger = options.logger;
  77. await context._initializeHarFromOptions(options.recordHar);
  78. await this._instrumentation.runAfterCreateBrowserContext(context);
  79. return context;
  80. }
  81. _connectToBrowserType(browserType, browserOptions, logger) {
  82. this._browserType = browserType;
  83. this._options = browserOptions;
  84. this._logger = logger;
  85. for (const context of this._contexts)
  86. this._setupBrowserContext(context);
  87. }
  88. _didCreateContext(context) {
  89. context._browser = this;
  90. this._contexts.add(context);
  91. if (this._browserType)
  92. this._setupBrowserContext(context);
  93. }
  94. _setupBrowserContext(context) {
  95. context._logger = this._logger;
  96. context.tracing._tracesDir = this._options.tracesDir;
  97. this._browserType._contexts.add(context);
  98. this._browserType._playwright.selectors._contextsForSelectors.add(context);
  99. context.setDefaultTimeout(this._browserType._playwright._defaultContextTimeout);
  100. context.setDefaultNavigationTimeout(this._browserType._playwright._defaultContextNavigationTimeout);
  101. }
  102. contexts() {
  103. return [...this._contexts];
  104. }
  105. version() {
  106. return this._initializer.version;
  107. }
  108. async newPage(options = {}) {
  109. return await this._wrapApiCall(async () => {
  110. const context = await this.newContext(options);
  111. const page = await context.newPage();
  112. page._ownedContext = context;
  113. context._ownerPage = page;
  114. return page;
  115. }, { title: "Create page" });
  116. }
  117. isConnected() {
  118. return this._isConnected;
  119. }
  120. async newBrowserCDPSession() {
  121. return import_cdpSession.CDPSession.from((await this._channel.newBrowserCDPSession()).session);
  122. }
  123. async _launchServer(options = {}) {
  124. const serverLauncher = this._browserType._serverLauncher;
  125. const browserImpl = this._connection.toImpl?.(this);
  126. if (!serverLauncher || !browserImpl)
  127. throw new Error("Launching server is not supported");
  128. return await serverLauncher.launchServerOnExistingBrowser(browserImpl, {
  129. _sharedBrowser: true,
  130. ...options
  131. });
  132. }
  133. async startTracing(page, options = {}) {
  134. this._path = options.path;
  135. await this._channel.startTracing({ ...options, page: page ? page._channel : void 0 });
  136. }
  137. async stopTracing() {
  138. const artifact = import_artifact.Artifact.from((await this._channel.stopTracing()).artifact);
  139. const buffer = await artifact.readIntoBuffer();
  140. await artifact.delete();
  141. if (this._path) {
  142. await (0, import_fileUtils.mkdirIfNeeded)(this._platform, this._path);
  143. await this._platform.fs().promises.writeFile(this._path, buffer);
  144. this._path = void 0;
  145. }
  146. return buffer;
  147. }
  148. async [Symbol.asyncDispose]() {
  149. await this.close();
  150. }
  151. async close(options = {}) {
  152. this._closeReason = options.reason;
  153. try {
  154. if (this._shouldCloseConnectionOnClose)
  155. this._connection.close();
  156. else
  157. await this._channel.close(options);
  158. await this._closedPromise;
  159. } catch (e) {
  160. if ((0, import_errors.isTargetClosedError)(e))
  161. return;
  162. throw e;
  163. }
  164. }
  165. _didClose() {
  166. this._isConnected = false;
  167. this.emit(import_events.Events.Browser.Disconnected, this);
  168. }
  169. }
  170. // Annotate the CommonJS export names for ESM import in node:
  171. 0 && (module.exports = {
  172. Browser
  173. });