Skip to content

Commit ba6bcee

Browse files
committed
fix(puppet): consisten timeout handling
1 parent 2979ba7 commit ba6bcee

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

core/lib/FrameEnvironment.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ b) Use the UserProfile feature to set cookies for 1 or more domains before they'
355355
return this.waitForDom(jsPath, options);
356356
}
357357

358-
public waitForLoad(status: IPipelineStatus, options?: IWaitForOptions): Promise<void> {
358+
public async waitForLoad(status: IPipelineStatus, options?: IWaitForOptions): Promise<void> {
359+
await this.isReady;
359360
return this.navigationsObserver.waitForLoad(status, options);
360361
}
361362

core/test/domRecorder.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ function sort() {
225225
const domFrames = domChanges.filter(x => x.tagName === 'IFRAME');
226226
expect(domFrames).toHaveLength(3);
227227

228-
await tab.puppetPage.frames[3].waitForLoad();
228+
await tab.frameEnvironmentsByPuppetId.get(tab.puppetPage.frames[3].id).isReady;
229229

230230
await state.db.flush();
231231
const frames = state.db.frames.all();

interfaces/IPuppetFrame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface IPuppetFrame extends ITypedEventEmitter<IPuppetFrameEvents> {
1414
isDefaultUrl: boolean;
1515
isAttached(): boolean;
1616
resolveNodeId(backendNodeId: number): Promise<string>;
17-
waitForLoad(eventName?: keyof ILifecycleEvents): Promise<void>;
17+
waitForLoad(eventName?: keyof ILifecycleEvents, timeoutMs?: number): Promise<void>;
1818
waitForLoader(loaderId?: string): Promise<Error | undefined>;
1919
canEvaluate(isolatedFromWebPageEnvironment: boolean): boolean;
2020
getFrameElementNodeId(): Promise<string>;

puppet-chrome/lib/Frame.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,12 @@ export default class Frame extends TypedEventEmitter<IPuppetFrameEvents> impleme
304304
}
305305
}
306306

307-
public async waitForLoader(loaderId?: string): Promise<Error | null> {
307+
public async waitForLoader(loaderId?: string, timeoutMs = 60e3): Promise<Error | null> {
308308
if (!loaderId) {
309309
loaderId = this.activeLoaderId;
310310
if (loaderId === this.defaultLoaderId) {
311311
// wait for an actual frame to load
312-
const frameLoader = await this.waitOn('frame-loader-created', null, 60e3);
312+
const frameLoader = await this.waitOn('frame-loader-created', null, timeoutMs);
313313
loaderId = frameLoader.loaderId;
314314
}
315315
}
@@ -438,9 +438,15 @@ export default class Frame extends TypedEventEmitter<IPuppetFrameEvents> impleme
438438
};
439439
}
440440

441-
public async waitForLoad(event: keyof ILifecycleEvents = 'load'): Promise<void> {
441+
public async waitForLoad(
442+
event: keyof ILifecycleEvents = 'load',
443+
timeoutMs = 30e3,
444+
): Promise<void> {
445+
event ??= 'load';
446+
timeoutMs ??= 30e3;
447+
await this.waitForLoader(null, timeoutMs);
442448
if (this.lifecycleEvents[event]) return;
443-
await this.waitOn('frame-lifecycle', x => x.name === event, 30e3);
449+
await this.waitOn('frame-lifecycle', x => x.name === event, timeoutMs);
444450
}
445451

446452
private setLoader(loaderId: string): void {

puppet-chrome/lib/FramesManager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export default class FramesManager extends TypedEventEmitter<IPuppetFrameManager
9292
}
9393
} catch (error) {
9494
if (error instanceof CanceledPromiseError) {
95+
resolve();
9596
return;
9697
}
9798
reject(error);

puppet/test/TestPage.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface ITestPage extends IPuppetPage {
1010
type(text: string): Promise<void>;
1111
attachFrame(frameId: string, url: string): Promise<IPuppetFrame>;
1212
detachFrame(frameId: string): Promise<void>;
13-
goto(url: string, waitOnLifecycle?: string): Promise<void>;
13+
goto(url: string, waitOnLifecycle?: string, timeoutMs?: number): Promise<void>;
1414
setContent(content: string): Promise<void>;
1515
waitForPopup(): Promise<IPuppetPage>;
1616
}
@@ -81,10 +81,15 @@ export async function goto(
8181
page: IPuppetPage,
8282
url: string,
8383
waitOnLifecycle: 'load' | 'DOMContentLoaded' = 'load',
84+
timeoutMs?: number,
8485
) {
85-
const nav = page.navigate(url);
86-
const lifecycle = page.mainFrame.waitOn('frame-lifecycle', ev => ev.name === waitOnLifecycle);
87-
await Promise.all([lifecycle, nav, page.mainFrame.waitOn('frame-navigated')]);
86+
waitOnLifecycle ??= 'load';
87+
timeoutMs ??= 30e3;
88+
await Promise.all([
89+
page.navigate(url),
90+
page.mainFrame.waitOn('frame-navigated', null, timeoutMs),
91+
]);
92+
await page.mainFrame.waitForLoad(waitOnLifecycle, timeoutMs);
8893
}
8994

9095
export async function setContent(page: IPuppetPage, content: string) {

0 commit comments

Comments
 (0)