Skip to content

Commit

Permalink
allow custom reports (#1)
Browse files Browse the repository at this point in the history
* allow custom reports
  • Loading branch information
Stephen Kilbourn authored Sep 30, 2022
1 parent 7eb70ca commit 9cfdc62
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 20 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,53 @@ Modify your `playwright.config.ts` file to include the reporter:

The default output location will be to your root as `summary.txt` Including the `outputFile` parameter allows you to specify a custom report location.

## Customizing Outputs
## Default Output 📜

Coming Soon
If you do not pass an outputFile option, then the summary will be generated to a `summary.txt` file in the following format:

```txt
Total Tests in Suite: 30,
Total Tests Completed: 30,
Tests Passed: 30,
Tests Failed: 0,
Flaky Tests: 0,
Test Skipped: 0,
Test run was failure free? true,
Duration in ms: 234,
Average Test Duration:0,
Formatted Duration: 00:01 (mm:ss),
Formatted Avg Test Duration: 00:01 (mm:ss)
```

## Customizing Outputs 👨‍💻

To add a custom report leveraging your stats, create a function in the format:

```typescript
stats,
) => `
I completed ${stats.totalCompleted} Total Tests in ${stats.formattedDuration}.`;
```

and then modify your `playwright.config.ts` file with the following:

```typescript
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

const customReport = require('./customReport'); // Your custom report path and preferred name


const config: PlaywrightTestConfig = {
...
reporter: [
['@skilbourn/playwright-report-summary', { outputFile: 'custom-summary.txt', inputTemplate: customReport }]]
],

```
this will generate a file such as :
```txt
I completed 30 Total Tests in 00:01 (mm:ss).
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@skilbourn/playwright-report-summary",
"version": "0.1.0",
"version": "0.1.1",
"description": "generate a customizable text summary of your playwright test results",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { devices } from '@playwright/test';
/**
* See https://playwright.dev/docs/test-configuration.
*/

const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
Expand All @@ -24,7 +25,7 @@ const config: PlaywrightTestConfig = {
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: [['html'], ['./src/index.ts', { outputFile: 'summary.txt' }]],
reporter: [['html'], ['./src/index.ts']],
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
actionTimeout: 0,
Expand Down
46 changes: 30 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ class PlaywrightReportSummary implements Reporter {

private outputFile: string;

constructor(options: { outputFile?: string } = {}) {
private inputTemplate: Function;

constructor(options: { outputFile?: string; inputTemplate?: Function } = {}) {
this.outputFile = options.outputFile;
this.inputTemplate = options.inputTemplate;
}

onBegin(config, suite) {
Expand All @@ -65,24 +68,35 @@ class PlaywrightReportSummary implements Reporter {
const avgTestTime = this.stats.duration / (this.stats.totalCompleted || 1);
this.stats.formattedDuration = millisToMinuteSeconds(this.stats.duration);
this.stats.formattedAvgTestDuration = millisToMinuteSeconds(avgTestTime);
outputReport(this.stats, this.outputFile);
outputReport(this.stats, this.inputTemplate, this.outputFile);
}
}

function outputReport(stats: Stats, outputFile = 'results.txt') {
const reportString = defaultReport(
stats.testsInSuite,
stats.totalCompleted,
stats.expectedResults,
stats.unexpectedResults,
stats.flakyTests,
stats.testMarkedSkipped,
stats.failureFree,
stats.duration,
stats.avgTestDuration,
stats.formattedDuration,
stats.formattedAvgTestDuration,
);
function outputReport(
stats: Stats,
inputTemplate?: Function,
outputFile: string = 'results.txt',
) {
let reportString: string;

if (typeof inputTemplate === 'undefined') {
reportString = defaultReport(
stats.testsInSuite,
stats.totalCompleted,
stats.expectedResults,
stats.unexpectedResults,
stats.flakyTests,
stats.testMarkedSkipped,
stats.failureFree,
stats.duration,
stats.avgTestDuration,
stats.formattedDuration,
stats.formattedAvgTestDuration,
);
} else {
reportString = inputTemplate(stats);
}

fs.mkdirSync(path.dirname(outputFile), { recursive: true });
fs.writeFileSync(outputFile, reportString);
}
Expand Down

0 comments on commit 9cfdc62

Please sign in to comment.