From f2916d4ced40359089a4cca2cc74a95a49a9802a Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Wed, 21 Dec 2022 11:14:30 -0800 Subject: [PATCH] cherry-pick(#19627): fix(screenshot): account for `screenshot === undefined` (#19631) --- packages/playwright-test/src/index.ts | 12 +++- tests/playwright-test/playwright.spec.ts | 83 ++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.ts index 4443f2248548e..5f157fc722fb5 100644 --- a/packages/playwright-test/src/index.ts +++ b/packages/playwright-test/src/index.ts @@ -20,7 +20,7 @@ import type { APIRequestContext, BrowserContext, BrowserContextOptions, LaunchOp import * as playwrightLibrary from 'playwright-core'; import { createGuid, debugMode } from 'playwright-core/lib/utils'; import { removeFolders } from 'playwright-core/lib/utils/fileUtils'; -import type { Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestInfo, TestType, TraceMode, VideoMode } from '../types/test'; +import type { Fixtures, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, ScreenshotMode, TestInfo, TestType, TraceMode, VideoMode } from '../types/test'; import { store as _baseStore } from './store'; import type { TestInfoImpl } from './testInfo'; import { rootTestType, _setProjectSetup } from './testType'; @@ -246,8 +246,8 @@ const playwrightFixtures: Fixtures = ({ if (debugMode()) testInfo.setTimeout(0); - const screenshotOptions = typeof screenshot !== 'string' ? { fullPage: screenshot.fullPage, omitBackground: screenshot.omitBackground } : undefined; - const screenshotMode = typeof screenshot === 'string' ? screenshot : screenshot.mode; + const screenshotMode = normalizeScreenshotMode(screenshot); + const screenshotOptions = typeof screenshot === 'string' ? undefined : screenshot; const traceMode = normalizeTraceMode(trace); const defaultTraceOptions = { screenshots: true, snapshots: true, sources: true }; const traceOptions = typeof trace === 'string' ? defaultTraceOptions : { ...defaultTraceOptions, ...trace, mode: undefined }; @@ -620,6 +620,12 @@ export function shouldCaptureTrace(traceMode: TraceMode, testInfo: TestInfo) { return traceMode === 'on' || traceMode === 'retain-on-failure' || (traceMode === 'on-first-retry' && testInfo.retry === 1); } +function normalizeScreenshotMode(screenshot: PlaywrightWorkerOptions['screenshot'] | undefined): ScreenshotMode { + if (!screenshot) + return 'off'; + return typeof screenshot === 'string' ? screenshot : screenshot.mode; +} + const kTracingStarted = Symbol('kTracingStarted'); export const test = _baseTest.extend(playwrightFixtures); diff --git a/tests/playwright-test/playwright.spec.ts b/tests/playwright-test/playwright.spec.ts index daa856f51a6d9..7956c6123b9ed 100644 --- a/tests/playwright-test/playwright.spec.ts +++ b/tests/playwright-test/playwright.spec.ts @@ -600,3 +600,86 @@ test('should pass fixture defaults to tests', async ({ runInlineTest }) => { expect(result.exitCode).toBe(0); expect(result.passed).toBe(1); }); + +test('should not throw with many fixtures set to undefined', async ({ runInlineTest }, testInfo) => { + const result = await runInlineTest({ + 'playwright.config.ts': ` + module.exports = { use: { + headless: undefined, + channel: undefined, + launchOptions: undefined, + connectOptions: undefined, + screenshot: undefined, + video: undefined, + trace: undefined, + acceptDownloads: undefined, + bypassCSP: undefined, + colorScheme: undefined, + deviceScaleFactor: undefined, + extraHTTPHeaders: undefined, + geolocation: undefined, + hasTouch: undefined, + httpCredentials: undefined, + ignoreHTTPSErrors: undefined, + isMobile: undefined, + javaScriptEnabled: undefined, + locale: undefined, + offline: undefined, + permissions: undefined, + proxy: undefined, + storageState: undefined, + timezoneId: undefined, + userAgent: undefined, + viewport: undefined, + actionTimeout: undefined, + testIdAttribute: undefined, + navigationTimeout: undefined, + baseURL: undefined, + serviceWorkers: undefined, + contextOptions: undefined, + } }; + `, + 'a.spec.ts': ` + const { test } = pwt; + test.use({ + headless: undefined, + channel: undefined, + launchOptions: undefined, + connectOptions: undefined, + screenshot: undefined, + video: undefined, + trace: undefined, + acceptDownloads: undefined, + bypassCSP: undefined, + colorScheme: undefined, + deviceScaleFactor: undefined, + extraHTTPHeaders: undefined, + geolocation: undefined, + hasTouch: undefined, + httpCredentials: undefined, + ignoreHTTPSErrors: undefined, + isMobile: undefined, + javaScriptEnabled: undefined, + locale: undefined, + offline: undefined, + permissions: undefined, + proxy: undefined, + storageState: undefined, + timezoneId: undefined, + userAgent: undefined, + viewport: undefined, + actionTimeout: undefined, + testIdAttribute: undefined, + navigationTimeout: undefined, + baseURL: undefined, + serviceWorkers: undefined, + contextOptions: undefined, + }); + test('passes', async ({ page }) => { + }); + `, + }, { workers: 1 }); + + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); +});