mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
fix(chromium): wait for existing pages when connecting (#6511)
This commit is contained in:
parent
92fa7ddead
commit
3bded35834
@ -67,12 +67,16 @@ export class ElectronApplication extends ChannelOwner<channels.ElectronApplicati
|
|||||||
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ElectronApplicationInitializer) {
|
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.ElectronApplicationInitializer) {
|
||||||
super(parent, type, guid, initializer);
|
super(parent, type, guid, initializer);
|
||||||
this._context = BrowserContext.from(initializer.context);
|
this._context = BrowserContext.from(initializer.context);
|
||||||
this._context.on(Events.BrowserContext.Page, 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._windows.add(page);
|
||||||
this.emit(Events.ElectronApplication.Window, page);
|
this.emit(Events.ElectronApplication.Window, page);
|
||||||
page.once(Events.Page.Close, () => this._windows.delete(page));
|
page.once(Events.Page.Close, () => this._windows.delete(page));
|
||||||
});
|
|
||||||
this._channel.on('close', () => this.emit(Events.ElectronApplication.Close));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
windows(): Page[] {
|
windows(): Page[] {
|
||||||
|
|||||||
@ -61,12 +61,16 @@ export class CRBrowser extends Browser {
|
|||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
browser._defaultContext = new CRBrowserContext(browser, undefined, options.persistent);
|
browser._defaultContext = new CRBrowserContext(browser, undefined, options.persistent);
|
||||||
|
|
||||||
await Promise.all([
|
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(),
|
(browser._defaultContext as CRBrowserContext)._initialize(),
|
||||||
]);
|
]);
|
||||||
|
await browser._waitForAllPagesToBeInitialized();
|
||||||
return browser;
|
return browser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,6 +108,10 @@ export class CRBrowser extends Browser {
|
|||||||
return this.options.name === 'clank';
|
return this.options.name === 'clank';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _waitForAllPagesToBeInitialized() {
|
||||||
|
await Promise.all([...this._crPages.values()].map(page => page.pageOrError()));
|
||||||
|
}
|
||||||
|
|
||||||
_onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) {
|
_onAttachedToTarget({targetInfo, sessionId, waitingForDebugger}: Protocol.Target.attachedToTargetPayload) {
|
||||||
if (targetInfo.type === 'browser')
|
if (targetInfo.type === 'browser')
|
||||||
return;
|
return;
|
||||||
|
|||||||
@ -63,6 +63,8 @@ export class ElectronApplication extends SdkObject {
|
|||||||
// Emit application closed after context closed.
|
// Emit application closed after context closed.
|
||||||
Promise.resolve().then(() => this.emit(ElectronApplication.Events.Close));
|
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._browserContext.on(BrowserContext.Events.Page, event => this._onPage(event));
|
||||||
this._nodeConnection = nodeConnection;
|
this._nodeConnection = nodeConnection;
|
||||||
this._nodeSession = nodeConnection.rootSession;
|
this._nodeSession = nodeConnection.rootSession;
|
||||||
@ -77,7 +79,7 @@ export class ElectronApplication extends SdkObject {
|
|||||||
this._nodeSession.send('Runtime.enable', {}).catch(e => {});
|
this._nodeSession.send('Runtime.enable', {}).catch(e => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _onPage(page: Page) {
|
private _onPage(page: Page) {
|
||||||
// Needs to be sync.
|
// Needs to be sync.
|
||||||
const windowId = ++this._lastWindowId;
|
const windowId = ++this._lastWindowId;
|
||||||
(page as any)._browserWindowId = windowId;
|
(page as any)._browserWindowId = windowId;
|
||||||
|
|||||||
@ -240,7 +240,6 @@ playwrightTest('should send extra headers with connect request', async ({browser
|
|||||||
});
|
});
|
||||||
|
|
||||||
playwrightTest('should report all pages in an existing browser', async ({ browserType, browserOptions }, testInfo) => {
|
playwrightTest('should report all pages in an existing browser', async ({ browserType, browserOptions }, testInfo) => {
|
||||||
playwrightTest.fail();
|
|
||||||
const port = 9339 + testInfo.workerIndex;
|
const port = 9339 + testInfo.workerIndex;
|
||||||
const browserServer = await browserType.launch({
|
const browserServer = await browserType.launch({
|
||||||
...browserOptions,
|
...browserOptions,
|
||||||
|
|||||||
@ -102,7 +102,7 @@ test('should return browser window', async ({ playwright }) => {
|
|||||||
const electronApp = await playwright._electron.launch({
|
const electronApp = await playwright._electron.launch({
|
||||||
args: [path.join(__dirname, 'electron-window-app.js')],
|
args: [path.join(__dirname, 'electron-window-app.js')],
|
||||||
});
|
});
|
||||||
const page = await electronApp.waitForEvent('window');
|
const page = await electronApp.firstWindow();
|
||||||
const bwHandle = await electronApp.browserWindow(page);
|
const bwHandle = await electronApp.browserWindow(page);
|
||||||
expect(await bwHandle.evaluate((bw: BrowserWindow) => bw.title)).toBe('Electron');
|
expect(await bwHandle.evaluate((bw: BrowserWindow) => bw.title)).toBe('Electron');
|
||||||
await electronApp.close();
|
await electronApp.close();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user