test: update fixtures to new syntax (#4063)

- Some simplifications around defineParameter.
- overrideTestFixtures must be chained as well.
This commit is contained in:
Dmitry Gozman 2020-10-05 21:40:56 -07:00 committed by GitHub
parent 0db09f8ed4
commit e403fd3912
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 89 deletions

View File

@ -15,9 +15,8 @@
*/
import { fixtures as playwrightFixtures } from '../fixtures';
const { it, expect, describe, overrideWorkerFixtures } = playwrightFixtures;
overrideWorkerFixtures({
const fixtures = playwrightFixtures.overrideWorkerFixtures({
browser: async ({browserType, defaultBrowserOptions}, test) => {
const browser = await browserType.launch({
...defaultBrowserOptions,
@ -28,6 +27,8 @@ overrideWorkerFixtures({
}
});
const { it, expect, describe } = fixtures;
describe('oopif', (suite, { browserName }) => {
suite.skip(browserName !== 'chromium');
}, () => {

View File

@ -32,6 +32,15 @@ export { expect, config } from '@playwright/test-runner';
const removeFolderAsync = util.promisify(require('rimraf'));
const mkdtempAsync = util.promisify(fs.mkdtemp);
const getExecutablePath = browserName => {
if (browserName === 'chromium' && process.env.CRPATH)
return process.env.CRPATH;
if (browserName === 'firefox' && process.env.FFPATH)
return process.env.FFPATH;
if (browserName === 'webkit' && process.env.WKPATH)
return process.env.WKPATH;
};
type AllTestFixtures = {
createUserDataDir: () => Promise<string>;
launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
@ -73,8 +82,70 @@ export const fixtures = playwrightFixtures
if (context)
await context.close();
},
})
.overrideWorkerFixtures({
defaultBrowserOptions: async ({ browserName, headful, slowMo }, runTest) => {
const executablePath = getExecutablePath(browserName);
if (executablePath)
console.error(`Using executable at ${executablePath}`);
await runTest({
executablePath,
handleSIGINT: false,
slowMo,
headless: !headful,
});
},
playwright: async ({ browserName, testWorkerIndex, platform, wire }, runTest) => {
assert(platform); // Depend on platform to generate all tests.
const { coverage, uninstall } = installCoverageHooks(browserName);
if (wire) {
require('../lib/utils/utils').setUnderTest();
const connection = new Connection();
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'driver.js'), ['serve'], {
stdio: 'pipe',
detached: true,
});
spawnedProcess.unref();
const onExit = (exitCode, signal) => {
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
};
spawnedProcess.on('exit', onExit);
const transport = new Transport(spawnedProcess.stdin, spawnedProcess.stdout);
connection.onmessage = message => transport.send(JSON.stringify(message));
transport.onmessage = message => connection.dispatch(JSON.parse(message));
const playwrightObject = await connection.waitForObjectWithKnownName('Playwright');
await runTest(playwrightObject);
spawnedProcess.removeListener('exit', onExit);
spawnedProcess.stdin.destroy();
spawnedProcess.stdout.destroy();
spawnedProcess.stderr.destroy();
await teardownCoverage();
} else {
const playwright = require('../index');
await runTest(playwright);
await teardownCoverage();
}
async function teardownCoverage() {
uninstall();
const coveragePath = path.join(__dirname, 'coverage-report', testWorkerIndex + '.json');
const coverageJSON = [...coverage.keys()].filter(key => coverage.get(key));
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
}
},
})
.overrideTestFixtures({
testParametersPathSegment: async ({ browserName }, runTest) => {
await runTest(browserName);
}
});
fixtures.generateParametrizedTests(
'platform',
process.env.PWTESTREPORT ? ['win32', 'darwin', 'linux'] : [process.platform as ('win32' | 'linux' | 'darwin')]);
export const it = fixtures.it;
export const fit = fixtures.fit;
export const xit = fixtures.xit;
@ -85,77 +156,3 @@ export const beforeEach = fixtures.beforeEach;
export const afterEach = fixtures.afterEach;
export const beforeAll = fixtures.beforeAll;
export const afterAll = fixtures.afterAll;
fixtures.generateParametrizedTests(
'platform',
process.env.PWTESTREPORT ? ['win32', 'darwin', 'linux'] : [process.platform as ('win32' | 'linux' | 'darwin')]);
const getExecutablePath = browserName => {
if (browserName === 'chromium' && process.env.CRPATH)
return process.env.CRPATH;
if (browserName === 'firefox' && process.env.FFPATH)
return process.env.FFPATH;
if (browserName === 'webkit' && process.env.WKPATH)
return process.env.WKPATH;
};
fixtures.overrideWorkerFixtures({
defaultBrowserOptions: async ({ browserName, headful, slowMo }, runTest) => {
const executablePath = getExecutablePath(browserName);
if (executablePath)
console.error(`Using executable at ${executablePath}`);
await runTest({
executablePath,
handleSIGINT: false,
slowMo,
headless: !headful,
});
},
playwright: async ({ browserName, testWorkerIndex, platform, wire }, runTest) => {
assert(platform); // Depend on platform to generate all tests.
const { coverage, uninstall } = installCoverageHooks(browserName);
if (wire) {
require('../lib/utils/utils').setUnderTest();
const connection = new Connection();
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'driver.js'), ['serve'], {
stdio: 'pipe',
detached: true,
});
spawnedProcess.unref();
const onExit = (exitCode, signal) => {
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
};
spawnedProcess.on('exit', onExit);
const transport = new Transport(spawnedProcess.stdin, spawnedProcess.stdout);
connection.onmessage = message => transport.send(JSON.stringify(message));
transport.onmessage = message => connection.dispatch(JSON.parse(message));
const playwrightObject = await connection.waitForObjectWithKnownName('Playwright');
await runTest(playwrightObject);
spawnedProcess.removeListener('exit', onExit);
spawnedProcess.stdin.destroy();
spawnedProcess.stdout.destroy();
spawnedProcess.stderr.destroy();
await teardownCoverage();
} else {
const playwright = require('../index');
await runTest(playwright);
await teardownCoverage();
}
async function teardownCoverage() {
uninstall();
const coveragePath = path.join(__dirname, 'coverage-report', testWorkerIndex + '.json');
const coverageJSON = [...coverage.keys()].filter(key => coverage.get(key));
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
}
},
});
fixtures.overrideTestFixtures({
testParametersPathSegment: async ({ browserName }, runTest) => {
await runTest(browserName);
}
});

View File

@ -59,12 +59,12 @@ type PlaywrightTestFixtures = {
};
export const fixtures = baseFixtures
.defineParameter<'browserName', 'chromium' | 'firefox' | 'webkit'>('browserName', 'Browser type name', process.env.BROWSER || 'chromium' as any)
.defineParameter<'headful', boolean>('headful', 'Whether to run tests headless or headful', process.env.HEADFUL ? true : false)
.defineParameter<'platform', 'win32' | 'linux' | 'darwin'>('platform', 'Operating system', process.platform as ('win32' | 'linux' | 'darwin'))
.defineParameter<'screenshotOnFailure', boolean>('screenshotOnFailure', 'Generate screenshot on failure', false)
.defineParameter<'slowMo', number>('slowMo', 'Slows down Playwright operations by the specified amount of milliseconds', 0)
.defineParameter<'trace', boolean>('trace', 'Whether to record the execution trace', !!process.env.TRACING || false)
.defineParameter('browserName', 'Browser type name', (process.env.BROWSER || 'chromium') as 'chromium' | 'firefox' | 'webkit')
.defineParameter('headful', 'Whether to run tests headless or headful', process.env.HEADFUL ? true : false)
.defineParameter('platform', 'Operating system', process.platform as ('win32' | 'linux' | 'darwin'))
.defineParameter('screenshotOnFailure', 'Generate screenshot on failure', false)
.defineParameter('slowMo', 'Slows down Playwright operations by the specified amount of milliseconds', 0)
.defineParameter('trace', 'Whether to record the execution trace', !!process.env.TRACING || false)
.defineWorkerFixtures<PlaywrightWorkerFixtures>({
defaultBrowserOptions: async ({ headful, slowMo }, runTest) => {
await runTest({
@ -159,16 +159,14 @@ export const fixtures = baseFixtures
await runTest(await context.newPage());
// Context fixture is taking care of closing the page.
},
})
.overrideTestFixtures({
testParametersPathSegment: async ({ browserName, platform }, runTest) => {
await runTest(browserName + '-' + platform);
}
});
// If browser is not specified, we are running tests against all three browsers.
fixtures.generateParametrizedTests(
'browserName',
process.env.BROWSER ? [process.env.BROWSER] as any : ['chromium', 'webkit', 'firefox']);
fixtures.overrideTestFixtures({
testParametersPathSegment: async ({ browserName, platform }, runTest) => {
await runTest(browserName + '-' + platform);
}
});