Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Playwright Offers API to write custom test reports. A custom reporter is a class that implements specific methods to handle events during the test run. Playwright Reporter API can be implemented and the native reporter methods of API can overridden based on needs. Below is an example:

PlaywrightCustomReporter.js

Code Block
languagejs
class MyCustomReporter {
  
   onBegin(config, suite) {
      console.log(`Starting the test run with ${suite.allTests().length} tests`);
   }

  
   onSuiteBegin(suite) {
      console.log(`Starting suite: ${suite.title}`);
   }

  
   onSuiteEnd(suite) {
      console.log(`Finished suite: ${suite.title}`);
   }

 
   onTestBegin(test) {
      console.log(`Starting test: ${test.title}`);
   }

  
   onTestEnd(test, result) {
      console.log(`Finished test: ${test.title} with status ${result.status}`);
      if (result.error) {
         console.error(`Error: ${result.error.message}`);
      }
   }

   
   onTestFailure(test, result) {
      console.error(`Test failed: ${test.title}`);
      console.error(`Error: ${result.error.message}`);
   }

  
   onTestSuccess(test, result) {
      console.log(`Test passed: ${test.title}`);
   }

   
   onTestTimeout(test) {
      console.warn(`Test timed out: ${test.title}`);
   }

   // Called when a test's retry count is exhausted
   onTestRetry(test) {
      console.warn(`Test retried: ${test.title}`);
   }

  
   onEnd(result) {
      console.log(`Test run finished with status ${result.status}`);
      console.log(`Total tests: ${result.totalTests}`);
      console.log(`Passed: ${result.passedTests}`);
      console.log(`Failed: ${result.failedTests}`);
   }

   
   onStdOut(data) {
      process.stdout.write(data);
   }

  
   onStdErr(data) {
      process.stderr.write(data);
   }

  
   onStepBegin(test, step) {
    console.log('Step Begin:');
    console.log('Test:', test);
    console.log('Step:', step);
  }
  
  onStepEnd(test, step, result) {
    console.log('Step End:');
    console.log('Test:', test);
    console.log('Step:', step);
    console.log('Result:', result);
  }
  
}

export default MyCustomReporter;

This code defines a custom reporter class MyCustomReporter that implements various methods to track and log the progress of a test run. It's designed to be used with a testing framework, likely Playwright, where it provides detailed logs at different stages of the test execution process.

Here’s a brief explanation of each method:

  1. onBegin(config, suite): Logs the start of the test run, including the number of tests in the suite.

  2. onSuiteBegin(suite): Logs the start of a test suite.

  3. onSuiteEnd(suite): Logs the end of a test suite.

  4. onTestBegin(test): Logs the start of an individual test.

  5. onTestEnd(test, result): Logs the end of an individual test, including its status and any errors if they occurred.

  6. onTestFailure(test, result): Logs detailed information about a failed test.

  7. onTestSuccess(test, result): Logs a message when a test passes.

  8. onTestTimeout(test): Logs when a test times out.

  9. onTestRetry(test): Logs when a test is retried.

  10. onEnd(result): Logs the summary of the test run, including the status and counts of total, passed, and failed tests.

  11. onStdOut(data): Writes stdout output to the console.

  12. onStdErr(data): Writes stderr output to the console.

  13. onStepBegin(test, step): Logs the start of a test step, along with the test and step details.

  14. onStepEnd(test, step, result): Logs the end of a test step, including the result of the step.

There are primarily two ways to use the Custom Reporter API.

  1. Define in playwright.config.js/ts

Code Block
languagejs
import { defineConfig } from '@playwright/test';

export default defineConfig({
  globalTeardown: './global-teardown',
});
  1. Passing the parameter while executing the test command

Code Block
languagejs
import {exec} from "child_process";
 
exec("npx playwright test --reporter="./myreporter/PlaywrightCustomReporter.js"")