Skip to content

Commit

Permalink
fix: use loaderId to reduce test flakiness (#8717)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN authored and jrandolf committed Aug 2, 2022
1 parent 6b35128 commit d2f6db2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/common/FrameManager.ts
Expand Up @@ -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),
Expand All @@ -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();
Expand All @@ -243,6 +245,7 @@ export class FrameManager extends EventEmitter {
referrer,
frameId,
});
ensureNewDocumentNavigation = !!response.loaderId;
return response.errorText
? new Error(`${response.errorText} at ${url}`)
: null;
Expand Down
6 changes: 3 additions & 3 deletions src/common/LifecycleWatcher.ts
Expand Up @@ -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 => {
Expand Down Expand Up @@ -97,7 +98,6 @@ export class LifecycleWatcher {

#maximumTimer?: NodeJS.Timeout;
#hasSameDocumentNavigation?: boolean;
#newDocumentNavigation?: boolean;
#swapped?: boolean;

constructor(
Expand All @@ -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);
Expand Down Expand Up @@ -232,7 +233,6 @@ export class LifecycleWatcher {
if (frame !== this.#frame) {
return;
}
this.#newDocumentNavigation = true;
this.#checkLifecycleComplete();
}

Expand All @@ -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();
}

Expand Down

0 comments on commit d2f6db2

Please sign in to comment.