Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Built-in Reporters in Playwright https://playwright.dev/docs/test-reporters#built-in-reporters

Playwright comes with several built-in reporters that help you visualize, log, and analyze test results. Here's a brief overview:

  1. List Reporter (list):

    • This is the default reporter.

    • It provides a simple, human-readable output in the terminal, listing each test as it runs.

    • Shows test results (pass/fail) with clear indicators.

  2. Dot Reporter (dot):

    • Displays a minimal output where each test result is shown as a single dot.

    • Good for situations where you want very concise feedback.

  3. Line Reporter (line):

    • Similar to the dot reporter, but each test is displayed on a single line.

    • Provides more information than dot but still remains compact.

  4. JSON Reporter (json):

    • Outputs test results in JSON format.

    • Useful for processing test results programmatically or for integrating with other tools.

  5. JUnit Reporter (junit):

    • Generates an XML report in the JUnit format.

    • This is often used in CI/CD pipelines for integrating with test reporting tools.

  6. HTML Reporter (html):

    • Generates a detailed HTML report.

    • Includes test results, screenshots, and videos (if configured).

    • Allows you to drill down into each test case and step.

  7. Third-Party Reporters (Allure, GitHub Actions, Report Portal etc.):

    • Third-party reporters are community-contributed plugins that extend Playwright's reporting capabilities.

    • These reporters often integrate with popular testing and CI/CD tools, offering additional features or customizations that aren't available in Playwright's built-in reporters.

For more details: https://playwright.dev/docs/test-reporters#introduction

Custom Reporter using Playwright API

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

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

import { defineConfig } from '@playwright/test';

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

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

  • No labels