Skip to content

Commit e403fd3

Browse files
authored
test: update fixtures to new syntax (#4063)
- Some simplifications around defineParameter. - overrideTestFixtures must be chained as well.
1 parent 0db09f8 commit e403fd3

File tree

3 files changed

+85
-89
lines changed

3 files changed

+85
-89
lines changed

test/chromium/oopif.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616

1717
import { fixtures as playwrightFixtures } from '../fixtures';
18-
const { it, expect, describe, overrideWorkerFixtures } = playwrightFixtures;
1918

20-
overrideWorkerFixtures({
19+
const fixtures = playwrightFixtures.overrideWorkerFixtures({
2120
browser: async ({browserType, defaultBrowserOptions}, test) => {
2221
const browser = await browserType.launch({
2322
...defaultBrowserOptions,
@@ -28,6 +27,8 @@ overrideWorkerFixtures({
2827
}
2928
});
3029

30+
const { it, expect, describe } = fixtures;
31+
3132
describe('oopif', (suite, { browserName }) => {
3233
suite.skip(browserName !== 'chromium');
3334
}, () => {

test/fixtures.ts

Lines changed: 71 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ export { expect, config } from '@playwright/test-runner';
3232
const removeFolderAsync = util.promisify(require('rimraf'));
3333
const mkdtempAsync = util.promisify(fs.mkdtemp);
3434

35+
const getExecutablePath = browserName => {
36+
if (browserName === 'chromium' && process.env.CRPATH)
37+
return process.env.CRPATH;
38+
if (browserName === 'firefox' && process.env.FFPATH)
39+
return process.env.FFPATH;
40+
if (browserName === 'webkit' && process.env.WKPATH)
41+
return process.env.WKPATH;
42+
};
43+
3544
type AllTestFixtures = {
3645
createUserDataDir: () => Promise<string>;
3746
launchPersistent: (options?: Parameters<BrowserType<Browser>['launchPersistentContext']>[1]) => Promise<{ context: BrowserContext, page: Page }>;
@@ -73,8 +82,70 @@ export const fixtures = playwrightFixtures
7382
if (context)
7483
await context.close();
7584
},
85+
})
86+
.overrideWorkerFixtures({
87+
defaultBrowserOptions: async ({ browserName, headful, slowMo }, runTest) => {
88+
const executablePath = getExecutablePath(browserName);
89+
if (executablePath)
90+
console.error(`Using executable at ${executablePath}`);
91+
await runTest({
92+
executablePath,
93+
handleSIGINT: false,
94+
slowMo,
95+
headless: !headful,
96+
});
97+
},
98+
99+
playwright: async ({ browserName, testWorkerIndex, platform, wire }, runTest) => {
100+
assert(platform); // Depend on platform to generate all tests.
101+
const { coverage, uninstall } = installCoverageHooks(browserName);
102+
if (wire) {
103+
require('../lib/utils/utils').setUnderTest();
104+
const connection = new Connection();
105+
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'driver.js'), ['serve'], {
106+
stdio: 'pipe',
107+
detached: true,
108+
});
109+
spawnedProcess.unref();
110+
const onExit = (exitCode, signal) => {
111+
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
112+
};
113+
spawnedProcess.on('exit', onExit);
114+
const transport = new Transport(spawnedProcess.stdin, spawnedProcess.stdout);
115+
connection.onmessage = message => transport.send(JSON.stringify(message));
116+
transport.onmessage = message => connection.dispatch(JSON.parse(message));
117+
const playwrightObject = await connection.waitForObjectWithKnownName('Playwright');
118+
await runTest(playwrightObject);
119+
spawnedProcess.removeListener('exit', onExit);
120+
spawnedProcess.stdin.destroy();
121+
spawnedProcess.stdout.destroy();
122+
spawnedProcess.stderr.destroy();
123+
await teardownCoverage();
124+
} else {
125+
const playwright = require('../index');
126+
await runTest(playwright);
127+
await teardownCoverage();
128+
}
129+
130+
async function teardownCoverage() {
131+
uninstall();
132+
const coveragePath = path.join(__dirname, 'coverage-report', testWorkerIndex + '.json');
133+
const coverageJSON = [...coverage.keys()].filter(key => coverage.get(key));
134+
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
135+
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
136+
}
137+
},
138+
})
139+
.overrideTestFixtures({
140+
testParametersPathSegment: async ({ browserName }, runTest) => {
141+
await runTest(browserName);
142+
}
76143
});
77144

145+
fixtures.generateParametrizedTests(
146+
'platform',
147+
process.env.PWTESTREPORT ? ['win32', 'darwin', 'linux'] : [process.platform as ('win32' | 'linux' | 'darwin')]);
148+
78149
export const it = fixtures.it;
79150
export const fit = fixtures.fit;
80151
export const xit = fixtures.xit;
@@ -85,77 +156,3 @@ export const beforeEach = fixtures.beforeEach;
85156
export const afterEach = fixtures.afterEach;
86157
export const beforeAll = fixtures.beforeAll;
87158
export const afterAll = fixtures.afterAll;
88-
89-
90-
fixtures.generateParametrizedTests(
91-
'platform',
92-
process.env.PWTESTREPORT ? ['win32', 'darwin', 'linux'] : [process.platform as ('win32' | 'linux' | 'darwin')]);
93-
94-
const getExecutablePath = browserName => {
95-
if (browserName === 'chromium' && process.env.CRPATH)
96-
return process.env.CRPATH;
97-
if (browserName === 'firefox' && process.env.FFPATH)
98-
return process.env.FFPATH;
99-
if (browserName === 'webkit' && process.env.WKPATH)
100-
return process.env.WKPATH;
101-
};
102-
103-
fixtures.overrideWorkerFixtures({
104-
defaultBrowserOptions: async ({ browserName, headful, slowMo }, runTest) => {
105-
const executablePath = getExecutablePath(browserName);
106-
if (executablePath)
107-
console.error(`Using executable at ${executablePath}`);
108-
await runTest({
109-
executablePath,
110-
handleSIGINT: false,
111-
slowMo,
112-
headless: !headful,
113-
});
114-
},
115-
116-
playwright: async ({ browserName, testWorkerIndex, platform, wire }, runTest) => {
117-
assert(platform); // Depend on platform to generate all tests.
118-
const { coverage, uninstall } = installCoverageHooks(browserName);
119-
if (wire) {
120-
require('../lib/utils/utils').setUnderTest();
121-
const connection = new Connection();
122-
const spawnedProcess = childProcess.fork(path.join(__dirname, '..', 'lib', 'driver.js'), ['serve'], {
123-
stdio: 'pipe',
124-
detached: true,
125-
});
126-
spawnedProcess.unref();
127-
const onExit = (exitCode, signal) => {
128-
throw new Error(`Server closed with exitCode=${exitCode} signal=${signal}`);
129-
};
130-
spawnedProcess.on('exit', onExit);
131-
const transport = new Transport(spawnedProcess.stdin, spawnedProcess.stdout);
132-
connection.onmessage = message => transport.send(JSON.stringify(message));
133-
transport.onmessage = message => connection.dispatch(JSON.parse(message));
134-
const playwrightObject = await connection.waitForObjectWithKnownName('Playwright');
135-
await runTest(playwrightObject);
136-
spawnedProcess.removeListener('exit', onExit);
137-
spawnedProcess.stdin.destroy();
138-
spawnedProcess.stdout.destroy();
139-
spawnedProcess.stderr.destroy();
140-
await teardownCoverage();
141-
} else {
142-
const playwright = require('../index');
143-
await runTest(playwright);
144-
await teardownCoverage();
145-
}
146-
147-
async function teardownCoverage() {
148-
uninstall();
149-
const coveragePath = path.join(__dirname, 'coverage-report', testWorkerIndex + '.json');
150-
const coverageJSON = [...coverage.keys()].filter(key => coverage.get(key));
151-
await fs.promises.mkdir(path.dirname(coveragePath), { recursive: true });
152-
await fs.promises.writeFile(coveragePath, JSON.stringify(coverageJSON, undefined, 2), 'utf8');
153-
}
154-
},
155-
});
156-
157-
fixtures.overrideTestFixtures({
158-
testParametersPathSegment: async ({ browserName }, runTest) => {
159-
await runTest(browserName);
160-
}
161-
});

test/playwright.fixtures.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ type PlaywrightTestFixtures = {
5959
};
6060

6161
export const fixtures = baseFixtures
62-
.defineParameter<'browserName', 'chromium' | 'firefox' | 'webkit'>('browserName', 'Browser type name', process.env.BROWSER || 'chromium' as any)
63-
.defineParameter<'headful', boolean>('headful', 'Whether to run tests headless or headful', process.env.HEADFUL ? true : false)
64-
.defineParameter<'platform', 'win32' | 'linux' | 'darwin'>('platform', 'Operating system', process.platform as ('win32' | 'linux' | 'darwin'))
65-
.defineParameter<'screenshotOnFailure', boolean>('screenshotOnFailure', 'Generate screenshot on failure', false)
66-
.defineParameter<'slowMo', number>('slowMo', 'Slows down Playwright operations by the specified amount of milliseconds', 0)
67-
.defineParameter<'trace', boolean>('trace', 'Whether to record the execution trace', !!process.env.TRACING || false)
62+
.defineParameter('browserName', 'Browser type name', (process.env.BROWSER || 'chromium') as 'chromium' | 'firefox' | 'webkit')
63+
.defineParameter('headful', 'Whether to run tests headless or headful', process.env.HEADFUL ? true : false)
64+
.defineParameter('platform', 'Operating system', process.platform as ('win32' | 'linux' | 'darwin'))
65+
.defineParameter('screenshotOnFailure', 'Generate screenshot on failure', false)
66+
.defineParameter('slowMo', 'Slows down Playwright operations by the specified amount of milliseconds', 0)
67+
.defineParameter('trace', 'Whether to record the execution trace', !!process.env.TRACING || false)
6868
.defineWorkerFixtures<PlaywrightWorkerFixtures>({
6969
defaultBrowserOptions: async ({ headful, slowMo }, runTest) => {
7070
await runTest({
@@ -159,16 +159,14 @@ export const fixtures = baseFixtures
159159
await runTest(await context.newPage());
160160
// Context fixture is taking care of closing the page.
161161
},
162+
})
163+
.overrideTestFixtures({
164+
testParametersPathSegment: async ({ browserName, platform }, runTest) => {
165+
await runTest(browserName + '-' + platform);
166+
}
162167
});
163168

164169
// If browser is not specified, we are running tests against all three browsers.
165170
fixtures.generateParametrizedTests(
166171
'browserName',
167172
process.env.BROWSER ? [process.env.BROWSER] as any : ['chromium', 'webkit', 'firefox']);
168-
169-
170-
fixtures.overrideTestFixtures({
171-
testParametersPathSegment: async ({ browserName, platform }, runTest) => {
172-
await runTest(browserName + '-' + platform);
173-
}
174-
});

0 commit comments

Comments
 (0)