Why do we need reporting?

When we are using Selenium or any other automation tool, we are performing operations on the web application. But our purpose of automation is not just to exercise the Application Under Test. We, as an automation tester are supposed to test the application, find bugs and report it to the development team or higher management. Here the reporting gets importance for software Testing process

TestNG Reporting

TestNG library provides a very handy reporting feature. After execution, Testng will generate a test-output folder at the root of the project. This folder contains two type of Reports- Index.html: This is the complete report of current execution which contains information like an error, groups, time, reporter logs, testng XML files.

emailable-report.html: This is the summarize report of the current test execution which contains Test Case message in green (for pass test cases) and red(for failed test cases) highlight.

How to customize TestNG Report

TestNG reporting is quite handy but still, sometimes we need some less data in reports or want to display reports in some other format like pdf, excel, etc. or want to change report’s layout. There can be two ways we can customize TestNG report

Using ITestListener Interface: Using IReporter Interface:

ITestListener Interface

We use this interface when we need to customize real time report. In other words, if we are executing the bunch of test cases in a TetNG suite and we want to get the report of each test case, then after each test case we need to implement ITestListener interface. This interface will override onTestFailure, onTestStart, onTestSkipped method to send the correct status of the current test case.

Here are the steps we will follow

Create a class say RealGuru99Report and implement iTestListener in it. Implement methods of iTestListener Create test method and add RealGuru99Report class as a listener in Test Method class.

Code Example RealGuru99TimeReport.java is the real time reporting class. It will implement ITestListener interface for reporting TestGuru99RealReport.java is the test case for real report The output will look like-

IReporter Interface

If we want to customize final test report generated by TestNG, we need to implement IReporter interface. This interface has only one method to implement generateReport. This method has all the information of a complete test execution in the List, and we can generate the report using it. Code Example Guru99Reporter.java is the file used to customize report TestGuru99ForReporter.java is a demo for Custom reporting Output will be like-

PDF and Email of Reports

The above report implementation is quite simple and clear to get you started with report customization. But in corporate environment, you will need to create highly customized reports. Here is the scenario we will be dealing with

Create Custom Report in PDF form Take Screenshots ONLY on Errors. Link to screenshots in PDF Send Email of the PDF

The PDF report looks like this

To create pdf report we need a Java API IText. Download it here . There is another custom listener class which is actually implementing this IText jar and creating a pdf report for us. Download it here Above figure shows the default format of the PDF report generated. You can customize it Here is how we will approach this Step 1) Create a Base Class Step 2) Customize JypersionListerner.Java (PDF creation code) Step 3) Create a TestGuru99PDFEmail.java which will execute test cases , create PDF Step 4) Append code to TestGuru99PDFEmail.java to send PDF report via email Let’s look into these steps Step 1) Create Base Class This base class has functions to create WebDriver and Take Screenshot Step 2) Customize JypersionListener.java We will stick with the default report format. But we will make 2 customizations

Adding code to instruct JypersionListener to take screenshot on Error Attaching the link of the screenshot take in the PDF report

Add code to attach the screenshot to the PDF report

Step 3) Create a TestGuru99PDFEmail.java which will execute test cases , create PDF

Here we will add JyperionListener.class as listener We will Execute 3 test cases. Using Assert.assertTrue we will fail 2 test cases while passing just one. Screenshot will be taken for the failed test cases only as per our customizations

Step 4) Append code to TestGuru99PDFEmail.java to send PDF report via email

We will use the annotation @AfterSuite to send email of the PDF report We will be sending email using Gmail To enable Email, need to import many library files like mail.jar, pop3.jar, smptp.jar, etc Before you execute this, do enter the from, to email address and password

Download the complete project here Note: When we click on the screen shot link in pdf, it shows security dialog. We have to allow this dialog to open pdf.

The email so generated will look like this

Summary:

TestNG has an inbuilt reporting ability in it. After a complete execution of test cases, TestNG generates a test-output folder in the root of the project. In the test-output folder, there are two main reports, index.html, and emailable-report.html. To customize TestNG report we need to implement two interfaces, ITestListener and IReporter. If we need to get a report in between execution, we need ITestListener. For creating a final report after complete execution, we need to implement IReporter. Taking the screenshot, in Selenium WebDriver, we need to type cast WebDriver to TakesScreenShot interface. To generate pdf reports we need to add IText jar in the project.

Download the Selenium Project Files for the Demo in this Tutorial