Skip to content

Commit 9273474

Browse files
committed
fix electron
1 parent 52b79e7 commit 9273474

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/client/electron.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
6767
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ElectronApplicationInitializer) {
6868
super(parent, type, guid, initializer);
6969
this._context = BrowserContext.from(initializer.context);
70-
this._context.on(Events.BrowserContext.Page, page => {
71-
this._windows.add(page);
72-
this.emit(Events.ElectronApplication.Window, page);
73-
page.once(Events.Page.Close, () => this._windows.delete(page));
74-
});
70+
for (const page of this._context._pages)
71+
this._onPage(page);
72+
this._context.on(Events.BrowserContext.Page, page => this._onPage(page));
7573
this._channel.on('close', () => this.emit(Events.ElectronApplication.Close));
7674
}
7775

76+
_onPage(page: Page) {
77+
this._windows.add(page);
78+
this.emit(Events.ElectronApplication.Window, page);
79+
page.once(Events.Page.Close, () => this._windows.delete(page));
80+
}
81+
7882
windows(): Page[] {
7983
// TODO: add ElectronPage class inherting from Page.
8084
return [...this._windows];

src/server/electron/electron.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ export class ElectronApplication extends SdkObject {
6363
// Emit application closed after context closed.
6464
Promise.resolve().then(() => this.emit(ElectronApplication.Events.Close));
6565
});
66+
for (const page of this._browserContext.pages())
67+
this._onPage(page);
6668
this._browserContext.on(BrowserContext.Events.Page, event => this._onPage(event));
6769
this._nodeConnection = nodeConnection;
6870
this._nodeSession = nodeConnection.rootSession;
@@ -77,7 +79,7 @@ export class ElectronApplication extends SdkObject {
7779
this._nodeSession.send('Runtime.enable', {}).catch(e => {});
7880
}
7981

80-
private async _onPage(page: Page) {
82+
private _onPage(page: Page) {
8183
// Needs to be sync.
8284
const windowId = ++this._lastWindowId;
8385
(page as any)._browserWindowId = windowId;

tests/electron/electron-app.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test('should return browser window', async ({ playwright }) => {
102102
const electronApp = await playwright._electron.launch({
103103
args: [path.join(__dirname, 'electron-window-app.js')],
104104
});
105-
const page = await electronApp.waitForEvent('window');
105+
const page = await electronApp.firstWindow();
106106
const bwHandle = await electronApp.browserWindow(page);
107107
expect(await bwHandle.evaluate((bw: BrowserWindow) => bw.title)).toBe('Electron');
108108
await electronApp.close();

0 commit comments

Comments
 (0)