Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
if (isTargetClosedError(e) && !options.runBeforeUnload)
return;
throw e;
} finally {
// If the page actually closed, the 'close' event (and with it, screencast's
// auto-save) is dispatched by the server before the close response is sent,
// so this is guaranteed to observe an in-flight save, if any, at this point.
await this.screencast._waitForPendingAutoSave();
}
}

Expand Down
10 changes: 8 additions & 2 deletions packages/playwright-core/src/client/screencast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class Screencast implements api.Screencast {
private _savePath: string | undefined;
private _onFrame: ((frame: { data: Buffer, timestamp: number, viewportWidth: number, viewportHeight: number }) => Promise<any>) | null = null;
private _artifact: Artifact | undefined;
private _pendingAutoSave: Promise<void> | undefined;

constructor(page: Page) {
this._page = page;
Expand All @@ -35,19 +36,24 @@ export class Screencast implements api.Screencast {
});
this._page.once(Events.Page.Close, () => {
// Auto-save the video when the page closes, so recordings are not lost
// if the user forgets to call stop().
// if the user forgets to call stop(). page.close() awaits this via
// _waitForPendingAutoSave() before it resolves.
if (this._started && this._savePath && this._artifact) {
const artifact = this._artifact;
const savePath = this._savePath;
this._started = false;
this._onFrame = null;
this._artifact = undefined;
this._savePath = undefined;
artifact.saveAs(savePath).catch(() => {});
this._pendingAutoSave = artifact.saveAs(savePath).catch(() => {});
}
});
}

async _waitForPendingAutoSave(): Promise<void> {
await this._pendingAutoSave;
}

async start(options: { onFrame?: (frame: { data: Buffer, timestamp: number, viewportWidth: number, viewportHeight: number }) => Promise<any>|any, path?: string, size?: { width: number, height: number }, quality?: number } = {}): Promise<DisposableStub> {
if (this._started)
throw new Error('Screencast is already started');
Expand Down