Skip to content
This repository was archived by the owner on Jun 11, 2021. It is now read-only.

Commit 57229e8

Browse files
committed
feat: simplified examples (asset generation)
1 parent 0d971e9 commit 57229e8

File tree

9 files changed

+93
-80
lines changed

9 files changed

+93
-80
lines changed

.github/workflows/test-examples.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
jobs:
88
test-examples:
99
strategy:
10+
fail-fast: false
1011
matrix:
1112
dir: [basic-js, basic-ts, browser-device-matrix, record-video, screenshot-on-failure]
1213
defaults:
@@ -20,5 +21,13 @@ jobs:
2021
uses: actions/setup-node@v1
2122
with:
2223
node-version: 12.x
23-
- run: npm install
24+
- run: yarn
25+
working-directory: .
26+
- run: yarn link
27+
working-directory: packages/playwright-runner
28+
- run: yarn link
29+
working-directory: packages/test-runner
30+
- run: yarn link playwright-runner
31+
- run: yarn link @playwright/test-runner
32+
- run: yarn install
2433
- run: npm test

examples/browser-device-matrix/test/playwright.spec.ts

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {registerWorkerFixture} from 'playwright-runner';
18-
import playwright from 'playwright';
19-
20-
declare global {
21-
interface FixtureParameters {
22-
deviceName: string;
23-
}
24-
}
25-
26-
registerWorkerFixture('deviceName', async ({}, test) => {
27-
await test(null);
28-
});
29-
30-
registerWorkerFixture('defaultContextOptions', async ({deviceName}, test) => {
31-
const device = typeof deviceName === 'string' ? playwright.devices[deviceName] : deviceName;
32-
await test({
33-
...device
34-
});
35-
});
17+
import 'playwright-runner';
3618

