test: speed up installation tests by not isolating browser unless required (#27370)

This commit is contained in:
Dmitry Gozman 2023-10-02 11:26:08 -07:00 committed by GitHub
parent d1ae0ab94f
commit dfc75a1e1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
34 changed files with 225 additions and 555 deletions

View File

@ -234,7 +234,7 @@ jobs:
env:
DEBUG: pw:install
- run: npm run build
- run: npx playwright install-deps
- run: npx playwright install --with-deps
- run: npm install -g yarn@1
- run: npm install -g pnpm@8
- run: npm run itest

View File

@ -136,7 +136,7 @@ jobs:
env:
DEBUG: pw:install
- run: npm run build
- run: npx playwright install-deps
- run: npx playwright install --with-deps
- run: npm install -g yarn@1
- run: npm install -g pnpm@8
- run: npm run itest

View File

@ -1,25 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test } from './npmTest';
test('android types', async ({ exec, tsc, writeFiles }) => {
await exec('npm i --foreground-scripts playwright');
await writeFiles({
'test.ts':
`import { AndroidDevice, _android, AndroidWebView, Page } from 'playwright';`,
});
await tsc('test.ts');
});

View File

@ -20,7 +20,7 @@ import { test } from './npmTest';
test('connect to selenium', async ({ exec, tmpWorkspace }, testInfo) => {
test.skip(os.platform() !== 'linux');
await exec('npm i --foreground-scripts playwright-core');
await exec('npm i playwright-core');
await exec(`node download-chromedriver.js ${tmpWorkspace}`);
const seleniumPath = path.join(tmpWorkspace, 'selenium');
await exec(`node download-selenium.js ${seleniumPath}`);

View File

@ -16,7 +16,7 @@
import { test, expect } from './npmTest';
test('driver should work', async ({ exec }) => {
await exec('npm i --foreground-scripts playwright');
await exec('npm i playwright');
await exec('npx playwright install');
await exec('node driver-client.js');
});
@ -24,7 +24,7 @@ test('driver should work', async ({ exec }) => {
test('driver should ignore SIGINT', async ({ exec }) => {
test.skip(process.platform === 'win32', 'Only relevant for POSIX');
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright-python/issues/1843' });
await exec('npm i --foreground-scripts playwright');
await exec('npm i playwright');
await exec('npx playwright install chromium');
const output = await exec('node driver-client-sigint.js');
const lines = output.split('\n').filter(l => l.trim());

View File

@ -1,25 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test } from './npmTest';
test('electron types', async ({ exec, tsc, writeFiles }) => {
await exec('npm i --foreground-scripts playwright electron@12');
await writeFiles({
'test.ts':
`import { Page, _electron, ElectronApplication, Electron } from 'playwright';`
});
await tsc('test.ts');
});

View File

@ -14,22 +14,13 @@
* limitations under the License.
*/
import http from 'http';
import https from 'https';
import url from 'url';
import type net from 'net';
import debugLogger from 'debug';
import path from 'path';
import fs from 'fs';
import { spawnAsync } from '../../packages/playwright-core/lib/utils/spawnAsync';
import { rimraf } from 'playwright-core/lib/utilsBundle';
import { TMP_WORKSPACES } from './npmTest';
import { createHttpServer } from '../../packages/playwright-core/lib/utils/network';
import { calculateSha1 } from '../../packages/playwright-core/lib/utils/crypto';
const PACKAGE_BUILDER_SCRIPT = path.join(__dirname, '..', '..', 'utils', 'pack_package.js');
const BROWSERS_CACHE_DIR = path.join(TMP_WORKSPACES, 'npm-test-browsers-cache');
const debug = debugLogger('itest');
async function globalSetup() {
await rimraf(TMP_WORKSPACES);
@ -83,61 +74,6 @@ async function globalSetup() {
await fs.promises.writeFile(path.join(__dirname, '.registry.json'), JSON.stringify(Object.fromEntries(builds)));
}
const cdnProxyServer = createHttpServer(async (request: http.IncomingMessage, response: http.ServerResponse) => {
const requestedPath = url.parse(request.url!).path;
const cachedPath = path.join(BROWSERS_CACHE_DIR, calculateSha1(requestedPath));
const cachedPathMetaInfo = cachedPath + '.metainfo';
if (!fs.existsSync(cachedPath)) {
const realUrl = 'https://playwright.azureedge.net' + requestedPath;
debug(`[cdn proxy] downloading ${realUrl} headers=${JSON.stringify(request.headers)}`);
const headers = { ...request.headers };
delete headers['host'];
const options = {
...url.parse(realUrl),
method: request.method,
headers,
};
const factory = options.protocol === 'https:' ? https : http;
let doneCallback = () => {};
const donePromise = new Promise<void>(f => doneCallback = () => {
debug(`[cdn proxy] downloading ${realUrl} finished`);
f();
});
const realRequest = factory.request(options, (realResponse: http.IncomingMessage) => {
const metaInfo = {
statusCode: realResponse.statusCode,
statusMessage: realResponse.statusMessage || '',
headers: realResponse.headers || {},
};
debug(`[cdn proxy] downloading ${realUrl} statusCode=${realResponse.statusCode}`);
fs.mkdirSync(path.dirname(cachedPathMetaInfo), { recursive: true });
fs.writeFileSync(cachedPathMetaInfo, JSON.stringify(metaInfo));
realResponse.pipe(fs.createWriteStream(cachedPath, { highWaterMark: 1024 * 1024 })).on('close', doneCallback).on('finish', doneCallback).on('error', doneCallback);
});
request.pipe(realRequest);
await donePromise;
}
const metaInfo = JSON.parse(fs.readFileSync(cachedPathMetaInfo, 'utf-8'));
response.writeHead(metaInfo.statusCode, metaInfo.statusMessage, metaInfo.headers);
const done = () => {
debug(`[cdn proxy] serving ${request.url!} finished`);
response.end();
};
fs.createReadStream(cachedPath, { highWaterMark: 1024 * 1024 }).pipe(response).on('close', done).on('error', done);
debug(`[cdn proxy] serving ${request.url!} from cached ${cachedPath}`);
});
cdnProxyServer.listen(0);
await new Promise(f => cdnProxyServer.once('listening', f));
process.env.CDN_PROXY_HOST = `http://127.0.0.1:${(cdnProxyServer.address() as net.AddressInfo).port}`;
console.log('Stared CDN proxy at ' + process.env.CDN_PROXY_HOST);
return async () => {
await new Promise(f => cdnProxyServer.close(f));
};
}
export default globalSetup;

View File

@ -61,15 +61,15 @@ type ExecOptions = SpawnOptions & { message?: string, expectToExitWithError?: bo
type ArgsOrOptions = [] | [...string[]] | [...string[], ExecOptions] | [ExecOptions];
type NPMTestOptions = {
useRealCDN: boolean;
isolateBrowsers: boolean;
allowGlobalInstall: boolean;
};
type NPMTestFixtures = {
_auto: void;
_browsersPath: string;
tmpWorkspace: string;
installedSoftwareOnDisk: (registryPath?: string) => Promise<string[]>;
writeConfig: (allowGlobal: boolean) => Promise<void>;
installedSoftwareOnDisk: () => Promise<string[]>;
writeFiles: (nameToContents: Record<string, string>) => Promise<void>;
exec: (cmd: string, ...argsAndOrOptions: ArgsOrOptions) => Promise<string>;
tsc: (args: string) => Promise<string>;
@ -79,9 +79,10 @@ type NPMTestFixtures = {
export const test = _test
.extend<CommonFixtures, CommonWorkerFixtures>(commonFixtures)
.extend<NPMTestFixtures & NPMTestOptions>({
useRealCDN: [false, { option: true }],
isolateBrowsers: [false, { option: true }],
allowGlobalInstall: [false, { option: true }],
_browsersPath: async ({ tmpWorkspace }, use) => use(path.join(tmpWorkspace, 'browsers')),
_auto: [async ({ tmpWorkspace, exec, _browsersPath, writeConfig }, use) => {
_auto: [async ({ tmpWorkspace, exec, _browsersPath, registry, allowGlobalInstall }, use, testInfo) => {
await exec('npm init -y');
const sourceDir = path.join(__dirname, 'fixture-scripts');
const contents = await fs.promises.readdir(sourceDir);
@ -93,7 +94,20 @@ export const test = _test
packageJSON.pnpm = { overrides: prefixed };
await fs.promises.writeFile(path.join(tmpWorkspace, 'package.json'), JSON.stringify(packageJSON, null, 2));
await writeConfig(false);
const yarnLines = [
`registry "${registry.url()}/"`,
`cache "${testInfo.outputPath('npm_cache')}"`,
];
const npmLines = [
`registry = ${registry.url()}/`,
`cache = ${testInfo.outputPath('npm_cache')}`,
];
if (!allowGlobalInstall) {
yarnLines.push(`prefix "${testInfo.outputPath('npm_global')}"`);
npmLines.push(`prefix = ${testInfo.outputPath('npm_global')}`);
}
await fs.promises.writeFile(path.join(tmpWorkspace, '.yarnrc'), yarnLines.join('\n'), 'utf-8');
await fs.promises.writeFile(path.join(tmpWorkspace, '.npmrc'), npmLines.join('\n'), 'utf-8');
await use();
if (test.info().status === test.info().expectedStatus) {
@ -124,28 +138,12 @@ export const test = _test
await use(registry);
await registry.shutdown();
},
writeConfig: async ({ tmpWorkspace, registry }, use, testInfo) => {
await use(async (allowGlobal: boolean) => {
const yarnLines = [
`registry "${registry.url()}/"`,
`cache "${testInfo.outputPath('npm_cache')}"`,
];
const npmLines = [
`registry = ${registry.url()}/`,
`cache = ${testInfo.outputPath('npm_cache')}`,
];
if (!allowGlobal) {
yarnLines.push(`prefix "${testInfo.outputPath('npm_global')}"`);
npmLines.push(`prefix = ${testInfo.outputPath('npm_global')}`);
}
await fs.promises.writeFile(path.join(tmpWorkspace, '.yarnrc'), yarnLines.join('\n'), 'utf-8');
await fs.promises.writeFile(path.join(tmpWorkspace, '.npmrc'), npmLines.join('\n'), 'utf-8');
});
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]).filter(f => !f.startsWith('.'))));
},
installedSoftwareOnDisk: async ({ _browsersPath }, use) => {
await use(async (registryPath?: string) => fs.promises.readdir(registryPath || _browsersPath).catch(() => []).then(files => files.map(f => f.split('-')[0]).filter(f => !f.startsWith('.'))));
},
exec: async ({ useRealCDN, tmpWorkspace, _browsersPath }, use, testInfo) => {
exec: async ({ tmpWorkspace, _browsersPath, isolateBrowsers }, use, testInfo) => {
await use(async (cmd: string, ...argsAndOrOptions: [] | [...string[]] | [...string[], ExecOptions] | [ExecOptions]) => {
let args: string[] = [];
let options: ExecOptions = {};
@ -164,8 +162,7 @@ export const test = _test
'PATH': process.env.PATH,
'DISPLAY': process.env.DISPLAY,
'XAUTHORITY': process.env.XAUTHORITY,
'PLAYWRIGHT_BROWSERS_PATH': _browsersPath,
...(useRealCDN ? {} : { PLAYWRIGHT_DOWNLOAD_HOST: process.env.CDN_PROXY_HOST }),
...(isolateBrowsers ? { PLAYWRIGHT_BROWSERS_PATH: _browsersPath } : {}),
...options.env,
}
});
@ -198,7 +195,7 @@ export const test = _test
});
},
tsc: async ({ exec }, use) => {
await exec('npm i --foreground-scripts typescript@5.2.2 @types/node@16');
await exec('npm i typescript@5.2.2 @types/node@16');
await use((args: string) => exec('npx', 'tsc', args, { shell: process.platform === 'win32' }));
},
});

View File

@ -1,24 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('npx playwright --help should not download browsers', async ({ writeConfig, exec, installedSoftwareOnDisk }) => {
await writeConfig(true);
const result = await exec('npx playwright --help');
expect(result).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
expect(result).not.toContain(`To avoid unexpected behavior, please install your dependencies first`);
});

View File

@ -1,24 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('npx playwright codegen', async ({ writeConfig, exec, installedSoftwareOnDisk }) => {
await writeConfig(true);
const stdio = await exec('npx playwright codegen', { expectToExitWithError: true });
expect(stdio).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
expect(stdio).toContain(`Please run the following command to download new browsers`);
});

View File

@ -15,10 +15,25 @@
*/
import { test, expect } from './npmTest';
test('npx playwright install global', async ({ writeConfig, exec, installedSoftwareOnDisk }) => {
test.use({ isolateBrowsers: true, allowGlobalInstall: true });
test('npx playwright --help should not download browsers', async ({ exec, installedSoftwareOnDisk }) => {
const result = await exec('npx playwright --help');
expect(result).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
expect(result).not.toContain(`To avoid unexpected behavior, please install your dependencies first`);
});
test('npx playwright codegen', async ({ exec, installedSoftwareOnDisk }) => {
const stdio = await exec('npx playwright codegen', { expectToExitWithError: true });
expect(stdio).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
expect(stdio).toContain(`Please run the following command to download new browsers`);
});
test('npx playwright install global', async ({ exec, installedSoftwareOnDisk }) => {
test.skip(process.platform === 'win32', 'isLikelyNpxGlobal() does not work in this setup on our bots');
await writeConfig(true);
const result = await exec('npx playwright install');
expect(result).toHaveLoggedSoftwareDownload(['chromium', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);

View File

@ -1,39 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import http from 'http';
import type { AddressInfo } from 'net';
import { test, expect } from './npmTest';
test.use({ useRealCDN: true });
test(`playwright cdn should race with a timeout`, async ({ exec }) => {
const server = http.createServer(() => {});
await new Promise<void>(resolve => server.listen(0, resolve));
try {
await exec('npm i --foreground-scripts playwright');
const result = await exec('npx playwright install', {
env: {
PLAYWRIGHT_DOWNLOAD_HOST: `http://127.0.0.1:${(server.address() as AddressInfo).port}`,
DEBUG: 'pw:install',
PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT: '1000',
},
expectToExitWithError: true
});
expect(result).toContain(`timed out after 1000ms`);
} finally {
await new Promise(resolve => server.close(resolve));
}
});

View File

@ -14,6 +14,8 @@
* limitations under the License.
*/
import { test, expect } from './npmTest';
import http from 'http';
import type { AddressInfo } from 'net';
const CDNS = [
'https://playwright.azureedge.net',
@ -32,11 +34,11 @@ const parsedDownloads = (rawLogs: string) => {
return out;
};
test.use({ useRealCDN: true });
test.use({ isolateBrowsers: true });
for (const cdn of CDNS) {
test(`playwright cdn failover should work (${cdn})`, async ({ exec, installedSoftwareOnDisk }) => {
await exec('npm i --foreground-scripts playwright');
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', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
@ -47,3 +49,22 @@ for (const cdn of CDNS) {
await exec('node esm-playwright.mjs');
});
}
test(`playwright cdn should race with a timeout`, async ({ exec }) => {
const server = http.createServer(() => {});
await new Promise<void>(resolve => server.listen(0, resolve));
try {
await exec('npm i playwright');
const result = await exec('npx playwright install', {
env: {
PLAYWRIGHT_DOWNLOAD_HOST: `http://127.0.0.1:${(server.address() as AddressInfo).port}`,
DEBUG: 'pw:install',
PLAYWRIGHT_DOWNLOAD_CONNECTION_TIMEOUT: '1000',
},
expectToExitWithError: true
});
expect(result).toContain(`timed out after 1000ms`);
} finally {
await new Promise(resolve => server.close(resolve));
}
});

View File

@ -1,39 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('codegen should print the right install command without browsers', async ({ exec }) => {
await exec('npm i --foreground-scripts playwright');
const pwLangName2InstallCommand = {
'java': 'mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"',
'python': 'playwright install',
'csharp': 'pwsh bin/Debug/netX/playwright.ps1 install',
'': 'npx playwright install',
};
for (const [langName, installCommand] of Object.entries(pwLangName2InstallCommand)) {
await test.step(`codegen should work for ${langName}`, async () => {
const result = await exec('npx playwright codegen', {
expectToExitWithError: true,
env: {
PW_LANG_NAME: langName,
}
});
expect(result).toContain(installCommand);
});
}
});

View File

@ -14,9 +14,12 @@
* limitations under the License.
*/
import { test, expect } from './npmTest';
import path from 'path';
test.use({ isolateBrowsers: true });
test('install command should work', async ({ exec, installedSoftwareOnDisk }) => {
await exec('npm i --foreground-scripts playwright', { env: { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' } });
await exec('npm i playwright');
await test.step('playwright install chromium', async () => {
const result = await exec('npx playwright install chromium');
@ -30,14 +33,59 @@ test('install command should work', async ({ exec, installedSoftwareOnDisk }) =>
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
});
await exec('node sanity.js playwright', { env: { PLAYWRIGHT_BROWSERS_PATH: undefined } });
await exec('node sanity.js playwright', { env: { PLAYWRIGHT_BROWSERS_PATH: '0' } });
await exec('node sanity.js playwright chromium firefox webkit');
const packages = ['playwright-chromium', 'playwright-firefox', 'playwright-webkit', '@playwright/browser-chromium', '@playwright/browser-firefox', '@playwright/browser-webkit'];
for (const pkg of packages) {
await test.step(pkg, async () => {
const result = await exec(`npm i ${pkg}`);
expect(result).toHaveLoggedSoftwareDownload([]);
if (!pkg.includes('@playwright/browser-'))
await exec('node sanity.js', pkg, 'chromium firefox webkit');
});
}
});
test('should be able to remove browsers', async ({ exec, installedSoftwareOnDisk }) => {
await exec('npm i --foreground-scripts playwright');
await exec('npm i playwright');
await exec('npx playwright install chromium');
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg']);
await exec('npx playwright uninstall');
expect(await installedSoftwareOnDisk()).toEqual([]);
});
test('should print the right install command without browsers', async ({ exec }) => {
await exec('npm i playwright');
const pwLangName2InstallCommand = {
'java': 'mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args="install"',
'python': 'playwright install',
'csharp': 'pwsh bin/Debug/netX/playwright.ps1 install',
'': 'npx playwright install',
};
for (const [langName, installCommand] of Object.entries(pwLangName2InstallCommand)) {
await test.step(`codegen should work for ${langName}`, async () => {
const result = await exec('npx playwright codegen', {
expectToExitWithError: true,
env: {
PW_LANG_NAME: langName,
}
});
expect(result).toContain(installCommand);
});
}
});
test('subsequent installs works', async ({ exec }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/1651' });
await exec('npm i @playwright/browser-chromium');
// Note: the `npm install` would not actually crash, the error
// is merely logged to the console. To reproduce the error, we should make
// sure that script's install.js can be run subsequently without unhandled promise rejections.
// Note: the flag `--unhandled-rejections=strict` will force node to terminate in case
// of UnhandledPromiseRejection.
await exec('node --unhandled-rejections=strict', path.join('node_modules', '@playwright', 'browser-chromium', 'install.js'));
});

View File

@ -1,28 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test } from './npmTest';
import fs from 'fs';
import path from 'path';
test('playwright cli screenshot should work', async ({ exec, tmpWorkspace }) => {
await exec('npm i --foreground-scripts playwright');
await exec('npx playwright install chromium');
await exec(path.join('node_modules', '.bin', 'playwright'), 'screenshot about:blank one.png');
await fs.promises.stat(path.join(tmpWorkspace, 'one.png'));
await exec('npx playwright screenshot about:blank two.png');
await fs.promises.stat(path.join(tmpWorkspace, 'two.png'));
});

View File

@ -14,9 +14,11 @@
* limitations under the License.
*/
import { test, expect } from './npmTest';
import path from 'path';
import fs from 'fs';
test('codegen should work', async ({ exec }) => {
await exec('npm i --foreground-scripts playwright');
test('cli should work', async ({ exec, tmpWorkspace }) => {
await exec('npm i playwright');
await exec('npx playwright install chromium');
await test.step('codegen without arguments', async () => {
@ -48,4 +50,12 @@ test('codegen should work', async ({ exec }) => {
});
expect(result).toContain(`browser.close`);
});
await test.step('screenshot', async () => {
await exec(path.join('node_modules', '.bin', 'playwright'), 'screenshot about:blank one.png');
await fs.promises.stat(path.join(tmpWorkspace, 'one.png'));
await exec('npx playwright screenshot about:blank two.png');
await fs.promises.stat(path.join(tmpWorkspace, 'two.png'));
});
});

View File

@ -1,24 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test(`playwright-core should work`, async ({ exec, installedSoftwareOnDisk }) => {
const result1 = await exec('npm i --foreground-scripts playwright-core');
expect(result1).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
const stdio = await exec('npx playwright-core', 'test', '-c', '.', { expectToExitWithError: true });
expect(stdio).toContain(`Please install @playwright/test package`);
});

View File

@ -15,7 +15,12 @@
*/
import { test } from './npmTest';
test('electron should work', async ({ exec }) => {
await exec('npm i --foreground-scripts playwright electron@19.0.11');
test('electron should work', async ({ exec, tsc, writeFiles }) => {
await exec('npm i playwright electron@19.0.11');
await exec('node sanity-electron.js');
await writeFiles({
'test.ts':
`import { Page, _electron, ElectronApplication, Electron } from 'playwright';`
});
await tsc('test.ts');
});

View File

@ -1,28 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('global installation cross package', async ({ exec, installedSoftwareOnDisk }) => {
const packages = ['playwright-chromium', 'playwright-firefox', 'playwright-webkit'];
for (const pkg of packages)
await exec('npm i --foreground-scripts', pkg, { env: { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' } });
const result = await exec('npx playwright install');
expect(result).toHaveLoggedSoftwareDownload(['chromium', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
for (const pkg of packages)
await test.step(pkg, () => exec('node sanity.js', pkg, 'chromium firefox webkit'));
});

View File

@ -1,29 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('global installation', async ({ exec, installedSoftwareOnDisk }) => {
const result1 = await exec('npm i --foreground-scripts playwright');
expect(result1).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
const result2 = await exec('npx playwright install');
expect(result2).toHaveLoggedSoftwareDownload(['chromium', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
await exec('node sanity.js playwright', { env: { PLAYWRIGHT_BROWSERS_PATH: undefined } });
await exec('node sanity.js playwright chromium firefox webkit');
});

View File

@ -1,28 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test } from './npmTest';
import path from 'path';
test('subsequent installs works', async ({ exec }) => {
test.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/1651' });
await exec('npm i --foreground-scripts @playwright/browser-chromium');
// Note: the `npm install` would not actually crash, the error
// is merely logged to the console. To reproduce the error, we should make
// sure that script's install.js can be run subsequently without unhandled promise rejections.
// Note: the flag `--unhandled-rejections=strict` will force node to terminate in case
// of UnhandledPromiseRejection.
await exec('node --unhandled-rejections=strict', path.join('node_modules', '@playwright', 'browser-chromium', 'install.js'));
});

View File

@ -16,6 +16,8 @@
*/
import { test, expect } from './npmTest';
test.use({ isolateBrowsers: true });
for (const browser of ['chromium', 'firefox', 'webkit']) {
test(`playwright-${browser} should work`, async ({ exec, installedSoftwareOnDisk }) => {
const pkg = `playwright-${browser}`;
@ -52,3 +54,46 @@ for (const browser of ['chromium', 'firefox', 'webkit']) {
await exec('node browser-only.js', pkg);
});
}
test(`playwright-core should work`, async ({ exec, installedSoftwareOnDisk }) => {
const result1 = await exec('npm i --foreground-scripts playwright-core');
expect(result1).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
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 }) => {
const result1 = await exec('npm i --foreground-scripts playwright');
expect(result1).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
const result2 = await exec('npx playwright install');
expect(result2).toHaveLoggedSoftwareDownload(['chromium', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
await exec('node sanity.js playwright chromium firefox webkit');
await exec('node esm-playwright.mjs');
});
test('@playwright/test should work', async ({ exec, installedSoftwareOnDisk }) => {
const result1 = await exec('npm i --foreground-scripts @playwright/test');
expect(result1).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
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', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
await exec('node sanity.js @playwright/test chromium firefox webkit');
await exec('node', 'esm-playwright-test.mjs');
const result3 = await exec('npx playwright test -c . --browser=all --reporter=list sample.spec.js');
expect(result3).toContain('3 passed');
const result4 = await exec('npx playwright test -c . failing.spec.js', { expectToExitWithError: true, env: { DEBUG: 'pw:api' } });
expect(result4).toContain('expect.toHaveText started');
expect(result4).toContain('failing.spec.js:5:38');
});

View File

@ -20,7 +20,7 @@ import path from 'path';
test('playwright should work with relative home path', async ({ exec, tmpWorkspace }) => {
await fs.promises.mkdir(path.join(tmpWorkspace, 'foo'));
// Make sure that browsers path is resolved relative to the `npm install` call location.
await exec('npm i --foreground-scripts playwright', { cwd: path.join(tmpWorkspace, 'foo'), env: { PLAYWRIGHT_BROWSERS_PATH: path.join('..', 'relative') } });
await exec('npm i playwright', { cwd: path.join(tmpWorkspace, 'foo'), env: { PLAYWRIGHT_BROWSERS_PATH: path.join('..', 'relative') } });
await exec('npx playwright install', { cwd: path.join(tmpWorkspace, 'foo'), env: { PLAYWRIGHT_BROWSERS_PATH: path.join('..', 'relative') } });
await exec('node sanity.js playwright chromium firefox webkit', { env: { PLAYWRIGHT_BROWSERS_PATH: path.join('.', 'relative') } });
});

View File

@ -20,7 +20,7 @@ test('playwright should work with relative home path', async ({ exec }) => {
test.skip(os.platform().startsWith('win'));
const env = { PLAYWRIGHT_BROWSERS_PATH: '0', HOME: '.' };
await exec('npm i --foreground-scripts playwright @playwright/browser-chromium @playwright/browser-webkit', { env });
await exec('npm i playwright @playwright/browser-chromium @playwright/browser-webkit', { env });
// Firefox does not work with relative HOME.
await exec('node sanity.js playwright chromium webkit', { env });
});

View File

@ -1,31 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test.use({ useRealCDN: true });
test(`playwright should work`, async ({ exec, installedSoftwareOnDisk }) => {
const result1 = await exec('npm i --foreground-scripts playwright');
expect(result1).toHaveLoggedSoftwareDownload([]);
expect(await installedSoftwareOnDisk()).toEqual([]);
const result2 = await exec('npm i --foreground-scripts @playwright/browser-chromium @playwright/browser-firefox @playwright/browser-webkit');
expect(result2).toHaveLoggedSoftwareDownload(['chromium', 'ffmpeg', 'firefox', 'webkit']);
expect(await installedSoftwareOnDisk()).toEqual(['chromium', 'ffmpeg', 'firefox', 'webkit']);
await exec('node sanity.js playwright chromium firefox webkit');
await exec('node esm-playwright.mjs');
});

View File

@ -17,9 +17,7 @@ import { test } from './npmTest';
import path from 'path';
test('npm: @playwright/test should work', async ({ exec, tmpWorkspace }) => {
await exec('npm i --foreground-scripts @playwright/test');
await exec('npx playwright test -c . sample.spec.js', { expectToExitWithError: true, message: 'should not be able to run tests without installing browsers' });
await exec('npm i @playwright/test');
await exec('npx playwright install');
await exec('npx playwright test -c . --browser=all --reporter=list,json sample.spec.js', { env: { PLAYWRIGHT_JSON_OUTPUT_NAME: 'report.json' } });
await exec('node read-json-report.js', path.join(tmpWorkspace, 'report.json'));
@ -28,8 +26,8 @@ test('npm: @playwright/test should work', async ({ exec, tmpWorkspace }) => {
});
test('npm: playwright + @playwright/test should work', async ({ exec, tmpWorkspace }) => {
await exec('npm i --foreground-scripts playwright');
await exec('npm i --foreground-scripts @playwright/test');
await exec('npm i playwright');
await exec('npm i @playwright/test');
await exec('npx playwright install');
await exec('npx playwright test -c . --browser=all --reporter=list,json sample.spec.js', { env: { PLAYWRIGHT_JSON_OUTPUT_NAME: 'report.json' } });
await exec('node read-json-report.js', path.join(tmpWorkspace, 'report.json'));
@ -38,8 +36,8 @@ test('npm: playwright + @playwright/test should work', async ({ exec, tmpWorkspa
});
test('npm: @playwright/test + playwright-core should work', async ({ exec, tmpWorkspace }) => {
await exec('npm i --foreground-scripts @playwright/test');
await exec('npm i --foreground-scripts playwright-core');
await exec('npm i @playwright/test');
await exec('npm i playwright-core');
await exec('npx playwright install');
await exec('npx playwright test -c . --browser=all --reporter=list,json sample.spec.js', { env: { PLAYWRIGHT_JSON_OUTPUT_NAME: 'report.json' } });
await exec('node read-json-report.js', path.join(tmpWorkspace, 'report.json'));
@ -49,8 +47,6 @@ test('npm: @playwright/test + playwright-core should work', async ({ exec, tmpWo
test('yarn: @playwright/test should work', async ({ exec, tmpWorkspace }) => {
await exec('yarn add @playwright/test');
await exec('yarn playwright test -c .', { expectToExitWithError: true, message: 'should not be able to run tests without installing browsers' });
await exec('yarn playwright install');
await exec('yarn playwright test -c . --browser=all --reporter=list,json sample.spec.js', { env: { PLAYWRIGHT_JSON_OUTPUT_NAME: 'report.json' } });
await exec('node read-json-report.js', path.join(tmpWorkspace, 'report.json'));
@ -60,7 +56,6 @@ test('yarn: @playwright/test should work', async ({ exec, tmpWorkspace }) => {
test('pnpm: @playwright/test should work', async ({ exec, tmpWorkspace }) => {
await exec('pnpm add @playwright/test');
await exec('pnpm exec playwright test -c .', { expectToExitWithError: true, message: 'should not be able to run tests without installing browsers' });
await exec('pnpm exec playwright install');
await exec('pnpm exec playwright test -c . --browser=all --reporter=list,json sample.spec.js', { env: { PLAYWRIGHT_JSON_OUTPUT_NAME: 'report.json' } });
await exec('node read-json-report.js', path.join(tmpWorkspace, 'report.json'));

View File

@ -33,16 +33,16 @@ function patchPackageJsonForPreReleaseIfNeeded(tmpWorkspace: string) {
}
test('npm: @playwright/test plugin should work', async ({ exec, tmpWorkspace }) => {
await exec('npm i --foreground-scripts @playwright/test');
await exec('npm i @playwright/test');
patchPackageJsonForPreReleaseIfNeeded(tmpWorkspace);
await exec('npm i --foreground-scripts playwright-test-plugin');
await exec('npm i playwright-test-plugin');
await exec('npx playwright install chromium');
const output = await exec('npx playwright test -c . --browser=chromium --reporter=line plugin.spec.ts');
expect(output).toContain('plugin value: hello from plugin');
expect(output).toContain('1 passed');
await exec('npm i --foreground-scripts typescript@5.2.2 @types/node@16');
await exec('npm i typescript@5.2.2 @types/node@16');
await exec('npx tsc playwright-test-plugin-types.ts');
});

View File

@ -1,24 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('@playwright/test stacks should work', async ({ exec }) => {
await exec('npm i --foreground-scripts @playwright/test');
await exec('npx playwright install chromium');
const output = await exec('npx playwright test -c . failing.spec.js', { expectToExitWithError: true, env: { DEBUG: 'pw:api' } });
expect(output).toContain('expect.toHaveText started');
expect(output).toContain('failing.spec.js:5:38');
});

View File

@ -16,8 +16,6 @@
import { test } from './npmTest';
test('screencast works', async ({ exec }) => {
await exec('npm i --foreground-scripts', 'playwright', 'playwright-chromium', 'playwright-firefox', 'playwright-webkit');
await test.step('playwright', () => exec('node screencast.js playwright chromium firefox webkit'));
for (const browser of ['chromium', 'firefox', 'webkit'])
await test.step(browser, () => exec(`node screencast.js playwright-${browser} ${browser}`));
await exec('npm i playwright');
await exec('node screencast.js playwright chromium firefox webkit');
});

View File

@ -1,25 +0,0 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from './npmTest';
test('should skip download', async ({ exec }) => {
const installOutput = await exec('npm i --foreground-scripts playwright @playwright/browser-chromium', { env: { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' } });
expect(installOutput).toContain('Skipping browsers download because');
if (process.platform === 'linux') {
const output = await exec('node inspector-custom-executable.js', { env: { PWDEBUG: '1' } });
expect(output).toContain('SUCCESS');
}
});

View File

@ -15,9 +15,16 @@
*/
import { test, expect } from './npmTest';
test('PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD should skip browser installs', async ({ exec, installedSoftwareOnDisk }) => {
const result = await exec('npm i --foreground-scripts @playwright/browser-firefox', { env: { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' } });
test.use({ isolateBrowsers: true });
test('should skip browser installs', async ({ exec, installedSoftwareOnDisk }) => {
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([]);
expect(result).toContain(`Skipping browsers download because`);
if (process.platform === 'linux') {
const output = await exec('node inspector-custom-executable.js', { env: { PWDEBUG: '1' } });
expect(output).toContain('SUCCESS');
}
});

View File

@ -31,28 +31,15 @@ test('typescript types should work', async ({ exec, tsc, writeFiles }) => {
[filename]: `import { Page } from '${libraryPackage}';`,
});
await tsc(filename);
}
await tsc('playwright-test-types.ts');
});
test('typescript types should work with module: NodeNext', async ({ exec, tsc, writeFiles }) => {
const libraryPackages = [
'playwright',
'playwright-core',
'playwright-firefox',
'playwright-webkit',
'playwright-chromium',
];
await exec('npm i @playwright/test', ...libraryPackages, { env: { PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD: '1' } });
for (const libraryPackage of libraryPackages) {
const filename = libraryPackage + '.ts';
await writeFiles({
[filename]: `import { Page } from '${libraryPackage}';`,
});
await tsc(`--module nodenext ${filename}`);
}
await tsc('playwright-test-types.ts');
await tsc('--module nodenext playwright-test-types.ts');
await writeFiles({
'test.ts':
`import { AndroidDevice, _android, AndroidWebView, Page } from 'playwright';`,
});
await tsc('test.ts');
});

View File

@ -15,18 +15,17 @@
*/
import { test, expect } from './npmTest';
test.describe('validate dependencies', () => {
test('default (on)', async ({ exec }) => {
await exec('npm i --foreground-scripts playwright');
await exec('npx playwright install chromium');
const result = await exec('node validate-dependencies.js');
expect(result).toContain(`PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS`);
test('validate dependencies', async ({ exec }) => {
await exec('npm i playwright');
await exec('npx playwright install chromium');
await test.step('default (on)', async () => {
const result1 = await exec('node validate-dependencies.js');
expect(result1).toContain(`PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS`);
});
test('disabled (off)', async ({ exec }) => {
await exec('npm i --foreground-scripts playwright');
await exec('npx playwright install chromium');
const result = await exec('node validate-dependencies-skip-executable-path.js');
expect(result).not.toContain(`PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS`);
await test.step('disabled (off)', async () => {
const result2 = await exec('node validate-dependencies-skip-executable-path.js');
expect(result2).not.toContain(`PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS`);
});
});