Versions Compared

Key

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

...

Playwright

...

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

...

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.

Dot Reporter (dot):

...

is

...

Good for situations where you want very concise feedback.

...

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.

...

JSON Reporter (json):

  • Outputs test results in JSON format.

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

...

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.

...

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.

...

  • 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

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

...

languagejs

...

very versatile with report generation. It can be categorized in three ways:

  • In-built Reports and Third-Party Support:

    • Playwright offers built-in report generation features, including HTML and JSON reports, which provide insights into test results and performance. It also supports third-party reporters like Allure and Jest, which can be integrated to customize the reporting format and functionality.

  • Custom Report API of Playwright:

    • Playwright provides a Custom Report API that allows you to create tailored reports by hooking into test events. This API lets you capture detailed information about test execution, steps, and results, enabling you to design reports that meet specific needs or organizational standards.

  • Write Your Own Custom API:

    • For maximum flexibility, you can develop a completely custom reporting solution by directly interfacing with Playwright's test results and events. This approach involves writing your own code to collect, format, and display test data according to your unique requirements, giving you full control over the reporting process.