electron.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ElectronApplication = exports.Electron = void 0;
  6. var _timeoutSettings = require("../common/timeoutSettings");
  7. var _browserContext = require("./browserContext");
  8. var _channelOwner = require("./channelOwner");
  9. var _clientHelper = require("./clientHelper");
  10. var _events = require("./events");
  11. var _jsHandle = require("./jsHandle");
  12. var _waiter = require("./waiter");
  13. var _errors = require("./errors");
  14. let _Symbol$asyncDispose;
  15. /**
  16. * Copyright (c) Microsoft Corporation.
  17. *
  18. * Licensed under the Apache License, Version 2.0 (the "License");
  19. * you may not use this file except in compliance with the License.
  20. * You may obtain a copy of the License at
  21. *
  22. * http://www.apache.org/licenses/LICENSE-2.0
  23. *
  24. * Unless required by applicable law or agreed to in writing, software
  25. * distributed under the License is distributed on an "AS IS" BASIS,
  26. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27. * See the License for the specific language governing permissions and
  28. * limitations under the License.
  29. */
  30. class Electron extends _channelOwner.ChannelOwner {
  31. static from(electron) {
  32. return electron._object;
  33. }
  34. constructor(parent, type, guid, initializer) {
  35. super(parent, type, guid, initializer);
  36. }
  37. async launch(options = {}) {
  38. const params = {
  39. ...(await (0, _browserContext.prepareBrowserContextParams)(options)),
  40. env: (0, _clientHelper.envObjectToArray)(options.env ? options.env : process.env),
  41. tracesDir: options.tracesDir
  42. };
  43. const app = ElectronApplication.from((await this._channel.launch(params)).electronApplication);
  44. app._context._options = params;
  45. return app;
  46. }
  47. }
  48. exports.Electron = Electron;
  49. _Symbol$asyncDispose = Symbol.asyncDispose;
  50. class ElectronApplication extends _channelOwner.ChannelOwner {
  51. static from(electronApplication) {
  52. return electronApplication._object;
  53. }
  54. constructor(parent, type, guid, initializer) {
  55. super(parent, type, guid, initializer);
  56. this._context = void 0;
  57. this._windows = new Set();
  58. this._timeoutSettings = new _timeoutSettings.TimeoutSettings();
  59. this._isClosed = false;
  60. this._context = _browserContext.BrowserContext.from(initializer.context);
  61. for (const page of this._context._pages) this._onPage(page);
  62. this._context.on(_events.Events.BrowserContext.Page, page => this._onPage(page));
  63. this._channel.on('close', () => {
  64. this._isClosed = true;
  65. this.emit(_events.Events.ElectronApplication.Close);
  66. });
  67. }
  68. process() {
  69. return this._toImpl().process();
  70. }
  71. _onPage(page) {
  72. this._windows.add(page);
  73. this.emit(_events.Events.ElectronApplication.Window, page);
  74. page.once(_events.Events.Page.Close, () => this._windows.delete(page));
  75. }
  76. windows() {
  77. // TODO: add ElectronPage class inheriting from Page.
  78. return [...this._windows];
  79. }
  80. async firstWindow(options) {
  81. if (this._windows.size) return this._windows.values().next().value;
  82. return await this.waitForEvent('window', options);
  83. }
  84. context() {
  85. return this._context;
  86. }
  87. async [_Symbol$asyncDispose]() {
  88. await this.close();
  89. }
  90. async close() {
  91. if (this._isClosed) return;
  92. await this._channel.close().catch(() => {});
  93. }
  94. async waitForEvent(event, optionsOrPredicate = {}) {
  95. return await this._wrapApiCall(async () => {
  96. const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
  97. const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
  98. const waiter = _waiter.Waiter.createForEvent(this, event);
  99. waiter.rejectOnTimeout(timeout, `Timeout ${timeout}ms exceeded while waiting for event "${event}"`);
  100. if (event !== _events.Events.ElectronApplication.Close) waiter.rejectOnEvent(this, _events.Events.ElectronApplication.Close, () => new _errors.TargetClosedError());
  101. const result = await waiter.waitForEvent(this, event, predicate);
  102. waiter.dispose();
  103. return result;
  104. });
  105. }
  106. async browserWindow(page) {
  107. const result = await this._channel.browserWindow({
  108. page: page._channel
  109. });
  110. return _jsHandle.JSHandle.from(result.handle);
  111. }
  112. async evaluate(pageFunction, arg) {
  113. const result = await this._channel.evaluateExpression({
  114. expression: String(pageFunction),
  115. isFunction: typeof pageFunction === 'function',
  116. arg: (0, _jsHandle.serializeArgument)(arg)
  117. });
  118. return (0, _jsHandle.parseResult)(result.value);
  119. }
  120. async evaluateHandle(pageFunction, arg) {
  121. const result = await this._channel.evaluateExpressionHandle({
  122. expression: String(pageFunction),
  123. isFunction: typeof pageFunction === 'function',
  124. arg: (0, _jsHandle.serializeArgument)(arg)
  125. });
  126. return _jsHandle.JSHandle.from(result.handle);
  127. }
  128. }
  129. exports.ElectronApplication = ElectronApplication;