How to Generate Extent Reports in Selenium

The open-source library used for test automation in selenium is ExtentReports. Testing frameworks; NUnit, JUnit, etc can be integrated with the library. In-built reports are created in selenium through the frameworks of TestNG and JUnit, but Extent reports are still the first choice of the Automation testers of selenium. The extent reports in selenium give more insight into the execution of the automation scripts. The results are depicted as pie charts and are basically HTML documents. Are details like the snapshots, and custom logs can be generated through the library. 

After the successful run of an automated test, a report of test execution is to be generated by the testers. A default report is provided by the TestNG, but the details are not provided. With the applicability of the ExtentReport API, interactive reports could be generated through simple configurations. Frameworks of Java and .Net such as JUnit, NUnit, and TestNG, etc. are supported by the library.

Dependency of the extent report maven

Source

The selenium extent reports consists of two classes:

1. ExtentReports class

The tester specifies a path and based on the path specified, HTML reports are generated from the ExtentReports class. Either there is the generation of a new report or the overwriting of an existing report. It is decided based on the Boolean value. The default Boolean value is “true” which means that there will be overwriting of all data that existed in the past.

2. ExtentTest class

Test steps are logged through the ExtentTest class onto the HTML reports generated through the above method.

3. Extent HtmlReporter

An HTML file is created by this class.

The listed built-in functions can be used with classes that are listed below.

  • startTest: The preconditions of a case in the test is executed
  • endTest: The postconditions of a case in the test are executed.
  • Log method: The status of every step in the test is logged onto the generated HTML report through the Log.
  • Flush: A new report is created removing the data on a previous report.

The following values are used to indicate the status of a test: PASS, SKIP, FAIL, and INFO.

How to create Extent Reports

A few prerequisites for generating an extent report are:

  • Preinstallation and setup of Java.
  • The installation of TestNG.
  • Downloading of the Extent Report Jars (Version 2.41.2).
  • Requirement of extent-config.xml that allows the configuration of HTML report.

Basic steps that are required to follow for the generation of extent reports are:

  • A testing project is to be created.
  • The extent of the report jar file is to be downloaded.
  • The extent report jar file is added into the project.
  • An implemented class is created.

Steps for Generating an Extent Report

  • The extent reports are able to be generated in WebDriver of selenium through the importation of a jar file which is extentreports-java-2.41.2.jar. The jar file contents are to be extracted into a directory once it gets downloaded.
  • Jar files extracted from the downloaded ZIP file are to be added to the build project. The option Build Path -> Configure Build Path is used.
  • A Java class is newly created for the extent reported through the following code. 

Source

Explanation of the code

With the startTest method, the test execution begins. The object of extent reports is initialized. The extent report object uses any user-defined path as a parameter passed onto it.

@beforeclass: The use of the extent reports is demonstrated by the code. 

@test: The following actions are automated through this class:

  • Opening of the chrome browser through the URL www.google.com.
  • Page title is to be validated with the value as expected once it gets opened.
  • The status of the case of the test is to be logged as a PASS or a FAIL using the extent reports log method.

@afterclass: Code for the execution of a case of test postconditions are included in this class. It includes the code for the test ending or report flushing. Without the use of the method flush(), the extent reports in selenium won’t be generated.

Capture screenshots in Extent Reports

1. Through the method of capture

  • There are several benefits to using a screenshot capture during the test. 
  • If software starts acting erroneously, then the testers could identify the problem through observing the screenshots. 
  • Therefore, if the test execution encounters any issues, the screen captures will allow the testers to debug the test script. 
  • Screenshots should be captured only during the failure of a test, as more memory is consumed by the captures.
  • The following code used for capturing a screenshot is taken from

Source

2. Explanation of the code

  • The current selenium WebDriver is captured as a screenshot through the method of getScreenShotAs(). It is then stored in various forms of output.

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.File);

  • An object of the file is returned through the method of getScreenShotAs and stored in a variable of the file. For using this method, it is mandatory to cast the instance of the web driver to take a screenshot.

