Advertisement
Guest User

Untitled

a guest
Aug 26th, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import type { Reporter, TestCase, TestResult } from '@playwright/test/reporter';
  2. import { writeFileSync } from 'fs';
  3.  
  4. interface ITestEntry {
  5.   id: string;
  6.   name: string;
  7.   duration: number;
  8. }
  9.  
  10. const buildFilePath = (outputPath: string, outputFile: string): string => {
  11.   // Remove trailing slashes from outputPath and add a slash if outputPath is not empty
  12.   return outputPath.replace(/\/+$/, '') + (outputPath ? '/' : '') + outputFile.replace(/^\/+/, '');
  13. };
  14.  
  15. class CustomerReporter implements Reporter {
  16.   private results: ITestEntry[] = [];
  17.   private outputFile: string;
  18.   private outputPath: string;
  19.   private finalOutputFile: string;
  20.  
  21.   constructor(options: { outputFile?: string; outputPath?: string } = {}) {
  22.     this.outputFile = `${options.outputFile || 'report'}${options.outputFile.endsWith('.json') ? '' : '.json'}`;
  23.     this.outputPath = options.outputPath || '';
  24.     this.finalOutputFile = buildFilePath(this.outputPath, this.outputFile);
  25.   }
  26.  
  27.   onTestEnd(test: TestCase, result: TestResult) {
  28.     if (result.status === 'passed') {
  29.       this.results.push({
  30.         id: test.id,
  31.         name: test.title,
  32.         duration: result.duration,
  33.       });
  34.     }
  35.   }
  36.  
  37.   onEnd() {
  38.     writeFileSync(this.finalOutputFile, JSON.stringify(this.results, null, 2), 'utf-8');
  39.     console.log(`Custom report generated: ${this.finalOutputFile}`);
  40.   }
  41. }
  42.  
  43. export default CustomerReporter;
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement