fix: use loaderId to reduce test flakiness (#8717)

This commit is contained in:
Alex Rudenko 2022-08-01 15:39:08 +02:00 committed by Randolf J
parent 6b35128e3e
commit d2f6db2073
2 changed files with 8 additions and 5 deletions

View File

@ -213,6 +213,7 @@ export class FrameManager extends EventEmitter {
timeout = this.#timeoutSettings.navigationTimeout(),
} = options;
let ensureNewDocumentNavigation = false;
const watcher = new LifecycleWatcher(this, frame, waitUntil, timeout);
let error = await Promise.race([
navigate(this.#client, url, referer, frame._id),
@ -221,8 +222,9 @@ export class FrameManager extends EventEmitter {
if (!error) {
error = await Promise.race([
watcher.timeoutOrTerminationPromise(),
watcher.newDocumentNavigationPromise(),
watcher.sameDocumentNavigationPromise(),
ensureNewDocumentNavigation
? watcher.newDocumentNavigationPromise()
: watcher.sameDocumentNavigationPromise(),
]);
}
watcher.dispose();
@ -243,6 +245,7 @@ export class FrameManager extends EventEmitter {
referrer,
frameId,
});
ensureNewDocumentNavigation = !!response.loaderId;
return response.errorText
? new Error(`${response.errorText} at ${url}`)
: null;

View File

@ -70,6 +70,7 @@ export class LifecycleWatcher {
#timeout: number;
#navigationRequest: HTTPRequest | null = null;
#eventListeners: PuppeteerEventListener[];
#initialLoaderId: string;
#sameDocumentNavigationCompleteCallback: (x?: Error) => void = noop;
#sameDocumentNavigationPromise = new Promise<Error | undefined>(fulfill => {
@ -97,7 +98,6 @@ export class LifecycleWatcher {
#maximumTimer?: NodeJS.Timeout;
#hasSameDocumentNavigation?: boolean;
#newDocumentNavigation?: boolean;
#swapped?: boolean;
constructor(
@ -111,6 +111,7 @@ export class LifecycleWatcher {
} else if (typeof waitUntil === 'string') {
waitUntil = [waitUntil];
}
this.#initialLoaderId = frame._loaderId;
this.#expectedLifecycle = waitUntil.map(value => {
const protocolEvent = puppeteerToProtocolLifecycle.get(value);
assert(protocolEvent, 'Unknown value for options.waitUntil: ' + value);
@ -232,7 +233,6 @@ export class LifecycleWatcher {
if (frame !== this.#frame) {
return;
}
this.#newDocumentNavigation = true;
this.#checkLifecycleComplete();
}
@ -253,7 +253,7 @@ export class LifecycleWatcher {
if (this.#hasSameDocumentNavigation) {
this.#sameDocumentNavigationCompleteCallback();
}
if (this.#swapped || this.#newDocumentNavigation) {
if (this.#swapped || this.#frame._loaderId !== this.#initialLoaderId) {
this.#newDocumentNavigationCompleteCallback();
}