File Dest = new File(“scr/../Errimages/” + System.currentTimeMillis()+ “.png”);

  • A folder with the name of “ErrImages” is created within the folder “src” through the above code. The file is saved in the created folder with the name of the file as the time of the current system.

 String errflpath = Dest,getAbsolutePath();

Fileutils.copyFile(scrFile, Dest);

returnerrflpath;

  • Error images generated are copied in a destination folder.

3. Log Method

  • A built-in method is used in the log method. The addScreenCapture of the class Extent is used to capture a screenshot and then appended to the generated extent reports.

test.log(LogStatus.Fail,test.addScreenCapture(capture(driver))+ “Test Failed”);

Advantages of Extent Reports

  • The use of the extent reports is easy.
  • Pie charts are used for displaying the results.
  • Percentage of a passed case of a test is provided.
  • The ability to add the details of the environment in a natural way.
  • Compared to the other reports, the extent report offers the provision of customization.
  • More interactive reports can be generated through extent API. The interactive reports might be in the form of a graphical view, screenshot capture during every testing step, a dashboard view, and reports that could be emailed.
  • Integration with the JUnit, TestNG, and NUnit frameworks is possible.
  • Parallel execution of test runs is possible with the generation of a single report for all the parallel runs.
  • The report can be configured.
  • For the execution of every test case, the time taken is displayed. 
  • Easy tracking of the runs of multiple test cases in a single suite is possible.
  • Based on the reports of the test, the results can be filtered out. Also, stepwise results can be filtered out through the options of pass/fail/info.
  • Categorizations of reports are provided for categories like Regression/functional, etc.
  • Logs of the test steps can be added to the reports.

Example of Extent Reports in Selenium

An example is shown to let you understand the process of generation of extent report maven.

Step 1: A maven project and a class is to be created BasicExtentReport.java with the addition of a code available here 

  • A HTML standalone file is generated from the ‘ExtentHtmlReporter’ class. 
  • ‘ExtentHtmlReporter(java.io.File)’ is accepted by this class and  the path ‘ExtentHtmlReporter(java.lang.String filePath)’. 
  • The file path is passed to save the reports. 
  • The method config()of the class ‘ExtentHtmlReporter’ allows various options for configuration. Items can be changed through the code to make changes in the look of the extent report including time stamp, report name, etc.
  • Status for each step of the test is reported by the ‘AfterMethod’ and thereby the status of the test is updated  like Pass, Fail, skip, etc. in the occurrence of any error, a message is generated to give the information. It is done through ‘result.getThrowable()’.
  • Labels of the status are created using the MarkupHelper class that also colors the report status.

Step 2: Passing the OS and parameters of the browser is done through creating a testng.xml file. The code is available on the mentioned website.

Step3: Another code is added to the pom.cml file.

Step4: Running the program generates the resulting report.

Source

For viewing the failure reports, the failed tests are to be clicked. This will display the test status along with the reason for the test failure.

A few additional things related to extent reports are:

  • For the sake of appearance, the theme existing in a report can be changed.
  • As per the convenience of the user, the dashboard view may be hidden or shown.
  • In case of running multiple tests, a test case can be searched in a step-by-step manner.
  • The total time taken for running the test including the start time and end time is displayed to the user.
  • Additional filters are present in a report. The results can be filtered according to the status of the tests like passed test, failed test or test cases where only information is present.

Conclusion

For the generation of any customizable reports, the extent reports are the best way. Being an open-source library with an easy configuration with selenium, the extent reports have definitely become the best choice for all the automation testers. The article briefly discussed the extent reports, how to generate them, and their usefulness. The benefits can be reaped out through the knowledge of the software programming which is the basis of the generation of the extent reports.

If you are willing to learn and explore the benefits of software programming, check the course of Executive PG Programme in Software Development – Specialisation in Full Stack Development, offered by upGrad. This is a specially designed course for entry-level professionals within the age group of 21 to 45 years. Providing a platform for online lectures and interactive sessions with hands-on experiences, the course provides placement assurance in top industries. Our team is available for any queries.

Prepare for a Career of the Future

INDUSTRY TRUSTED LEARNING - INDUSTRY-RECOGNIZED CERTIFICATION.
Apply Now

0 replies on “How to Generate Extent Reports in Selenium”

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses

×