Skip to content

Commit 3bded35

Browse files
fix(chromium): wait for existing pages when connecting (#6511)
1 parent 92fa7dd commit 3bded35

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
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/chromium/crBrowser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,16 @@ export class CRBrowser extends Browser {
6161
return browser;
6262
}
6363
browser._defaultContext = new CRBrowserContext(browser, undefined, options.persistent);
64-
6564
await Promise.all([
66-
session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }),
65+
session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }).then(async () => {
66+
// Target.setAutoAttach has a bug where it does not wait for new Targets being attached.
67+
// However making a dummy call afterwards fixes this.
68+
// This can be removed after https://chromium-review.googlesource.com/c/chromium/src/+/2885888 lands in stable.
69+
await session.send('Target.getTargetInfo');
70+
}),
6771
(browser._defaultContext as CRBrowserContext)._initialize(),
6872
]);
69-
73+
await browser._waitForAllPagesToBeInitialized();
7074
return browser;
7175
}
7276

@@ -104,6 +108,10 @@ export class CRBrowser extends Browser {
104108
return this.options.name === 'clank';
105109
}
106110

111+
async _waitForAllPagesToBeInitialized() {
112+
await Promise.all([...this._crPages.values()].map(page => page.pageOrError()));
113+
}
114+
107115
_onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) {
108116
if (targetInfo.type === 'browser')
109117
return;

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/chromium/chromium.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ playwrightTest('should send extra headers with connect request', async ({browser
240240
});
241241

242242
playwrightTest('should report all pages in an existing browser', async ({ browserType, browserOptions }, testInfo) => {
243-
playwrightTest.fail();
244243
const port = 9339 + testInfo.workerIndex;
245244
const browserServer = await browserType.launch({
246245
...browserOptions,

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)