Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/client/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ElectronApplicationInitializer) {
super(parent, type, guid, initializer);
this._context = BrowserContext.from(initializer.context);
this._context.on(Events.BrowserContext.Page, page => {
this._windows.add(page);
this.emit(Events.ElectronApplication.Window, page);
page.once(Events.Page.Close, () => this._windows.delete(page));
});
for (const page of this._context._pages)
this._onPage(page);
this._context.on(Events.BrowserContext.Page, page => this._onPage(page));
this._channel.on('close', () => this.emit(Events.ElectronApplication.Close));
}

_onPage(page: Page) {
this._windows.add(page);
this.emit(Events.ElectronApplication.Window, page);
page.once(Events.Page.Close, () => this._windows.delete(page));
}

windows(): Page[] {
// TODO: add ElectronPage class inherting from Page.
return [...this._windows];
Expand Down
14 changes: 11 additions & 3 deletions src/server/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ export class CRBrowser extends Browser {
return browser;
}
browser._defaultContext = new CRBrowserContext(browser, undefined, options.persistent);

await Promise.all([
session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }),
session.send('Target.setAutoAttach', { autoAttach: true, waitForDebuggerOnStart: true, flatten: true }).then(async () => {
// Target.setAutoAttach has a bug where it does not wait for new Targets being attached.
// However making a dummy call afterwards fixes this.
// This can be removed after https://chromium-review.googlesource.com/c/chromium/src/+/2885888 lands in stable.
await session.send('Target.getTargetInfo');
}),
(browser._defaultContext as CRBrowserContext)._initialize(),
]);

await browser._waitForAllPagesToBeInitialized();
return browser;
}

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

async _waitForAllPagesToBeInitialized() {
await Promise.all([...this._crPages.values()].map(page => page.pageOrError()));
}

_onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) {
if (targetInfo.type === 'browser')
return;
Expand Down
4 changes: 3 additions & 1 deletion src/server/electron/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class ElectronApplication extends SdkObject {
// Emit application closed after context closed.
Promise.resolve().then(() => this.emit(ElectronApplication.Events.Close));
});
for (const page of this._browserContext.pages())
this._onPage(page);
this._browserContext.on(BrowserContext.Events.Page, event => this._onPage(event));
this._nodeConnection = nodeConnection;
this._nodeSession = nodeConnection.rootSession;
Expand All @@ -77,7 +79,7 @@ export class ElectronApplication extends SdkObject {
this._nodeSession.send('Runtime.enable', {}).catch(e => {});
}

private async _onPage(page: Page) {
private _onPage(page: Page) {
// Needs to be sync.
const windowId = ++this._lastWindowId;
(page as any)._browserWindowId = windowId;
Expand Down
27 changes: 27 additions & 0 deletions tests/chromium/chromium.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,30 @@ playwrightTest.describe('chromium', () => {
}
});
});

playwrightTest('should report all pages in an existing browser', async ({ browserType, browserOptions }, testInfo) => {
const port = 9339 + testInfo.workerIndex;
const browserServer = await browserType.launch({
...browserOptions,
args: ['--remote-debugging-port=' + port]
});
try {
const cdpBrowser = await browserType.connectOverCDP({
endpointURL: `http://localhost:${port}/`,
});
const contexts = cdpBrowser.contexts();
expect(contexts.length).toBe(1);
for (let i = 0; i < 3; i++)
await contexts[0].newPage();
await cdpBrowser.close();

const cdpBrowser2 = await browserType.connectOverCDP({
endpointURL: `http://localhost:${port}/`,
});
expect(cdpBrowser2.contexts()[0].pages().length).toBe(3);

await cdpBrowser2.close();
} finally {
await browserServer.close();
}
});
2 changes: 1 addition & 1 deletion tests/electron/electron-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ test('should return browser window', async ({ playwright }) => {
const electronApp = await playwright._electron.launch({
args: [path.join(__dirname, '..', 'config', 'electron-window-app.js')],
});
const page = await electronApp.waitForEvent('window');
const page = await electronApp.firstWindow();
const bwHandle = await electronApp.browserWindow(page);
expect(await bwHandle.evaluate((bw: BrowserWindow) => bw.title)).toBe('Electron');
await electronApp.close();
Expand Down