Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
- import { writeFileSync } from 'fs';
- interface ITestEntry {
- id: string;
- name: string;
- duration: number;
- }
- const buildFilePath = (outputPath: string, outputFile: string): string => {
- // Remove trailing slashes from outputPath and add a slash if outputPath is not empty
- return outputPath.replace(/\/+$/, '') + (outputPath ? '/' : '') + outputFile.replace(/^\/+/, '');
- };
- class CustomerReporter implements Reporter {
- private results: ITestEntry[] = [];
- private outputFile: string;
- private outputPath: string;
- private finalOutputFile: string;
- constructor(options: { outputFile?: string; outputPath?: string } = {}) {
- this.outputFile = `${options.outputFile || 'report'}${options.outputFile.endsWith('.json') ? '' : '.json'}`;
- this.outputPath = options.outputPath || '';
- this.finalOutputFile = buildFilePath(this.outputPath, this.outputFile);
- }
- onTestEnd(test: TestCase, result: TestResult) {
- if (result.status === 'passed') {
- this.results.push({
- id: test.id,
- name: test.title,
- duration: result.duration,
- });
- }
- }
- onEnd() {
- writeFileSync(this.finalOutputFile, JSON.stringify(this.results, null, 2), 'utf-8');
- console.log(`Custom report generated: ${this.finalOutputFile}`);
- }
- }
- export default CustomerReporter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement