From 15053d9b3bafea6260b0b49fd0bd14d428addf88 Mon Sep 17 00:00:00 2001 From: Joel Einbinder Date: Mon, 29 Nov 2021 12:21:15 -0500 Subject: [PATCH] fix(test-runner): apply default options to all browserTypes (#10507) --- packages/playwright-test/src/index.ts | 29 ++++++++++++++---------- tests/playwright-test/playwright.spec.ts | 11 ++++++++- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/packages/playwright-test/src/index.ts b/packages/playwright-test/src/index.ts index aab953ef1e..1016709065 100644 --- a/packages/playwright-test/src/index.ts +++ b/packages/playwright-test/src/index.ts @@ -16,12 +16,12 @@ import * as fs from 'fs'; import * as path from 'path'; -import type { LaunchOptions, BrowserContextOptions, Page, BrowserContext, BrowserType, Video } from 'playwright-core'; +import type { LaunchOptions, BrowserContextOptions, Page, BrowserContext, BrowserType, Video, Browser } from 'playwright-core'; import type { TestType, PlaywrightTestArgs, PlaywrightTestOptions, PlaywrightWorkerArgs, PlaywrightWorkerOptions, TestInfo } from '../types/test'; import { rootTestType } from './testType'; import { createGuid, removeFolders } from 'playwright-core/lib/utils/utils'; import { GridClient } from 'playwright-core/lib/grid/gridClient'; -import { Browser } from 'playwright-core'; +import { chromium, firefox, webkit } from 'playwright-core'; import { prependToTestError } from './util'; export { expect } from './expect'; export const _baseTest: TestType<{}, {}> = rootTestType.test; @@ -172,7 +172,7 @@ export const test = _baseTest.extend({ _snapshotSuffix: [process.env.PLAYWRIGHT_DOCKER ? 'docker' : process.platform, { scope: 'worker' }], - _setupContextOptionsAndArtifacts: [async ({ _snapshotSuffix, _browserType, _combinedContextOptions, _artifactsDir, trace, screenshot, actionTimeout, navigationTimeout }, use, testInfo) => { + _setupContextOptionsAndArtifacts: [async ({ _snapshotSuffix, _combinedContextOptions, _artifactsDir, trace, screenshot, actionTimeout, navigationTimeout }, use, testInfo) => { testInfo.snapshotSuffix = _snapshotSuffix; if (process.env.PWDEBUG) testInfo.setTimeout(0); @@ -248,11 +248,13 @@ export const test = _baseTest.extend({ }; // 1. Setup instrumentation and process existing contexts. - (_browserType as any)._onDidCreateContext = onDidCreateContext; - (_browserType as any)._onWillCloseContext = onWillCloseContext; - (_browserType as any)._defaultContextOptions = _combinedContextOptions; - const existingContexts = Array.from((_browserType as any)._contexts) as BrowserContext[]; - await Promise.all(existingContexts.map(onDidCreateContext)); + for (const browserType of [chromium, firefox, webkit]) { + (browserType as any)._onDidCreateContext = onDidCreateContext; + (browserType as any)._onWillCloseContext = onWillCloseContext; + (browserType as any)._defaultContextOptions = _combinedContextOptions; + const existingContexts = Array.from((browserType as any)._contexts) as BrowserContext[]; + await Promise.all(existingContexts.map(onDidCreateContext)); + } // 2. Run the test. await use(); @@ -280,10 +282,13 @@ export const test = _baseTest.extend({ }; // 4. Cleanup instrumentation. - const leftoverContexts = Array.from((_browserType as any)._contexts) as BrowserContext[]; - (_browserType as any)._onDidCreateContext = undefined; - (_browserType as any)._onWillCloseContext = undefined; - (_browserType as any)._defaultContextOptions = undefined; + const leftoverContexts: BrowserContext[] = []; + for (const browserType of [chromium, firefox, webkit]) { + leftoverContexts.push(...(browserType as any)._contexts); + (browserType as any)._onDidCreateContext = undefined; + (browserType as any)._onWillCloseContext = undefined; + (browserType as any)._defaultContextOptions = undefined; + } leftoverContexts.forEach(context => (context as any)._instrumentation.removeAllListeners()); // 5. Collect artifacts from any non-closed contexts. diff --git a/tests/playwright-test/playwright.spec.ts b/tests/playwright-test/playwright.spec.ts index ad58a06e20..80976b98e0 100644 --- a/tests/playwright-test/playwright.spec.ts +++ b/tests/playwright-test/playwright.spec.ts @@ -213,11 +213,20 @@ test('should respect context options in various contexts', async ({ runInlineTes await context.close(); rimraf.sync(dir); }); + + test('another browser', async ({ playwright, browserName }) => { + const browser = await playwright.webkit.launch(); + const page = await browser.newPage(); + + expect(await page.evaluate(() => navigator.language)).toBe('fr-CH'); + + await browser.close(); + }); `, }, { workers: 1 }); expect(result.exitCode).toBe(0); - expect(result.passed).toBe(4); + expect(result.passed).toBe(5); }); test('should call logger from launchOptions config', async ({ runInlineTest }, testInfo) => {