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
test: use checkInstalledSoftwareOnDisk for itest (#34103)
Max Schmitt61ce37cd53c2
diff --git a/tests/installation/npmTest.ts b/tests/installation/npmTest.tsindex ab2b9b2a88..4801f967e8 100644--- a/tests/installation/npmTest.ts+++ b/tests/installation/npmTest.ts@@ -75,7 +75,7 @@ type NPMTestFixtures = {_auto: void;_browsersPath: string;tmpWorkspace: string;- installedSoftwareOnDisk: () => Promise<string[]>;+ checkInstalledSoftwareOnDisk: (browsers: string[]) => Promise<void>;writeFiles: (nameToContents: Record<string, string>) => Promise<void>;exec: (cmd: string, ...argsAndOrOptions: ArgsOrOptions) => Promise<string>;tsc: (args: string) => Promise<string>;@@ -146,10 +146,13 @@ export const test = _testawait use(registry);await registry.shutdown();},- installedSoftwareOnDisk: async ({ isolateBrowsers, _browsersPath }, use) => {- if (!isolateBrowsers)- throw new Error(`Test that checks browser installation must set "isolateBrowsers" to true`);- await use(async () => fs.promises.readdir(_browsersPath).catch(() => []).then(files => files.map(f => f.split('-')[0].replace(/_/g, '-')).filter(f => !f.startsWith('.'))));+ checkInstalledSoftwareOnDisk: async ({ isolateBrowsers, _browsersPath }, use) => {+ await use(async expected => {+ if (!isolateBrowsers)+ throw new Error(`Test that checks browser installation must set "isolateBrowsers" to true`);+ const actual = await fs.promises.readdir(_browsersPath).catch(() => []).then(files => files.map(f => f.split('-')[0].replace(/_/g, '-')).filter(f => !f.startsWith('.')));+ expect(new Set(actual)).toEqual(new Set(expected));+ });},exec: async ({ tmpWorkspace, _browsersPath, isolateBrowsers }, use, testInfo) => {await use(async (cmd: string, ...argsAndOrOptions: [] | [...string[]] | [...string[], ExecOptions] | [ExecOptions]) => {diff --git a/tests/installation/npx-global.spec.ts b/tests/installation/npx-global.spec.tsindex 6a3da572ef..0df4d4829b 100755--- a/tests/installation/npx-global.spec.ts+++ b/tests/installation/npx-global.spec.ts@@ -17,26 +17,26 @@ import { test, expect } from './npmTest';test.use({ isolateBrowsers: true, allowGlobalInstall: true });-test('npx playwright --help should not download browsers', async ({ exec, installedSoftwareOnDisk }) => {+test('npx playwright --help should not download browsers', async ({ exec, checkInstalledSoftwareOnDisk }) => {const result = await exec('npx playwright --help');expect(result).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);expect(result).not.toContain(`To avoid unexpected behavior, please install your dependencies first`);});-test('npx playwright codegen', async ({ exec, installedSoftwareOnDisk }) => {+test('npx playwright codegen', async ({ exec, checkInstalledSoftwareOnDisk }) => {const stdio = await exec('npx playwright codegen', { expectToExitWithError: true });expect(stdio).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);expect(stdio).toContain(`Please run the following command to download new browsers`);});-test('npx playwright install global', async ({ exec, installedSoftwareOnDisk }) => {+test('npx playwright install global', async ({ exec, checkInstalledSoftwareOnDisk }) => {test.skip(process.platform === 'win32', 'isLikelyNpxGlobal() does not work in this setup on our bots');const result = await exec('npx playwright install');expect(result).toHaveLoggedSoftwareDownload(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);expect(result).not.toContain(`Please run the following command to download new browsers`);expect(result).toContain(`To avoid unexpected behavior, please install your dependencies first`);});diff --git a/tests/installation/playwright-cdn.spec.ts b/tests/installation/playwright-cdn.spec.tsindex 3b472625e9..9b74345025 100644--- a/tests/installation/playwright-cdn.spec.ts+++ b/tests/installation/playwright-cdn.spec.ts@@ -38,11 +38,11 @@ const parsedDownloads = (rawLogs: string) => {test.use({ isolateBrowsers: true });for (const cdn of CDNS) {- test(`playwright cdn failover should work (${cdn})`, async ({ exec, installedSoftwareOnDisk }) => {+ test(`playwright cdn failover should work (${cdn})`, async ({ exec, checkInstalledSoftwareOnDisk }) => {await exec('npm i playwright');const result = await exec('npx playwright install', { env: { PW_TEST_CDN_THAT_SHOULD_WORK: cdn, DEBUG: 'pw:install' } });expect(result).toHaveLoggedSoftwareDownload(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);const dls = parsedDownloads(result);for (const software of ['chromium', 'ffmpeg', 'firefox', 'webkit'])expect(dls).toContainEqual({ status: 200, name: software, url: expect.stringContaining(cdn) });diff --git a/tests/installation/playwright-cli-install-should-work.spec.ts b/tests/installation/playwright-cli-install-should-work.spec.tsindex d1a8bd3d04..064568f3e2 100755--- a/tests/installation/playwright-cli-install-should-work.spec.ts+++ b/tests/installation/playwright-cli-install-should-work.spec.ts@@ -19,19 +19,19 @@ import path from 'path';test.use({ isolateBrowsers: true });-test('install command should work', async ({ exec, installedSoftwareOnDisk }) => {+test('install command should work', async ({ exec, checkInstalledSoftwareOnDisk }) => {await exec('npm i playwright');await test.step('playwright install chromium', async () => {const result = await exec('npx playwright install chromium');expect(result).toHaveLoggedSoftwareDownload(['chromium', 'chromium-headless-shell', 'ffmpeg']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg']);});await test.step('playwright install', async () => {const result = await exec('npx playwright install');expect(result).toHaveLoggedSoftwareDownload(['firefox', 'webkit']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);});await exec('node sanity.js playwright', { env: { PLAYWRIGHT_BROWSERS_PATH: '0' } });@@ -48,12 +48,12 @@ test('install command should work', async ({ exec, installedSoftwareOnDisk }) =>}});-test('should be able to remove browsers', async ({ exec, installedSoftwareOnDisk }) => {+test('should be able to remove browsers', async ({ exec, checkInstalledSoftwareOnDisk }) => {await exec('npm i playwright');await exec('npx playwright install chromium');- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg']);await exec('npx playwright uninstall');- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);});test('should print the right install command without browsers', async ({ exec }) => {@@ -91,7 +91,7 @@ test('subsequent installs works', async ({ exec }) => {await exec('node --unhandled-rejections=strict', path.join('node_modules', '@playwright', 'browser-chromium', 'install.js'));});-test('install playwright-chromium should work', async ({ exec, installedSoftwareOnDisk }) => {+test('install playwright-chromium should work', async ({ exec }) => {await exec('npm i playwright-chromium');await exec('npx playwright install chromium');await exec('node sanity.js playwright-chromium chromium');diff --git a/tests/installation/playwright-packages-install-behavior.spec.ts b/tests/installation/playwright-packages-install-behavior.spec.tsindex 9975abce99..9ace1eaa67 100755--- a/tests/installation/playwright-packages-install-behavior.spec.ts+++ b/tests/installation/playwright-packages-install-behavior.spec.ts@@ -19,7 +19,7 @@ import { test, expect } from './npmTest';test.use({ isolateBrowsers: true });for (const browser of ['chromium', 'firefox', 'webkit']) {- test(`playwright-${browser} should work`, async ({ exec, installedSoftwareOnDisk }) => {+ test(`playwright-${browser} should work`, async ({ exec, checkInstalledSoftwareOnDisk }) => {const pkg = `playwright-${browser}`;const result = await exec('npm i --foreground-scripts', pkg);const browserName = pkg.split('-')[1];@@ -27,7 +27,7 @@ for (const browser of ['chromium', 'firefox', 'webkit']) {if (browserName === 'chromium')expectedSoftware.push('chromium-headless-shell', 'ffmpeg');expect(result).toHaveLoggedSoftwareDownload(expectedSoftware as any);- expect(await installedSoftwareOnDisk()).toEqual(expectedSoftware);+ await checkInstalledSoftwareOnDisk(expectedSoftware);expect(result).not.toContain(`To avoid unexpected behavior, please install your dependencies first`);await exec('node sanity.js', pkg, browser);await exec('node', `esm-${pkg}.mjs`);@@ -35,7 +35,7 @@ for (const browser of ['chromium', 'firefox', 'webkit']) {}for (const browser of ['chromium', 'firefox', 'webkit']) {- test(`@playwright/browser-${browser} should work`, async ({ exec, installedSoftwareOnDisk }) => {+ test(`@playwright/browser-${browser} should work`, async ({ exec, checkInstalledSoftwareOnDisk }) => {const pkg = `@playwright/browser-${browser}`;const expectedSoftware = [browser];if (browser === 'chromium')@@ -43,67 +43,67 @@ for (const browser of ['chromium', 'firefox', 'webkit']) {const result1 = await exec('npm i --foreground-scripts', pkg);expect(result1).toHaveLoggedSoftwareDownload(expectedSoftware as any);- expect(await installedSoftwareOnDisk()).toEqual(expectedSoftware);+ await checkInstalledSoftwareOnDisk(expectedSoftware);expect(result1).not.toContain(`To avoid unexpected behavior, please install your dependencies first`);const result2 = await exec('npm i --foreground-scripts playwright');expect(result2).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual(expectedSoftware);+ await checkInstalledSoftwareOnDisk(expectedSoftware);await exec('node sanity.js playwright', browser);await exec('node browser-only.js', pkg);});}-test(`playwright-core should work`, async ({ exec, installedSoftwareOnDisk }) => {+test(`playwright-core should work`, async ({ exec, checkInstalledSoftwareOnDisk }) => {const result1 = await exec('npm i --foreground-scripts playwright-core');expect(result1).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);const stdio = await exec('npx playwright-core', 'test', '-c', '.', { expectToExitWithError: true });expect(stdio).toContain(`Please install @playwright/test package`);});-test(`playwright should work`, async ({ exec, installedSoftwareOnDisk }) => {+test(`playwright should work`, async ({ exec, checkInstalledSoftwareOnDisk }) => {const result1 = await exec('npm i --foreground-scripts playwright');expect(result1).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);const result2 = await exec('npx playwright install');expect(result2).toHaveLoggedSoftwareDownload(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);await exec('node sanity.js playwright chromium firefox webkit');await exec('node esm-playwright.mjs');});-test(`playwright should work with chromium --no-shell`, async ({ exec, installedSoftwareOnDisk }) => {+test(`playwright should work with chromium --no-shell`, async ({ exec, checkInstalledSoftwareOnDisk }) => {const result1 = await exec('npm i --foreground-scripts playwright');expect(result1).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);const result2 = await exec('npx playwright install chromium --no-shell');expect(result2).toHaveLoggedSoftwareDownload(['chromium', 'ffmpeg']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg']);+ await checkInstalledSoftwareOnDisk(['chromium', 'ffmpeg']);});-test(`playwright should work with chromium --only-shell`, async ({ exec, installedSoftwareOnDisk }) => {+test(`playwright should work with chromium --only-shell`, async ({ exec, checkInstalledSoftwareOnDisk }) => {const result1 = await exec('npm i --foreground-scripts playwright');expect(result1).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);const result2 = await exec('npx playwright install --only-shell');expect(result2).toHaveLoggedSoftwareDownload(['chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);+ await checkInstalledSoftwareOnDisk(['chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);});-test('@playwright/test should work', async ({ exec, installedSoftwareOnDisk }) => {+test('@playwright/test should work', async ({ exec, checkInstalledSoftwareOnDisk }) => {const result1 = await exec('npm i --foreground-scripts @playwright/test');expect(result1).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);await exec('npx playwright test -c . sample.spec.js', { expectToExitWithError: true, message: 'should not be able to run tests without installing browsers' });const result2 = await exec('npx playwright install');expect(result2).toHaveLoggedSoftwareDownload(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);- expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);+ await checkInstalledSoftwareOnDisk(['chromium', 'chromium-headless-shell', 'ffmpeg', 'firefox', 'webkit']);await exec('node sanity.js @playwright/test chromium firefox webkit');await exec('node', 'esm-playwright-test.mjs');diff --git a/tests/installation/skip-browser-download.spec.ts b/tests/installation/skip-browser-download.spec.tsindex 7cf9cdf602..21d4443dca 100755--- a/tests/installation/skip-browser-download.spec.ts+++ b/tests/installation/skip-browser-download.spec.ts@@ -17,10 +17,10 @@ import { test, expect } from './npmTest';test.use({ isolateBrowsers: true });-test('should skip browser installs', async ({ exec, installedSoftwareOnDisk }) => {+test('should skip browser installs', async ({ exec, checkInstalledSoftwareOnDisk }) => {const result = await exec('npm i --foreground-scripts playwright @playwright/browser-firefox', { env: { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' } });expect(result).toHaveLoggedSoftwareDownload([]);- expect(await installedSoftwareOnDisk()).toEqual([]);+ await checkInstalledSoftwareOnDisk([]);expect(result).toContain(`Skipping browsers download because`);if (process.platform === 'linux') {