diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 9b9224b9090..bffd9577c63 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -361,7 +361,7 @@ export class CDPBrowser extends BrowserBase { }; #onAttachedToTarget = async (target: Target) => { - if (await target._initializedPromise) { + if ((await target._initializedPromise) === InitializationStatus.SUCCESS) { this.emit(BrowserEmittedEvents.TargetCreated, target); target .browserContext() @@ -372,7 +372,7 @@ export class CDPBrowser extends BrowserBase { #onDetachedFromTarget = async (target: Target): Promise => { target._initializedPromise.resolve(InitializationStatus.ABORTED); target._isClosedPromise.resolve(); - if (await target._initializedPromise) { + if ((await target._initializedPromise) === InitializationStatus.SUCCESS) { this.emit(BrowserEmittedEvents.TargetDestroyed, target); target .browserContext() @@ -432,7 +432,8 @@ export class CDPBrowser extends BrowserBase { if (!target) { throw new Error(`Missing target for page (id = ${targetId})`); } - const initialized = await target._initializedPromise; + const initialized = + (await target._initializedPromise) === InitializationStatus.SUCCESS; if (!initialized) { throw new Error(`Failed to create target for page (id = ${targetId})`); } @@ -453,7 +454,9 @@ export class CDPBrowser extends BrowserBase { return Array.from( this.#targetManager.getAvailableTargets().values() ).filter(target => { - return target._initializedPromise.resolved(); + return ( + target._initializedPromise.value() === InitializationStatus.SUCCESS + ); }); } diff --git a/packages/puppeteer-core/src/common/Target.ts b/packages/puppeteer-core/src/common/Target.ts index 1c8c7e1eed7..4a3008a2501 100644 --- a/packages/puppeteer-core/src/common/Target.ts +++ b/packages/puppeteer-core/src/common/Target.ts @@ -249,13 +249,13 @@ export class PageTarget extends Target { protected override _initialize(): void { this._initializedPromise - .then(async success => { - if (!success) { - return false; + .then(async result => { + if (result === InitializationStatus.ABORTED) { + return; } const opener = this.opener(); if (!(opener instanceof PageTarget)) { - return true; + return; } if (!opener || !opener.pagePromise || this.type() !== 'page') { return true;