From 19820de7a9b05e3841b6e14c033fcf9d1d40fa1f Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 26 Jan 2022 07:43:07 -0800 Subject: [PATCH] fix(test runner): correctly save videos when running remotely (#11633) --- packages/playwright-test/src/index.ts | 5 +++-- tests/playwright-test/playwright.spec.ts | 26 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.ts index 3374aa3d72..d8b59cce66 100644 --- a/packages/playwright-test/src/index.ts +++ b/packages/playwright-test/src/index.ts @@ -392,6 +392,7 @@ export const test = _baseTest.extend({ const prependToError = testInfo.status === 'timedOut' ? formatPendingCalls((browser as any)._connection.pendingProtocolCalls()) : ''; + let counter = 0; await Promise.all([...contexts.keys()].map(async context => { await context.close(); @@ -402,8 +403,8 @@ export const test = _baseTest.extend({ const videos = pages.map(p => p.video()).filter(Boolean) as Video[]; await Promise.all(videos.map(async v => { try { - const videoPath = await v.path(); - const savedPath = testInfo.outputPath(path.basename(videoPath)); + const savedPath = testInfo.outputPath(`video${counter ? '-' + counter : ''}.webm`); + ++counter; await v.saveAs(savedPath); testInfo.attachments.push({ name: 'video', path: savedPath, contentType: 'video/webm' }); } catch (e) { diff --git a/tests/playwright-test/playwright.spec.ts b/tests/playwright-test/playwright.spec.ts index 80976b98e0..cf7cb03a30 100644 --- a/tests/playwright-test/playwright.spec.ts +++ b/tests/playwright-test/playwright.spec.ts @@ -492,3 +492,29 @@ test('should work with video size', async ({ runInlineTest }, testInfo) => { expect(videoPlayer.videoWidth).toBe(220); expect(videoPlayer.videoHeight).toBe(110); }); + +test('should work with video.path() throwing', async ({ runInlineTest }, testInfo) => { + // When running remotely, video.path() is not available, so we must not use it. + const result = await runInlineTest({ + 'playwright.config.js': ` + module.exports = { + use: { video: { mode: 'on' } }, + name: 'chromium', + preserveOutput: 'always', + }; + `, + 'a.test.ts': ` + const { test } = pwt; + test('pass', async ({ page }) => { + page.video().path = () => { throw new Error('No-no!'); }; + await page.setContent('
PASS
'); + await page.waitForTimeout(3000); + }); + `, + }, { workers: 1 }); + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); + const dir = testInfo.outputPath(`test-results/a-pass-chromium/`); + const video = fs.readdirSync(dir).find(file => file.endsWith('webm')); + expect(video).toBeTruthy(); +});