2020-07-13 21:46:59 -07:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-22 15:13:51 -07:00
|
|
|
import { ElectronChannel, ElectronInitializer, ElectronApplicationChannel, ElectronApplicationInitializer, ElectronLaunchParams, ElectronLaunchOptions } from '../protocol/channels';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { BrowserContext } from './browserContext';
|
|
|
|
import { ChannelOwner } from './channelOwner';
|
|
|
|
import { Page } from './page';
|
|
|
|
import { serializeArgument, FuncOn, parseResult, SmartHandle, JSHandle } from './jsHandle';
|
2020-08-22 15:13:51 -07:00
|
|
|
import { TimeoutSettings } from '../utils/timeoutSettings';
|
2020-07-13 21:46:59 -07:00
|
|
|
import { Waiter } from './waiter';
|
2020-07-29 17:26:59 -07:00
|
|
|
import { Events } from './events';
|
|
|
|
import { WaitForEventOptions, Env, LoggerSink } from './types';
|
2020-08-18 09:37:40 -07:00
|
|
|
import { envObjectToArray } from './clientHelper';
|
2020-07-13 21:46:59 -07:00
|
|
|
|
2020-07-31 17:00:36 -07:00
|
|
|
type ElectronOptions = Omit<ElectronLaunchOptions, 'env'> & {
|
|
|
|
env?: Env,
|
|
|
|
logger?: LoggerSink,
|
|
|
|
};
|
|
|
|
|
2020-07-13 21:46:59 -07:00
|
|
|
export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer> {
|
|
|
|
static from(electron: ElectronChannel): Electron {
|
|
|
|
return (electron as any)._object;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronInitializer) {
|
2020-07-27 10:21:39 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
2020-07-31 17:00:36 -07:00
|
|
|
async launch(executablePath: string, options: ElectronOptions = {}): Promise<ElectronApplication> {
|
2020-07-16 14:32:21 -07:00
|
|
|
const logger = options.logger;
|
|
|
|
options = { ...options, logger: undefined };
|
|
|
|
return this._wrapApiCall('electron.launch', async () => {
|
2020-07-20 17:38:06 -07:00
|
|
|
const params: ElectronLaunchParams = {
|
|
|
|
...options,
|
|
|
|
env: options.env ? envObjectToArray(options.env) : undefined,
|
|
|
|
executablePath,
|
|
|
|
};
|
|
|
|
return ElectronApplication.from((await this._channel.launch(params)).electronApplication);
|
2020-07-16 14:32:21 -07:00
|
|
|
}, logger);
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel, ElectronApplicationInitializer> {
|
2020-07-24 16:22:20 -07:00
|
|
|
private _context?: BrowserContext;
|
2020-07-13 21:46:59 -07:00
|
|
|
private _windows = new Set<Page>();
|
|
|
|
private _timeoutSettings = new TimeoutSettings();
|
|
|
|
|
|
|
|
static from(electronApplication: ElectronApplicationChannel): ElectronApplication {
|
|
|
|
return (electronApplication as any)._object;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) {
|
2020-07-27 10:21:39 -07:00
|
|
|
super(parent, type, guid, initializer);
|
2020-07-24 16:22:20 -07:00
|
|
|
this._channel.on('context', ({ context }) => this._context = BrowserContext.from(context));
|
2020-07-15 18:48:19 -07:00
|
|
|
this._channel.on('window', ({ page, browserWindow }) => {
|
2020-07-14 18:26:50 -07:00
|
|
|
const window = Page.from(page);
|
2020-07-15 18:48:19 -07:00
|
|
|
(window as any).browserWindow = JSHandle.from(browserWindow);
|
2020-07-14 18:26:50 -07:00
|
|
|
this._windows.add(window);
|
2020-07-29 17:26:59 -07:00
|
|
|
this.emit(Events.ElectronApplication.Window, window);
|
2020-07-15 18:48:19 -07:00
|
|
|
window.once(Events.Page.Close, () => this._windows.delete(window));
|
2020-07-13 21:46:59 -07:00
|
|
|
});
|
2020-07-29 17:26:59 -07:00
|
|
|
this._channel.on('close', () => this.emit(Events.ElectronApplication.Close));
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
windows(): Page[] {
|
|
|
|
return [...this._windows];
|
|
|
|
}
|
|
|
|
|
|
|
|
async firstWindow(): Promise<Page> {
|
|
|
|
if (this._windows.size)
|
|
|
|
return this._windows.values().next().value;
|
|
|
|
return this.waitForEvent('window');
|
|
|
|
}
|
|
|
|
|
|
|
|
async newBrowserWindow(options: any): Promise<Page> {
|
2020-07-14 18:26:50 -07:00
|
|
|
const result = await this._channel.newBrowserWindow({ arg: serializeArgument(options) });
|
|
|
|
return Page.from(result.page);
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
context(): BrowserContext {
|
2020-07-24 16:22:20 -07:00
|
|
|
return this._context!;
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
await this._channel.close();
|
|
|
|
}
|
|
|
|
|
2020-07-29 17:26:59 -07:00
|
|
|
async waitForEvent(event: string, optionsOrPredicate: WaitForEventOptions = {}): Promise<any> {
|
2020-07-16 22:38:52 -07:00
|
|
|
const timeout = this._timeoutSettings.timeout(typeof optionsOrPredicate === 'function' ? {} : optionsOrPredicate);
|
|
|
|
const predicate = typeof optionsOrPredicate === 'function' ? optionsOrPredicate : optionsOrPredicate.predicate;
|
2020-07-13 21:46:59 -07:00
|
|
|
const waiter = new Waiter();
|
2020-07-21 23:48:21 -07:00
|
|
|
waiter.rejectOnTimeout(timeout, `Timeout while waiting for event "${event}"`);
|
2020-07-29 17:26:59 -07:00
|
|
|
if (event !== Events.ElectronApplication.Close)
|
|
|
|
waiter.rejectOnEvent(this, Events.ElectronApplication.Close, new Error('Electron application closed'));
|
2020-07-13 21:46:59 -07:00
|
|
|
const result = await waiter.waitForEvent(this, event, predicate as any);
|
|
|
|
waiter.dispose();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async evaluate<R, Arg>(pageFunction: FuncOn<any, Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async evaluate<R>(pageFunction: FuncOn<any, void, R>, arg?: any): Promise<R>;
|
|
|
|
async evaluate<R, Arg>(pageFunction: FuncOn<any, Arg, R>, arg: Arg): Promise<R> {
|
2020-07-14 18:26:50 -07:00
|
|
|
const result = await this._channel.evaluateExpression({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
|
|
|
return parseResult(result.value);
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: FuncOn<any, Arg, R>, arg: Arg): Promise<SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R>(pageFunction: FuncOn<any, void, R>, arg?: any): Promise<SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: FuncOn<any, Arg, R>, arg: Arg): Promise<SmartHandle<R>> {
|
2020-07-14 18:26:50 -07:00
|
|
|
const result = await this._channel.evaluateExpressionHandle({ expression: String(pageFunction), isFunction: typeof pageFunction === 'function', arg: serializeArgument(arg) });
|
|
|
|
return JSHandle.from(result.handle) as SmartHandle<R>;
|
2020-07-13 21:46:59 -07:00
|
|
|
}
|
|
|
|
}
|