Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(electron): deliver promised _nodeElectronHandle (#6348)

Gnome Bard018f3146a83c
diff --git a/src/dispatchers/electronDispatcher.ts b/src/dispatchers/electronDispatcher.ts
index 010601c6cc..72e8990b5e 100644
--- a/src/dispatchers/electronDispatcher.ts
+++ b/src/dispatchers/electronDispatcher.ts
@@ -50,12 +50,12 @@ export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplicatio
}
async evaluateExpression(params: channels.ElectronApplicationEvaluateExpressionParams): Promise<channels.ElectronApplicationEvaluateExpressionResult> {
- const handle = this._object._nodeElectronHandle!;
+ const handle = await this._object._nodeElectronHandlePromised;
return { value: serializeResult(await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, true /* returnByValue */, parseArgument(params.arg))) };
}
async evaluateExpressionHandle(params: channels.ElectronApplicationEvaluateExpressionHandleParams): Promise<channels.ElectronApplicationEvaluateExpressionHandleResult> {
- const handle = this._object._nodeElectronHandle!;
+ const handle = await this._object._nodeElectronHandlePromised;
const result = await handle.evaluateExpressionAndWaitForSignals(params.expression, params.isFunction, false /* returnByValue */, parseArgument(params.arg));
return { handle: ElementHandleDispatcher.fromJSHandle(this._scope, result) };
}
diff --git a/src/server/electron/electron.ts b/src/server/electron/electron.ts
index 5a609f0c84..42cdbf41db 100644
--- a/src/server/electron/electron.ts
+++ b/src/server/electron/electron.ts
@@ -58,7 +58,8 @@ export class ElectronApplication extends SdkObject {
private _nodeConnection: CRConnection;
private _nodeSession: CRSession;
private _nodeExecutionContext: js.ExecutionContext | undefined;
- _nodeElectronHandle: js.JSHandle<any> | undefined;
+ _nodeElectronHandlePromised: Promise<js.JSHandle<any>>;
+ private _resolveNodeElectronHandle!: (handle: js.JSHandle<any>) => void;
private _windows = new Set<ElectronPage>();
private _lastWindowId = 0;
readonly _timeoutSettings = new TimeoutSettings();
@@ -73,6 +74,7 @@ export class ElectronApplication extends SdkObject {
this._browserContext.on(BrowserContext.Events.Page, event => this._onPage(event));
this._nodeConnection = nodeConnection;
this._nodeSession = nodeConnection.rootSession;
+ this._nodeElectronHandlePromised = new Promise(resolve => this._resolveNodeElectronHandle = resolve);
}
private async _onPage(page: ElectronPage) {
@@ -87,7 +89,7 @@ export class ElectronApplication extends SdkObject {
this._windows.add(page);
// Below is async.
- const handle = await this._nodeElectronHandle!.evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
+ const handle = await (await this._nodeElectronHandlePromised).evaluateHandle(({ BrowserWindow }, windowId) => BrowserWindow.fromId(windowId), windowId).catch(e => {});
if (!handle)
return;
page.browserWindow = handle;
@@ -103,7 +105,7 @@ export class ElectronApplication extends SdkObject {
async close() {
const progressController = new ProgressController(internalCallMetadata(), this);
const closed = progressController.run(progress => helper.waitForEvent(progress, this, ElectronApplication.Events.Close).promise, this._timeoutSettings.timeout({}));
- await this._nodeElectronHandle!.evaluate(({ app }) => app.quit());
+ await (await this._nodeElectronHandlePromised).evaluate(({ app }) => app.quit());
this._nodeConnection.close();
await closed;
}
@@ -114,7 +116,8 @@ export class ElectronApplication extends SdkObject {
this._nodeExecutionContext = new js.ExecutionContext(this, new CRExecutionContext(this._nodeSession, event.context));
});
await this._nodeSession.send('Runtime.enable', {}).catch(e => {});
- this._nodeElectronHandle = await js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`);
+ js.evaluate(this._nodeExecutionContext!, false /* returnByValue */, `process.mainModule.require('electron')`)
+ .then(this._resolveNodeElectronHandle);
}
}