Skip to content

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

feat(rpc): make ElectronApplication a scope (#3159)

Dmitry Gozmand9890f1102d9
diff --git a/src/rpc/channels.ts b/src/rpc/channels.ts
index 28765b291c..6fc4d7917f 100644
--- a/src/rpc/channels.ts
+++ b/src/rpc/channels.ts
@@ -1617,10 +1617,9 @@ export type ElectronLaunchResult = {
};
// ----------- ElectronApplication -----------
-export type ElectronApplicationInitializer = {
- context: BrowserContextChannel,
-};
+export type ElectronApplicationInitializer = {};
export interface ElectronApplicationChannel extends Channel {
+ on(event: 'context', callback: (params: ElectronApplicationContextEvent) => void): this;
on(event: 'close', callback: (params: ElectronApplicationCloseEvent) => void): this;
on(event: 'window', callback: (params: ElectronApplicationWindowEvent) => void): this;
newBrowserWindow(params: ElectronApplicationNewBrowserWindowParams): Promise<ElectronApplicationNewBrowserWindowResult>;
@@ -1628,6 +1627,9 @@ export interface ElectronApplicationChannel extends Channel {
evaluateExpressionHandle(params: ElectronApplicationEvaluateExpressionHandleParams): Promise<ElectronApplicationEvaluateExpressionHandleResult>;
close(params?: ElectronApplicationCloseParams): Promise<ElectronApplicationCloseResult>;
}
+export type ElectronApplicationContextEvent = {
+ context: BrowserContextChannel,
+};
export type ElectronApplicationCloseEvent = {};
export type ElectronApplicationWindowEvent = {
page: PageChannel,
diff --git a/src/rpc/client/channelOwner.ts b/src/rpc/client/channelOwner.ts
index 411e13885a..90b251c0f3 100644
--- a/src/rpc/client/channelOwner.ts
+++ b/src/rpc/client/channelOwner.ts
@@ -15,10 +15,10 @@
*/
import { EventEmitter } from 'events';
-import { Channel } from '../channels';
-import { Connection } from './connection';
+import type { Channel } from '../channels';
+import type { Connection } from './connection';
import { assert } from '../../helper';
-import { LoggerSink } from '../../loggerSink';
+import type { LoggerSink } from '../../loggerSink';
import { DebugLoggerSink } from '../../logger';
export abstract class ChannelOwner<T extends Channel = Channel, Initializer = {}> extends EventEmitter {
@@ -37,11 +37,11 @@ export abstract class ChannelOwner<T extends Channel = Channel, Initializer = {}
constructor(parent: ChannelOwner | Connection, type: string, guid: string, initializer: Initializer, isScope?: boolean) {
super();
- this._connection = parent instanceof Connection ? parent : parent._connection;
+ this._connection = parent instanceof ChannelOwner ? parent._connection : parent;
this._type = type;
this._guid = guid;
this._isScope = !!isScope;
- this._parent = parent instanceof Connection ? undefined : parent;
+ this._parent = parent instanceof ChannelOwner ? parent : undefined;
this._connection._objects.set(guid, this);
if (this._parent) {
diff --git a/src/rpc/client/connection.ts b/src/rpc/client/connection.ts
index d6eddf1f01..74767a362f 100644
--- a/src/rpc/client/connection.ts
+++ b/src/rpc/client/connection.ts
@@ -131,8 +131,8 @@ export class Connection {
break;
case 'BrowserContext':
let browserName = '';
- if (parent instanceof Electron) {
- // Launching electron produces Electron parent for BrowserContext.
+ if (parent instanceof ElectronApplication) {
+ // Launching electron produces ElectronApplication parent for BrowserContext.
browserName = 'electron';
} else if (parent instanceof Browser) {
// Launching a browser produces Browser parent for BrowserContext.
diff --git a/src/rpc/client/electron.ts b/src/rpc/client/electron.ts
index e32143af17..03b7136e39 100644
--- a/src/rpc/client/electron.ts
+++ b/src/rpc/client/electron.ts
@@ -51,7 +51,7 @@ export class Electron extends ChannelOwner<ElectronChannel, ElectronInitializer>
}
export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel, ElectronApplicationInitializer> {
- private _context: BrowserContext;
+ private _context?: BrowserContext;
private _windows = new Set<Page>();
private _timeoutSettings = new TimeoutSettings();
@@ -60,8 +60,8 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
}
constructor(parent: ChannelOwner, type: string, guid: string, initializer: ElectronApplicationInitializer) {
- super(parent, type, guid, initializer);
- this._context = BrowserContext.from(initializer.context);
+ super(parent, type, guid, initializer, true);
+ 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);
@@ -71,6 +71,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
});
this._channel.on('close', () => {
this.emit(ElectronEvents.ElectronApplication.Close);
+ this._dispose();
});
}
@@ -90,7 +91,7 @@ export class ElectronApplication extends ChannelOwner<ElectronApplicationChannel
}
context(): BrowserContext {
- return this._context;
+ return this._context!;
}
async close() {
diff --git a/src/rpc/protocol.yml b/src/rpc/protocol.yml
index 8c4222e105..c918baf51d 100644
--- a/src/rpc/protocol.yml
+++ b/src/rpc/protocol.yml
@@ -1887,9 +1887,6 @@ Electron:
ElectronApplication:
type: interface
- initializer:
- context: BrowserContext
-
commands:
newBrowserWindow:
@@ -1918,6 +1915,11 @@ ElectronApplication:
events:
+ # This event happens once immediately after creation.
+ context:
+ parameters:
+ context: BrowserContext
+
close:
window:
diff --git a/src/rpc/server/electronDispatcher.ts b/src/rpc/server/electronDispatcher.ts
index a3f55818c3..91a8ab5b89 100644
--- a/src/rpc/server/electronDispatcher.ts
+++ b/src/rpc/server/electronDispatcher.ts
@@ -41,11 +41,12 @@ export class ElectronDispatcher extends Dispatcher<Electron, ElectronInitializer
export class ElectronApplicationDispatcher extends Dispatcher<ElectronApplication, ElectronApplicationInitializer> implements ElectronApplicationChannel {
constructor(scope: DispatcherScope, electronApplication: ElectronApplication) {
- super(scope, electronApplication, 'ElectronApplication', {
- context: new BrowserContextDispatcher(scope, electronApplication.context() as BrowserContextBase),
+ super(scope, electronApplication, 'ElectronApplication', {}, true);
+ this._dispatchEvent('context', { context: new BrowserContextDispatcher(this._scope, electronApplication.context() as BrowserContextBase) });
+ electronApplication.on(ElectronEvents.ElectronApplication.Close, () => {
+ this._dispatchEvent('close');
+ this._dispose();
});
-
- electronApplication.on(ElectronEvents.ElectronApplication.Close, () => this._dispatchEvent('close'));
electronApplication.on(ElectronEvents.ElectronApplication.Window, (page: ElectronPage) => {
this._dispatchEvent('window', {
page: lookupDispatcher<PageDispatcher>(page),