Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(fixtures): make sure defaultLaunchOptions are always there (#11713)
Dmitry Gozman30f3cbd6da55
diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.tsindex 77d4964309..34d67fe69b 100644--- a/packages/playwright-test/src/index.ts+++ b/packages/playwright-test/src/index.ts@@ -16,7 +16,7 @@import * as fs from 'fs';import * as path from 'path';-import type { LaunchOptions, BrowserContextOptions, Page, BrowserContext, BrowserType, Video, Browser, APIRequestContext } from 'playwright-core';+import type { LaunchOptions, BrowserContextOptions, Page, BrowserContext, Video, APIRequestContext } from 'playwright-core';import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestInfo } from '../types/test';import { rootTestType } from './testType';import { createGuid, removeFolders, debugMode } from 'playwright-core/lib/utils/utils';@@ -44,7 +44,6 @@ type TestFixtures = PlaywrightTestArgs & PlaywrightTestOptions & {_contextFactory: (options?: BrowserContextOptions) => Promise<BrowserContext>;};type WorkerFixtures = PlaywrightWorkerArgs & PlaywrightWorkerOptions & {- _browserType: BrowserType;_browserOptions: LaunchOptions;_artifactsDir: () => string;_snapshotSuffix: string;@@ -88,9 +87,26 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({await removeFolders([dir]);}, { scope: 'worker' }],- _browserOptions: [browserOptionsWorkerFixture, { scope: 'worker' }],- _browserType: [browserTypeWorkerFixture, { scope: 'worker' }],- browser: [browserWorkerFixture, { scope: 'worker' } ],+ _browserOptions: [async ({ headless, channel, launchOptions }, use) => {+ const options: LaunchOptions = {+ handleSIGINT: false,+ timeout: 0,+ ...launchOptions,+ };+ if (headless !== undefined)+ options.headless = headless;+ if (channel !== undefined)+ options.channel = channel;+ await use(options);+ }, { scope: 'worker' }],++ browser: [async ({ playwright, browserName }, use) => {+ if (!['chromium', 'firefox', 'webkit'].includes(browserName))+ throw new Error(`Unexpected browserName "${browserName}", must be one of "chromium", "firefox" or "webkit"`);+ const browser = await playwright[browserName].launch();+ await use(browser);+ await browser.close();+ }, { scope: 'worker' } ],acceptDownloads: [ undefined, { option: true } ],bypassCSP: [ undefined, { option: true } ],@@ -190,7 +206,7 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({_snapshotSuffix: [process.env.PLAYWRIGHT_DOCKER ? 'docker' : process.platform, { scope: 'worker' }],- _setupContextOptionsAndArtifacts: [async ({ playwright, _snapshotSuffix, _combinedContextOptions, _artifactsDir, trace, screenshot, actionTimeout, navigationTimeout }, use, testInfo) => {+ _setupContextOptionsAndArtifacts: [async ({ playwright, _snapshotSuffix, _combinedContextOptions, _browserOptions, _artifactsDir, trace, screenshot, actionTimeout, navigationTimeout }, use, testInfo) => {testInfo.snapshotSuffix = _snapshotSuffix;if (debugMode())testInfo.setTimeout(0);@@ -284,6 +300,7 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({(browserType as any)._onDidCreateContext = onDidCreateBrowserContext;(browserType as any)._onWillCloseContext = onWillCloseContext;(browserType as any)._defaultContextOptions = _combinedContextOptions;+ (browserType as any)._defaultLaunchOptions = _browserOptions;const existingContexts = Array.from((browserType as any)._contexts) as BrowserContext[];await Promise.all(existingContexts.map(onDidCreateBrowserContext));}@@ -320,6 +337,7 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({(browserType as any)._onDidCreateContext = undefined;(browserType as any)._onWillCloseContext = undefined;(browserType as any)._defaultContextOptions = undefined;+ (browserType as any)._defaultLaunchOptions = undefined;}leftoverContexts.forEach(context => (context as any)._instrumentation.removeAllListeners());(playwright.request as any)._onDidCreateContext = undefined;@@ -435,54 +453,6 @@ export const test = _baseTest.extend<TestFixtures, WorkerFixtures>({});-export async function browserOptionsWorkerFixture(- {- headless,- channel,- launchOptions- }: {- headless: boolean | undefined,- channel: string | undefined,- launchOptions: LaunchOptions- }, use: (options: LaunchOptions) => Promise<void>) {- const options: LaunchOptions = {- handleSIGINT: false,- timeout: 0,- ...launchOptions,- };- if (headless !== undefined)- options.headless = headless;- if (channel !== undefined)- options.channel = channel;- await use(options);-}--export async function browserTypeWorkerFixture(- {- playwright,- browserName,- _browserOptions- }: {- playwright: any,- browserName: string,- _browserOptions: LaunchOptions- }, use: (browserType: BrowserType) => Promise<void>) {- if (!['chromium', 'firefox', 'webkit'].includes(browserName))- throw new Error(`Unexpected browserName "${browserName}", must be one of "chromium", "firefox" or "webkit"`);- const browserType = playwright[browserName];- (browserType as any)._defaultLaunchOptions = _browserOptions;- await use(browserType);- (browserType as any)._defaultLaunchOptions = undefined;-}--export async function browserWorkerFixture(- { _browserType }: { _browserType: BrowserType },- use: (browser: Browser) => Promise<void>) {- const browser = await _browserType.launch();- await use(browser);- await browser.close();-}-function formatPendingCalls(calls: ParsedStackTrace[]) {if (!calls.length)diff --git a/tests/config/browserTest.ts b/tests/config/browserTest.tsindex aedd2b1bcc..f7abcc2584 100644--- a/tests/config/browserTest.ts+++ b/tests/config/browserTest.ts@@ -43,8 +43,8 @@ const test = baseTest.extend<BrowserTestTestFixtures, BrowserTestWorkerFixtures>await run(browser.version());}, { scope: 'worker' } ],- browserType: [async ({ _browserType }: any, run) => {- await run(_browserType);+ browserType: [async ({ playwright, browserName }, run) => {+ await run(playwright[browserName]);}, { scope: 'worker' } ],browserMajorVersion: [async ({ browserVersion }, run) => {diff --git a/tests/playwright-test/playwright.spec.ts b/tests/playwright-test/playwright.spec.tsindex 7cd266aced..1c1cdde31f 100644--- a/tests/playwright-test/playwright.spec.ts+++ b/tests/playwright-test/playwright.spec.ts@@ -229,6 +229,34 @@ test('should respect context options in various contexts', async ({ runInlineTesexpect(result.passed).toBe(5);});+test('should respect headless in launchPersistent', async ({ runInlineTest }) => {+ const result = await runInlineTest({+ 'playwright.config.ts': `+ module.exports = { use: { headless: false } };+ `,+ 'a.test.ts': `+ import fs from 'fs';+ import os from 'os';+ import path from 'path';+ import rimraf from 'rimraf';++ const { test } = pwt;++ test('persistent context', async ({ playwright, browserName }) => {+ const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'user-data-dir-'));+ const context = await playwright[browserName].launchPersistentContext(dir);+ const page = context.pages()[0];+ expect(await page.evaluate(() => navigator.userAgent)).not.toContain('Headless');+ await context.close();+ rimraf.sync(dir);+ });+ `,+ }, { workers: 1 });++ expect(result.exitCode).toBe(0);+ expect(result.passed).toBe(1);+});+test('should call logger from launchOptions config', async ({ runInlineTest }, testInfo) => {const result = await runInlineTest({'a.test.ts': `