electron.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 electron_exports = {};
  20. __export(electron_exports, {
  21. Electron: () => Electron,
  22. ElectronApplication: () => ElectronApplication
  23. });
  24. module.exports = __toCommonJS(electron_exports);
  25. var import_browserContext = require("./browserContext");
  26. var import_channelOwner = require("./channelOwner");
  27. var import_clientHelper = require("./clientHelper");
  28. var import_consoleMessage = require("./consoleMessage");
  29. var import_errors = require("./errors");
  30. var import_events = require("./events");
  31. var import_jsHandle = require("./jsHandle");
  32. var import_waiter = require("./waiter");
  33. var import_timeoutSettings = require("./timeoutSettings");
  34. class Electron extends import_channelOwner.ChannelOwner {
  35. static from(electron) {
  36. return electron._object;
  37. }
  38. constructor(parent, type, guid, initializer) {
  39. super(parent, type, guid, initializer);
  40. }
  41. async launch(options = {}) {
  42. options = this._playwright.selectors._withSelectorOptions(options);
  43. const params = {
  44. ...await (0, import_browserContext.prepareBrowserContextParams)(this._platform, options),
  45. env: (0, import_clientHelper.envObjectToArray)(options.env ? options.env : this._platform.env),
  46. tracesDir: options.tracesDir,
  47. timeout: new import_timeoutSettings.TimeoutSettings(this._platform).launchTimeout(options)
  48. };
  49. const app = ElectronApplication.from((await this._channel.launch(params)).electronApplication);
  50. this._playwright.selectors._contextsForSelectors.add(app._context);
  51. app.once(import_events.Events.ElectronApplication.Close, () => this._playwright.selectors._contextsForSelectors.delete(app._context));
  52. await app._context._initializeHarFromOptions(options.recordHar);
  53. app._context.tracing._tracesDir = options.tracesDir;
  54. return app;
  55. }
  56. }
  57. class ElectronApplication extends import_channelOwner.ChannelOwner {
  58. constructor(parent, type, guid, initializer) {
  59. super(parent, type, guid, initializer);
  60. this._windows = /* @__PURE__ */ new Set();
  61. this._timeoutSettings = new import_timeoutSettings.TimeoutSettings(this._platform);
  62. this._context = import_browserContext.BrowserContext.from(initializer.context);
  63. for (const page of this._context._pages)
  64. this._onPage(page);
  65. this._context.on(import_events.Events.BrowserContext.Page, (page) => this._onPage(page));
  66. this._channel.on("close", () => {
  67. this.emit(import_events.Events.ElectronApplication.Close);
  68. });
  69. this._channel.on("console", (event) => this.emit(import_events.Events.ElectronApplication.Console, new import_consoleMessage.ConsoleMessage(this._platform, event, null, null)));
  70. this._setEventToSubscriptionMapping(/* @__PURE__ */ new Map([
  71. [import_events.Events.ElectronApplication.Console, "console"]
  72. ]));
  73. }
  74. static from(electronApplication) {
  75. return electronApplication._object;
  76. }
  77. process() {
  78. return this._connection.toImpl?.(this)?.process();
  79. }
  80. _onPage(page) {
  81. this._windows.add(page);
  82. this.emit(import_events.Events.ElectronApplication.Window, page);
  83. page.once(import_events.Events.Page.Close, () => this._windows.delete(page));
  84. }
  85. windows() {
  86. return [...this._windows];
  87. }
  88. async firstWindow(options) {
  89. if (this._windows.size)
  90. return this._windows.values().next().value;
  91. return await this.waitForEvent("window", options);
  92. }
  93. context() {
  94. return this._context;
  95. }
  96. async [Symbol.asyncDispose]() {
  97. await this.close();
  98. }
  99. async close() {
  100. try {
  101. await this._context.close();
  102. } catch (e) {
  103. if ((0, import_errors.isTargetClosedError)(e))
  104. return;
  105. throw e;
  106. }
  107. }
  108. async waitForEvent(event, optionsOrPredicate = {}) {
  109. return await this._wrapApiCall(async () => {
  110. const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === "function" ? {} : optionsOrPredicate);
  111. const predicate = typeof optionsOrPredicate === "function" ? optionsOrPredicate : optionsOrPredicate.predicate;
  112. const waiter = import_waiter.Waiter.createForEvent(this, event);
  113. waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`);
  114. if (event !== import_events.Events.ElectronApplication.Close)
  115. waiter.rejectOnEvent(this, import_events.Events.ElectronApplication.Close, () => new import_errors.TargetClosedError());
  116. const result = await waiter.waitForEvent(this, event, predicate);
  117. waiter.dispose();
  118. return result;
  119. });
  120. }
  121. async browserWindow(page) {
  122. const result = await this._channel.browserWindow({ page: page._channel });
  123. return import_jsHandle.JSHandle.from(result.handle);
  124. }
  125. async evaluate(pageFunction, arg) {
  126. const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  127. return (0, import_jsHandle.parseResult)(result.value);
  128. }
  129. async evaluateHandle(pageFunction, arg) {
  130. const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === "function", arg: (0, import_jsHandle.serializeArgument)(arg) });
  131. return import_jsHandle.JSHandle.from(result.handle);
  132. }
  133. }
  134. // Annotate the CommonJS export names for ESM import in node:
  135. 0 && (module.exports = {
  136. Electron,
  137. ElectronApplication
  138. });