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

View file

@ -32,6 +32,15 @@ export { expect, config } from '@playwright/test-runner';
const removeFolderAsync = util.promisify(require('rimraf')); const removeFolderAsync = util.promisify(require('rimraf'));
const mkdtempAsync = util.promisify(fs.mkdtemp); 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 = { type AllTestFixtures = {
createUserDataDir: () => Promise<string>; createUserDataDir: () => Promise<string>;
launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>; launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
@ -73,34 +82,8 @@ export const fixtures = playwrightFixtures
if (context) if (context)
await context.close(); await context.close();
}, },
}); })
.overrideWorkerFixtures({
export const it = fixtures.it;
export const fit = fixtures.fit;
export const xit = fixtures.xit;
export const describe = fixtures.describe;
export const fdescribe = fixtures.fdescribe;
export const xdescribe = fixtures.xdescribe;
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) => { defaultBrowserOptions: async ({ browserName, headful, slowMo }, runTest) => {
const executablePath = getExecutablePath(browserName); const executablePath = getExecutablePath(browserName);
if (executablePath) if (executablePath)
@ -152,10 +135,24 @@ fixtures.overrideWorkerFixtures({
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8'); await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
} }
}, },
}); })
.overrideTestFixtures({
fixtures.overrideTestFixtures({
testParametersPathSegment: async ({ browserName }, runTest) => { testParametersPathSegment: async ({ browserName }, runTest) => {
await runTest(browserName); 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;
export const describe = fixtures.describe;
export const fdescribe = fixtures.fdescribe;
export const xdescribe = fixtures.xdescribe;
export const beforeEach = fixtures.beforeEach;
export const afterEach = fixtures.afterEach;
export const beforeAll = fixtures.beforeAll;
export const afterAll = fixtures.afterAll;

View file

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