Skip to content

Commit

Permalink
test: use checkInstalledSoftwareOnDisk for itest
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Dec 19, 2024
1 parent 613f6c5 commit 38521da
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
13 changes: 8 additions & 5 deletions tests/installation/npmTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -146,10 +146,13 @@ export const test = _test
await 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]) => {
Expand Down
12 changes: 6 additions & 6 deletions tests/installation/npx-global.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
4 changes: 2 additions & 2 deletions tests/installation/playwright-cdn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
Expand Down
14 changes: 7 additions & 7 deletions tests/installation/playwright-cli-install-should-work.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' } });
Expand All @@ -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 }) => {
Expand Down Expand Up @@ -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');
Expand Down
38 changes: 19 additions & 19 deletions tests/installation/playwright-packages-install-behavior.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,91 +19,91 @@ 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];
const expectedSoftware = [browserName];
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`);
});
}

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')
expectedSoftware.push('chromium-headless-shell', 'ffmpeg');

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');
Expand Down
4 changes: 2 additions & 2 deletions tests/installation/skip-browser-download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit 38521da

Please sign in to comment.