DEV Community

Cover image for How to Generate Extent Reports in Selenium
Steve Wortham
Steve Wortham

Posted on

How to Generate Extent Reports in Selenium

Reporting is an important step of test automation as it helps stakeholders understand the outcome of the test case, the status of the application’s health and potential issues.

What are extent reports?

Extent Reports is an open-source reporting library used with test automation tools. It can be integrated with testing frameworks like JUnit and TestNG. Extend Reports generates HTML documents that serve as interactive and detailed test execution reports. It provides reports that can depict results as pie charts along with features such as logging and screenshots. Moreover, TestNG provides a report by default, but they are generally less detailed. Additionally, while TestNG provides default reports, they lack the level of detail and interactivity that Extent Reports offer, such as graphical representations, logs, and screenshots.

How to use extent reports in selenium?

Extent Reports has two major components or classes namely:

  1. ExtentReports class
  2. ExtentTest class

Syntax

Image description

ExtentReports class generates the HTML reports and accepts two arguments.

First argument is the path where the report has to be generated and the second argument is whether the file has to be overwritten or a new file should be created.

True will be the default value to overwrite all the existing data.

The ExtentTest class logs each individual test step to the HTML report, allowing you to track the progress and results of the test case

The ExtentReports class and ExtentTest class can be used with the below built-in methods:

  1. startTest: To execute preconditions of a test case
  2. endTest: To execute postconditions of a test case
  3. Log: To log the status of each step
  4. Flush: Flush: This method clears any previous data from the memory and writes the final report to the designated output file.

How to generate reports?

  1. Add the extentreports-java-2.41.2.jar to the project buildpath.
  2. Create a new java class and add the below code:

Image description

Test execution begins with the startTest() method, annotated with @BeforeClass, which initializes the Extent Reports object. This method sets up the report by specifying the output file location and initializing the test case.

@test methods opens the chrome browser and launches the URL and validates the page Title with the expected and logs the test case status as Pass or Fail. endTest() method executes postconditions of the test case

How to log test steps and results?

Test Status can be PASS, FAIL, SKIP and INFO.

Image description

The log() method accepts two arguments, first is test status and second is message to be displayed in the report.

How to Add Screenshots to Extent Report?

Screenshots can be captured in the report if a test fails.

Image description

Benefits of using extent reports

  1. It is open source.
  2. It can be customized as per the requirements.
  3. It provides pictorial and pie chart representations of test results
  4. It can be easily integrated with frameworks like Junit , TestNG and Nunit.
  5. It allows users to attach screenshots when the test fails.
  6. It logs the test report for a detailed summary of the tests.

Conclusion:

The significance of reporting in Selenium using ExtentReports is to provide comprehensive and customizable reports. Extent Reports help to communicate and track defects effectively with stakeholders. The final goal is to lead to better testing approaches and faster defect resolution.

Source: This article was originally published at testgrid.io.

Top comments (0)