From b94643786b97dc51dd70278ec950a66b23bc4bba Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 19 May 2021 05:29:39 +0000 Subject: [PATCH] fix: wait for ffmpeg to finish writing even if page was closed (#6648) --- src/server/chromium/crPage.ts | 4 +++- tests/video.spec.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) 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); + }); + });