2020-05-11 18:00:33 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as path from 'path';
|
|
|
|
import { CRBrowser, CRBrowserContext } from '../chromium/crBrowser';
|
|
|
|
import { CRConnection, CRSession } from '../chromium/crConnection';
|
|
|
|
import { CRExecutionContext } from '../chromium/crExecutionContext';
|
|
|
|
import { Events } from '../events';
|
|
|
|
import * as js from '../javascript';
|
2020-06-16 17:11:19 -07:00
|
|
|
import { Loggers, Logger } from '../logger';
|
2020-05-11 18:00:33 -07:00
|
|
|
import { Page } from '../page';
|
|
|
|
import { TimeoutSettings } from '../timeoutSettings';
|
|
|
|
import { WebSocketTransport } from '../transport';
|
|
|
|
import * as types from '../types';
|
|
|
|
import { BrowserServer } from './browserServer';
|
|
|
|
import { launchProcess, waitForLine } from './processLauncher';
|
|
|
|
import { BrowserContext } from '../browserContext';
|
2020-05-13 20:51:53 -07:00
|
|
|
import type {BrowserWindow} from 'electron';
|
2020-06-10 15:12:50 -07:00
|
|
|
import { runAbortableTask, ProgressController } from '../progress';
|
|
|
|
import { EventEmitter } from 'events';
|
|
|
|
import { helper } from '../helper';
|
2020-06-23 14:51:06 -07:00
|
|
|
import { LoggerSink } from '../loggerSink';
|
2020-05-11 18:00:33 -07:00
|
|
|
|
2020-07-20 17:38:06 -07:00
|
|
|
export type ElectronLaunchOptionsBase = {
|
2020-05-11 18:00:33 -07:00
|
|
|
args?: string[],
|
|
|
|
cwd?: string,
|
2020-07-20 17:38:06 -07:00
|
|
|
env?: types.Env,
|
2020-05-11 18:00:33 -07:00
|
|
|
handleSIGINT?: boolean,
|
|
|
|
handleSIGTERM?: boolean,
|
|
|
|
handleSIGHUP?: boolean,
|
|
|
|
timeout?: number,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ElectronEvents = {
|
|
|
|
ElectronApplication: {
|
|
|
|
Close: 'close',
|
|
|
|
Window: 'window',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-15 18:48:19 -07:00
|
|
|
export interface ElectronPage extends Page {
|
2020-05-13 20:51:53 -07:00
|
|
|
browserWindow: js.JSHandle<BrowserWindow>;
|
|
|
|
_browserWindowId: number;
|
|
|
|
}
|
|
|
|
|
2020-06-10 15:12:50 -07:00
|
|
|
export class ElectronApplication extends EventEmitter {
|
2020-06-16 17:11:19 -07:00
|
|
|
private _apiLogger: Logger;
|
2020-05-11 18:00:33 -07:00
|
|
|
private _browserContext: CRBrowserContext;
|
|
|
|
private _nodeConnection: CRConnection;
|
|
|
|
private _nodeSession: CRSession;
|
|
|
|
private _nodeExecutionContext: js.ExecutionContext | undefined;
|
2020-07-13 21:46:59 -07:00
|
|
|
_nodeElectronHandle: js.JSHandle<any> | undefined;
|
2020-05-13 20:51:53 -07:00
|
|
|
private _windows = new Set<ElectronPage>();
|
2020-05-11 18:00:33 -07:00
|
|
|
private _lastWindowId = 0;
|
|
|
|
readonly _timeoutSettings = new TimeoutSettings();
|
|
|
|
|
2020-06-16 17:11:19 -07:00
|
|
|
constructor(logger: Loggers, browser: CRBrowser, nodeConnection: CRConnection) {
|
2020-05-11 18:00:33 -07:00
|
|
|
super();
|
2020-06-16 17:11:19 -07:00
|
|
|
this._apiLogger = logger.api;
|
2020-05-20 16:30:04 -07:00
|
|
|
this._browserContext = browser._defaultContext as CRBrowserContext;
|
2020-05-11 18:00:33 -07:00
|
|
|
this._browserContext.on(Events.BrowserContext.Close, () => this.emit(ElectronEvents.ElectronApplication.Close));
|
|
|
|
this._browserContext.on(Events.BrowserContext.Page, event => this._onPage(event));
|
|
|
|
this._nodeConnection = nodeConnection;
|
|
|
|
this._nodeSession = nodeConnection.rootSession;
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:51:53 -07:00
|
|
|
private async _onPage(page: ElectronPage) {
|
2020-05-11 18:00:33 -07:00
|
|
|
// Needs to be sync.
|
|
|
|
const windowId = ++this._lastWindowId;
|
|
|
|
// Can be async.
|
2020-05-12 15:28:37 -07:00
|
|
|
const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
|
|
|
|
if (!handle)
|
|
|
|
return;
|
2020-05-13 20:51:53 -07:00
|
|
|
page.browserWindow = handle;
|
|
|
|
page._browserWindowId = windowId;
|
2020-05-11 18:00:33 -07:00
|
|
|
page.on(Events.Page.Close, () => {
|
2020-05-13 20:51:53 -07:00
|
|
|
page.browserWindow.dispose();
|
2020-05-11 18:00:33 -07:00
|
|
|
this._windows.delete(page);
|
|
|
|
});
|
|
|
|
this._windows.add(page);
|
2020-05-12 15:28:37 -07:00
|
|
|
await page.waitForLoadState('domcontentloaded').catch(e => {}); // can happen after detach
|
2020-05-11 18:00:33 -07:00
|
|
|
this.emit(ElectronEvents.ElectronApplication.Window, page);
|
|
|
|
}
|
|
|
|
|
|
|
|
windows(): Page[] {
|
|
|
|
return [...this._windows];
|
|
|
|
}
|
|
|
|
|
2020-05-12 08:43:41 -07:00
|
|
|
async firstWindow(): Promise<Page> {
|
|
|
|
if (this._windows.size)
|
|
|
|
return this._windows.values().next().value;
|
|
|
|
return this.waitForEvent('window');
|
|
|
|
}
|
|
|
|
|
2020-05-11 18:00:33 -07:00
|
|
|
async newBrowserWindow(options: any): Promise<Page> {
|
|
|
|
const windowId = await this.evaluate(async ({ BrowserWindow }, options) => {
|
|
|
|
const win = new BrowserWindow(options);
|
|
|
|
win.loadURL('about:blank');
|
|
|
|
return win.id;
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
for (const page of this._windows) {
|
2020-05-13 20:51:53 -07:00
|
|
|
if (page._browserWindowId === windowId)
|
2020-05-11 18:00:33 -07:00
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2020-05-13 20:51:53 -07:00
|
|
|
return await this.waitForEvent(ElectronEvents.ElectronApplication.Window, (page: ElectronPage) => page._browserWindowId === windowId);
|
2020-05-11 18:00:33 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
context(): BrowserContext {
|
|
|
|
return this._browserContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
async close() {
|
|
|
|
await this.evaluate(({ app }) => app.quit());
|
|
|
|
this._nodeConnection.close();
|
|
|
|
}
|
|
|
|
|
2020-06-10 15:12:50 -07:00
|
|
|
async waitForEvent(event: string, optionsOrPredicate: types.WaitForEventOptions = {}): Promise<any> {
|
|
|
|
const options = typeof optionsOrPredicate === 'function' ? { predicate: optionsOrPredicate } : optionsOrPredicate;
|
2020-06-16 17:11:19 -07:00
|
|
|
const progressController = new ProgressController(this._apiLogger, this._timeoutSettings.timeout(options), 'electron.waitForEvent');
|
2020-06-10 15:12:50 -07:00
|
|
|
if (event !== ElectronEvents.ElectronApplication.Close)
|
|
|
|
this._browserContext._closePromise.then(error => progressController.abort(error));
|
2020-07-07 15:22:05 -07:00
|
|
|
return progressController.run(progress => helper.waitForEvent(progress, this, event, options.predicate).promise);
|
2020-06-10 15:12:50 -07:00
|
|
|
}
|
|
|
|
|
2020-05-11 18:00:33 -07:00
|
|
|
async _init() {
|
|
|
|
this._nodeSession.once('Runtime.executionContextCreated', event => {
|
2020-06-09 16:11:17 -07:00
|
|
|
this._nodeExecutionContext = new js.ExecutionContext(new CRExecutionContext(this._nodeSession, event.context));
|
2020-05-11 18:00:33 -07:00
|
|
|
});
|
|
|
|
await this._nodeSession.send('Runtime.enable', {}).catch(e => {});
|
2020-06-03 17:50:16 -07:00
|
|
|
this._nodeElectronHandle = await js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, () => {
|
2020-05-11 18:00:33 -07:00
|
|
|
// Resolving the race between the debugger and the boot-time script.
|
|
|
|
if ((global as any)._playwrightRun)
|
|
|
|
return (global as any)._playwrightRun();
|
|
|
|
return new Promise(f => (global as any)._playwrightRunCallback = f);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluate<R, Arg>(pageFunction: js.FuncOn<any, Arg, R>, arg: Arg): Promise<R>;
|
|
|
|
async evaluate<R>(pageFunction: js.FuncOn<any, void, R>, arg?: any): Promise<R>;
|
|
|
|
async evaluate<R, Arg>(pageFunction: js.FuncOn<any, Arg, R>, arg: Arg): Promise<R> {
|
2020-05-11 18:00:33 -07:00
|
|
|
return this._nodeElectronHandle!.evaluate(pageFunction, arg);
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async evaluateHandle<R, Arg>(pageFunction: js.FuncOn<any, Arg, R>, arg: Arg): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R>(pageFunction: js.FuncOn<any, void, R>, arg?: any): Promise<js.SmartHandle<R>>;
|
|
|
|
async evaluateHandle<R, Arg>(pageFunction: js.FuncOn<any, Arg, R>, arg: Arg): Promise<js.SmartHandle<R>> {
|
2020-05-11 18:00:33 -07:00
|
|
|
return this._nodeElectronHandle!.evaluateHandle(pageFunction, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Electron {
|
2020-07-20 17:38:06 -07:00
|
|
|
async launch(executablePath: string, options: ElectronLaunchOptionsBase & { logger?: LoggerSink } = {}): Promise<ElectronApplication> {
|
2020-05-11 18:00:33 -07:00
|
|
|
const {
|
|
|
|
args = [],
|
|
|
|
env = process.env,
|
|
|
|
handleSIGINT = true,
|
|
|
|
handleSIGTERM = true,
|
|
|
|
handleSIGHUP = true,
|
|
|
|
} = options;
|
2020-06-16 17:11:19 -07:00
|
|
|
const loggers = new Loggers(options.logger);
|
2020-06-04 16:43:48 -07:00
|
|
|
return runAbortableTask(async progress => {
|
2020-05-29 14:39:34 -07:00
|
|
|
let app: ElectronApplication | undefined = undefined;
|
|
|
|
const electronArguments = ['--inspect=0', '--remote-debugging-port=0', '--require', path.join(__dirname, 'electronLoader.js'), ...args];
|
|
|
|
const { launchedProcess, gracefullyClose, kill } = await launchProcess({
|
|
|
|
executablePath,
|
|
|
|
args: electronArguments,
|
|
|
|
env,
|
|
|
|
handleSIGINT,
|
|
|
|
handleSIGTERM,
|
|
|
|
handleSIGHUP,
|
|
|
|
progress,
|
|
|
|
pipe: true,
|
|
|
|
cwd: options.cwd,
|
|
|
|
tempDirectories: [],
|
|
|
|
attemptToGracefullyClose: () => app!.close(),
|
|
|
|
onExit: (exitCode, signal) => {
|
|
|
|
if (app)
|
|
|
|
app.emit(ElectronEvents.ElectronApplication.Close, exitCode, signal);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const nodeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^Debugger listening on (ws:\/\/.*)$/);
|
|
|
|
const nodeTransport = await WebSocketTransport.connect(progress, nodeMatch[1]);
|
2020-06-16 17:11:19 -07:00
|
|
|
const nodeConnection = new CRConnection(nodeTransport, loggers);
|
2020-05-29 14:39:34 -07:00
|
|
|
|
|
|
|
const chromeMatch = await waitForLine(progress, launchedProcess, launchedProcess.stderr, /^DevTools listening on (ws:\/\/.*)$/);
|
|
|
|
const chromeTransport = await WebSocketTransport.connect(progress, chromeMatch[1]);
|
|
|
|
const browserServer = new BrowserServer(launchedProcess, gracefullyClose, kill);
|
2020-07-08 21:36:03 -07:00
|
|
|
const browser = await CRBrowser.connect(chromeTransport, { name: 'electron', headful: true, loggers, persistent: { viewport: null }, ownedServer: browserServer });
|
2020-06-16 17:11:19 -07:00
|
|
|
app = new ElectronApplication(loggers, browser, nodeConnection);
|
2020-05-29 14:39:34 -07:00
|
|
|
await app._init();
|
|
|
|
return app;
|
2020-06-16 17:11:19 -07:00
|
|
|
}, loggers.browser, TimeoutSettings.timeout(options), 'electron.launch');
|
2020-05-11 18:00:33 -07:00
|
|
|
}
|
|
|
|
}
|