cherry-pick(release-1.11): wait for existing pages when connecting (#6619)
This cherry-picks two PRs: - PR https://github.com/microsoft/playwright/pull/6502 SHA2ea465bc82- PR https://github.com/microsoft/playwright/pull/6511 SHA3bded35834Co-authored-by: Joel Einbinder <joel.einbinder@gmail.com>
This commit is contained in:
parent
f496c65491
commit
97aacf3cde
|
|
@ -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];
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue