fix: resolve target manager init if no existing targets detected (#8748)

Closes #8747
This commit is contained in:
Alex Rudenko 2022-08-06 15:35:42 +02:00 committed by GitHub
parent 9588072a28
commit 8cb5043868
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 13 deletions

View File

@ -103,30 +103,37 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
// TODO: remove `as any` once the protocol definitions are updated with the // TODO: remove `as any` once the protocol definitions are updated with the
// next Chromium roll. // next Chromium roll.
this.#connection.send('Target.setDiscoverTargets', { this.#connection
discover: true, .send('Target.setDiscoverTargets', {
filter: [{type: 'tab', exclude: true}, {}], discover: true,
} as any); filter: [{type: 'tab', exclude: true}, {}],
} as any)
.then(this.#storeExistingTargetsForInit)
.catch(debugError);
} }
async initialize(): Promise<void> { #storeExistingTargetsForInit = () => {
this.#targetsIdsForInit = new Set();
for (const [ for (const [
targetId, targetId,
targetInfo, targetInfo,
] of this.#discoveredTargetsByTargetId.entries()) { ] of this.#discoveredTargetsByTargetId.entries()) {
if ( if (
!this.#targetFilterCallback || (!this.#targetFilterCallback ||
this.#targetFilterCallback(targetInfo) this.#targetFilterCallback(targetInfo)) &&
targetInfo.type !== 'browser'
) { ) {
this.#targetsIdsForInit.add(targetId); this.#targetsIdsForInit.add(targetId);
} }
} }
};
async initialize(): Promise<void> {
await this.#connection.send('Target.setAutoAttach', { await this.#connection.send('Target.setAutoAttach', {
waitForDebuggerOnStart: true, waitForDebuggerOnStart: true,
flatten: true, flatten: true,
autoAttach: true, autoAttach: true,
}); });
this.#finishInitializationIfReady();
await this.#initializePromise; await this.#initializePromise;
} }
@ -358,9 +365,7 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
this.#targetsIdsForInit.delete(target._targetId); this.#targetsIdsForInit.delete(target._targetId);
this.emit(TargetManagerEmittedEvents.TargetAvailable, target); this.emit(TargetManagerEmittedEvents.TargetAvailable, target);
if (this.#targetsIdsForInit.size === 0) { this.#finishInitializationIfReady();
this.#initializeCallback();
}
// TODO: the browser might be shutting down here. What do we do with the // TODO: the browser might be shutting down here. What do we do with the
// error? // error?
@ -374,8 +379,8 @@ export class ChromeTargetManager extends EventEmitter implements TargetManager {
]).catch(debugError); ]).catch(debugError);
}; };
#finishInitializationIfReady(targetId: string): void { #finishInitializationIfReady(targetId?: string): void {
this.#targetsIdsForInit.delete(targetId); targetId !== undefined && this.#targetsIdsForInit.delete(targetId);
if (this.#targetsIdsForInit.size === 0) { if (this.#targetsIdsForInit.size === 0) {
this.#initializeCallback(); this.#initializeCallback();
} }

View File

@ -699,6 +699,24 @@ describe('Launcher specs', function () {
remoteBrowser.close(), remoteBrowser.close(),
]); ]);
}); });
it('should be able to connect to a browser with no page targets', async () => {
const {defaultBrowserOptions, puppeteer} = getTestState();
const originalBrowser = await puppeteer.launch(defaultBrowserOptions);
const pages = await originalBrowser.pages();
await Promise.all(
pages.map(page => {
return page.close();
})
);
const remoteBrowser = await puppeteer.connect({
browserWSEndpoint: originalBrowser.wsEndpoint(),
});
await Promise.all([
utils.waitEvent(originalBrowser, 'disconnected'),
remoteBrowser.close(),
]);
});
it('should support ignoreHTTPSErrors option', async () => { it('should support ignoreHTTPSErrors option', async () => {
const {httpsServer, puppeteer, defaultBrowserOptions} = getTestState(); const {httpsServer, puppeteer, defaultBrowserOptions} = getTestState();