/** * Copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ElectronChannel, ElectronInitializer, ElectronApplicationChannel, ElectronApplicationInitializer, ElectronLaunchParams, ElectronLaunchOptions } from '../../protocol/channels'; import { BrowserContext } from './browserContext'; import { ChannelOwner } from './channelOwner'; import { Page } from './page'; import { serializeArgument, FuncOn, parseResult, SmartHandle, JSHandle } from './jsHandle'; import { TimeoutSettings } from '../../timeoutSettings'; import { Waiter } from './waiter'; import { Events } from './events'; import { WaitForEventOptions, Env, LoggerSink } from './types'; import { envObjectToArray } from './clientHelper'; type ElectronOptions = Omit & { env?: Env, logger?: LoggerSink, }; export class Electron extends ChannelOwner { static from(electron: ElectronChannel): Electron { return (electron as any)._object; } constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronInitializer) { super(parent, type, guid, initializer); } async launch(executablePath: string, options: ElectronOptions = {}): Promise { const logger = options.logger; options = { ...options, logger: undefined }; return this._wrapApiCall('electron.launch', async () => { const params: ElectronLaunchParams = { ...options, env: options.env ? envObjectToArray(options.env) : undefined, executablePath, }; return ElectronApplication.from((await this._channel.launch(params)).electronApplication); }, logger); } } export class ElectronApplication extends ChannelOwner { private _context?: BrowserContext; private _windows = new Set(); private _timeoutSettings = new TimeoutSettings(); static from(electronApplication: ElectronApplicationChannel): ElectronApplication { return (electronApplication as any)._object; } constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) { super(parent, type, guid, initializer); this._channel.on('context', ({ context }) => this._context = BrowserContext.from(context)); this._channel.on('window', ({ page, browserWindow }) => { const window = Page.from(page); (window as any).browserWindow = JSHandle.from(browserWindow); this._windows.add(window); this.emit(Events.ElectronApplication.Window, window); window.once(Events.Page.Close, () => this._windows.delete(window)); }); this._channel.on('close', () => this.emit(Events.ElectronApplication.Close)); } windows(): Page[] { return [...this._windows]; } async firstWindow(): Promise { if (this._windows.size) return this._windows.values().next().value; return this.waitForEvent('window'); } async newBrowserWindow(options: any): Promise { const result = await this._channel.newBrowserWindow({ arg: serializeArgument(options) }); return Page.from(result.page); } context(): BrowserContext { return this._context!; } async close() { await this._channel.close(); } async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise { const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate); const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate; const waiter = new Waiter(); waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`); if (event !== Events.ElectronApplication.Close) waiter.rejectOnEvent(this, Events.ElectronApplication.Close, new Error('Electron application closed')); const result = await waiter.waitForEvent(this, event, predicate as any); waiter.dispose(); return result; } async evaluate(pageFunction: FuncOn, arg: Arg): Promise; async evaluate(pageFunction: FuncOn, arg?: any): Promise; async evaluate(pageFunction: FuncOn, arg: Arg): Promise { const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }); return parseResult(result.value); } async evaluateHandle(pageFunction: FuncOn, arg: Arg): Promise>; async evaluateHandle(pageFunction: FuncOn, arg?: any): Promise>; async evaluateHandle(pageFunction: FuncOn, arg: Arg): Promise> { const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) }); return JSHandle.from(result.handle) as SmartHandle; } }