3719
it('is a basic test with the page', async ({page}) => {
3820
await page.goto('http://whatsmyuseragent.org/');

examples/record-video/tests/playwright.spec.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {registerFixture} from 'playwright-runner';
17+
import {registerFixture, generateTestAssetName} from 'playwright-runner';
1818
import {saveVideo} from 'playwright-video';
19-
import path from 'path';
2019

2120
registerFixture('page', async ({context}, runTest, info) => {
2221
const page = await context.newPage();
23-
const {test, config} = info;
24-
const relativePath = path.relative(config.testDir, test.file).replace(/\.spec\.[jt]s/, '');
25-
const sanitizedTitle = test.title.replace(/[^\w\d]+/g, '_');
26-
const assetPath = path.join(config.outputDir, relativePath, sanitizedTitle) + '-recording.mp4';
27-
22+
const assetPath = await generateTestAssetName(info, 'recording.mp4');
2823
const recording = await saveVideo(page, assetPath);
2924
await runTest(page);
3025
await recording.stop();

examples/screenshot-on-failure/tests/playwright.spec.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
import fs from 'fs';
18-
import path from 'path';
1917
import 'playwright-runner';
20-
import {registerFixture} from 'playwright-runner';
18+
import {registerFixture, generateTestAssetName} from 'playwright-runner';
2119

2220
registerFixture('page', async ({context}, runTest, info) => {
2321
const page = await context.newPage();
2422
await runTest(page);
25-
const {test, config, result} = info;
23+
const {result} = info;
2624
if (result.status === 'failed' || result.status === 'timedOut') {
27-
const relativePath = path.relative(config.testDir, test.file).replace(/\.spec\.[jt]s/, '');
28-
const sanitizedTitle = test.title.replace(/[^\w\d]+/g, '_');
29-
const assetPath = path.join(config.outputDir, relativePath, sanitizedTitle) + '-failed.png';
30-
fs.mkdirSync(path.dirname(assetPath), {
31-
recursive: true
32-
});
25+
const assetPath = await generateTestAssetName(info, 'failed.png');
3326
await page.screenshot({ path: assetPath});
3427
}
3528
await page.close();

packages/playwright-runner/src/index.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
import {registerWorkerFixture, registerFixture} from '@playwright/test-runner';
18-
import {LaunchOptions, BrowserType, Browser, BrowserContext, Page, chromium, firefox, webkit, BrowserContextOptions} from 'playwright';
18+
import {LaunchOptions, BrowserType, Browser, BrowserContext, Page, chromium, firefox, webkit, BrowserContextOptions, devices} from 'playwright';
1919
export * from '@playwright/test-runner';
2020

2121
declare global {
@@ -31,6 +31,7 @@ declare global {
3131
}
3232
interface FixtureParameters {
3333
browserName: 'chromium'|'firefox'|'webkit';
34+
deviceName: null | string | BrowserContextOptions
3435
}
3536
}
3637

@@ -49,14 +50,28 @@ registerWorkerFixture('browser', async ({browserType, defaultBrowserOptions}, te
4950
await browser.close();
5051
});
5152

52-
registerWorkerFixture('defaultBrowserOptions', async ({}, test) => {
53+
registerWorkerFixture('deviceName', async ({}, test) => {
54+
await test(null);
55+
});
56+
57+
registerWorkerFixture('defaultContextOptions', async ({deviceName}, test) => {
58+
let device: BrowserContextOptions = {};
59+
60+
if (deviceName && typeof deviceName === 'string')
61+
device = devices[deviceName];
62+
else if (deviceName && typeof deviceName === 'object')
63+
device = deviceName;
64+
5365
await test({
54-
handleSIGINT: false,
66+
...device
5567
});
5668
});
5769

58-
registerWorkerFixture('defaultContextOptions', async ({}, test) => {
59-
await test({});
70+
registerWorkerFixture('defaultBrowserOptions', async ({}, test) => {
71+
await test({
72+
handleSIGINT: false,
73+
...(process.env.HEADFUL ? {headless: false} : {})
74+
});
6075
});
6176

6277
registerFixture('context', async ({browser, defaultContextOptions}, test) => {

packages/test-runner/src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function collectFiles(testDir: string, dir: string, filters: string[]): string[]
112112
files.push(...collectFiles(testDir, path.join(dir, name), filters));
113113
continue;
114114
}
115-
if (!name.endsWith('spec.ts'))
115+
if (!name.endsWith('spec.ts') && !name.endsWith('spec.js'))
116116
continue;
117117
const relativeName = path.join(dir, name);
118118
const fullName = path.join(testDir, relativeName);

packages/test-runner/src/fixtures.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616

1717
import debug from 'debug';
18+
import path from 'path';
19+
import fs from 'fs';
20+
import {promisify} from 'util';
1821
import { RunnerConfig } from './runnerConfig';
1922
import { serializeError, Test, TestResult } from './test';
2023
import { raceAgainstTimeout } from './util';
2124

25+
const mkdirAsync = promisify(fs.mkdir);
26+
2227
type Scope = 'test' | 'worker';
2328

2429
type FixtureRegistration = {
@@ -274,3 +279,16 @@ export function rerunRegistrations(file: string, scope: Scope) {
274279
for (const registration of lookupRegistrations(file, scope).values())
275280
registrations.set(registration.name, registration);
276281
}
282+
283+
export async function generateTestAssetName(info: TestInfo, suffix: string): Promise<string> {
284+
const {config, test} = info;
285+
const relativePath = path.relative(config.testDir, test.file)
286+
.replace(/\.spec\.[jt]s/, '')
287+
.replace(new RegExp(`(tests|test|src)${path.sep}`), '');
288+
const sanitizedTitle = test.title.replace(/[^\w\d]+/g, '_');
289+
const assetPath = path.join(config.outputDir, relativePath, `${sanitizedTitle}-${suffix}`);
290+
await mkdirAsync(path.dirname(assetPath), {
291+
recursive: true
292+
});
293+
return assetPath;
294+
}

packages/test-runner/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export { parameters, registerParameter } from './fixtures';
3333
export { Reporter } from './reporter';
3434
export { RunnerConfig } from './runnerConfig';
3535
export { Suite, Test } from './test';
36+
export { generateTestAssetName } from './fixtures';
3637

3738
const removeFolderAsync = promisify(rimraf);
3839

packages/test-runner/test/fixtures.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,39 @@ export type RunResult = {
3636
};
3737

3838
async function runTest(reportFile: string, outputDir: string, filePath: string, params: any = {}): Promise<RunResult> {
39-
const { output, status } = spawnSync('node', [
40-
path.join(__dirname, '..', 'cli.js'),
41-
path.join(__dirname, 'assets', filePath),
42-
'--output=' + outputDir,
43-
'--reporter=dot,json',
44-
...Object.keys(params).map(key => `--${key}=${params[key]}`)
45-
], {
46-
env: {
47-
...process.env,
48-
PW_OUTPUT_DIR: outputDir,
49-
PWRUNNER_JSON_REPORT: reportFile,
50-
}
51-
});
52-
const passed = (/(\d+) passed/.exec(output.toString()) || [])[1];
53-
const failed = (/(\d+) failed/.exec(output.toString()) || [])[1];
54-
const timedOut = (/(\d+) timed out/.exec(output.toString()) || [])[1];
55-
const expectedFlaky = (/(\d+) expected flaky/.exec(output.toString()) || [])[1];
56-
const unexpectedFlaky = (/(\d+) unexpected flaky/.exec(output.toString()) || [])[1];
57-
const skipped = (/(\d+) skipped/.exec(output.toString()) || [])[1];
58-
const report = JSON.parse(fs.readFileSync(reportFile).toString());
59-
let outputStr = output.toString();
60-
outputStr = outputStr.substring(1, outputStr.length - 1);
61-
return {
62-
exitCode: status,
63-
output: outputStr,
64-
passed: parseInt(passed, 10),
65-
failed: parseInt(failed || '0', 10),
66-
timedOut: parseInt(timedOut || '0', 10),
67-
expectedFlaky: parseInt(expectedFlaky || '0', 10),
68-
unexpectedFlaky: parseInt(unexpectedFlaky || '0', 10),
69-
skipped: parseInt(skipped || '0', 10),
70-
report
71-
};
39+
const { output, status } = spawnSync('node', [
40+
path.join(__dirname, '..', 'cli.js'),
41+
path.join(__dirname, 'assets', filePath),
42+
'--output=' + outputDir,
43+
'--reporter=dot,json',
44+
...Object.keys(params).map(key => `--${key}=${params[key]}`)
45+
], {
46+
env: {
47+
...process.env,
48+
PW_OUTPUT_DIR: outputDir,
49+
PWRUNNER_JSON_REPORT: reportFile,
50+
}
51+
});
52+
const passed = (/(\d+) passed/.exec(output.toString()) || [])[1];
53+
const failed = (/(\d+) failed/.exec(output.toString()) || [])[1];
54+
const timedOut = (/(\d+) timed out/.exec(output.toString()) || [])[1];
55+
const expectedFlaky = (/(\d+) expected flaky/.exec(output.toString()) || [])[1];
56+
const unexpectedFlaky = (/(\d+) unexpected flaky/.exec(output.toString()) || [])[1];
57+
const skipped = (/(\d+) skipped/.exec(output.toString()) || [])[1];
58+
const report = JSON.parse(fs.readFileSync(reportFile).toString());
59+
let outputStr = output.toString();
60+
outputStr = outputStr.substring(1, outputStr.length - 1);
61+
return {
62+
exitCode: status,
63+
output: outputStr,
64+
passed: parseInt(passed, 10),
65+
failed: parseInt(failed || '0', 10),
66+
timedOut: parseInt(timedOut || '0', 10),
67+
expectedFlaky: parseInt(expectedFlaky || '0', 10),
68+
unexpectedFlaky: parseInt(unexpectedFlaky || '0', 10),
69+
skipped: parseInt(skipped || '0', 10),
70+
report
71+
};
7272
}
7373

7474
declare global {
@@ -79,11 +79,11 @@ declare global {
7979
}
8080

8181
registerFixture('outputDir', async ({}, testRun) => {
82-
await testRun(path.join(__dirname, 'test-results', String(parameters.parallelIndex)));
82+
await testRun(path.join(__dirname, 'test-results', String(parameters.parallelIndex)));
8383
});
8484

8585
registerFixture('runTest', async ({ outputDir }, testRun) => {
86-
const reportFile = path.join(outputDir, `results-${parameters.parallelIndex}.json`);
87-
await removeFolderAsync(outputDir).catch(e => { });
88-
await testRun(runTest.bind(null, reportFile, outputDir));
86+
const reportFile = path.join(outputDir, `results-${parameters.parallelIndex}.json`);
87+
await removeFolderAsync(outputDir).catch(e => { });
88+
await testRun(runTest.bind(null, reportFile, outputDir));
8989
});

0 commit comments

Comments
 (0)