Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix: wait for ffmpeg to finish writing even if page was closed (#6648)

Yury Semikhatskyb94643786b97
diff --git a/src/server/chromium/crPage.ts b/src/server/chromium/crPage.ts
index 1e849e53d3..dbfb2761cc 100644
--- a/src/server/chromium/crPage.ts
+++ b/src/server/chromium/crPage.ts
@@ -900,9 +900,11 @@ class FrameSession {
this._screencastId = null;
const recorder = this._videoRecorder!;
this._videoRecorder = null;
- const video = this._crPage._browserContext._browser._takeVideo(screencastId);
await this._stopScreencast(recorder);
await recorder.stop().catch(() => {});
+ // Keep the video artifact in the map utntil encoding is fully finished, if the context
+ // starts closing before the video is fully written to disk it will wait for it.
+ const video = this._crPage._browserContext._browser._takeVideo(screencastId);
video?.reportFinished();
}
diff --git a/tests/video.spec.ts b/tests/video.spec.ts
index 3f8a439265..26df9be1a3 100644
--- a/tests/video.spec.ts
+++ b/tests/video.spec.ts
@@ -615,4 +615,32 @@ it.describe('screencast', () => {
const saveResult = await page.video().saveAs(file).catch(e => e);
expect(saveResult.message).toContain('rowser has been closed');
});
+
+ it('should wait for video to finish if page was closed', async ({browserType, browserOptions, contextOptions}, testInfo) => {
+ const size = { width: 320, height: 240 };
+ const browser = await browserType.launch(browserOptions);
+
+ const videoDir = testInfo.outputPath('');
+ const context = await browser.newContext({
+ ...contextOptions,
+ recordVideo: {
+ dir: videoDir,
+ size,
+ },
+ viewport: size,
+ });
+
+ const page = await context.newPage();
+ await new Promise(r => setTimeout(r, 1000));
+ await page.close();
+ await context.close();
+ await browser.close();
+
+ const videoFiles = findVideos(videoDir);
+ expect(videoFiles.length).toBe(1);
+ const videoPlayer = new VideoPlayer(videoFiles[0]);
+ expect(videoPlayer.videoWidth).toBe(320);
+ expect(videoPlayer.videoHeight).toBe(240);
+ });
+
});