How to generate PDF report in Selenium with TestNG & iTextPDF?

We know that presentation matters for testing, so, it is always good to present a well-elaborated test report before the test manager. We have discussed a lot about different techniques of test report generation in Selenium, and logging with Log4j, ITestListener, and WebDriverListener. All these are the way to represent test execution in an elaborated manner. Today we are going to add another milestone in test reporting and it is about creating a PDF report of the entire test execution.

We will use the iText JAR file for pdf report generation and further we will integrate it with TestNG reporting. Hence, basically, we will use iText PDF with TestNG’s IReporter listener.

Before we go the implementation of PDF report generation functionality in our Selenium project, let’s have a look over some of the test report and log generation techniques in Selenium:

These are some of the awesome articles which will definitely help you in building your knowledge base.

Pre-Requisites for PDF report generation in Selenium

There are some pre-requisites for PDF report generation in Selenium. Those pre-requisites are listed as below:

  • Your project should be configured with iText Java API. Click here to download iText API. Once you have downloaded the JAR file, then add it to your project. I believe you know the steps to add external JAR files.
  • If you are using Maven, then you can create dependencies for the iText JAR file. Click here to get the maven dependencies.

Java definition of iText declaration to write to the PDF file

Following code is the Java definition to create a PDF file and addition of some para to the newly created file.

Explanation of the code

Here firstly we are creating the String definition of the path of the PDF file. Then, the instance of Document class creates a new PDF file. Further, PdfWriter.getInstance method is used give the path of the file. Further open() and close() method is used to open and close the file respectively. Meanwhile, open and close, we declared Author name, Title, Subject and Paragraph of the PDF report with addAuthor, addTitile, addSubject and add(new Paragraph()) methods respectively.

Steps to generate PDF report in Selenium

We will carefully implement above logic to generate PDF report with our test execution data. Let’s begin.

Step# 1: Create a separate class to define PDF actions

Here we create a separate class in which we have created a separate method for each action. Following code elaborate this step.

Step# 2: Implement IReporter and Merge with methods of

Now implement IReporter for test reporting and add the data to the PDF file by extending Following code explains this step.

Step# 3: Create any TestNG tests to validate the PDF report generation

Following test program contains two scenarios, in which, the first scenario fails the test and second scenario will pass the test. Further, reporting will be printed in PDF, Custom TestNG report and console as well.

Step# 4: testng.xml

Now run the sample program from the testng.xml file. Here do not forget to add the listeners.

PDF Report Generated

PDF Report in project

Console Output

Console output of PDF Report

PDF File

PDF Report Test execution

This was all about PDF report generation in the Selenium project. You can post your queries below and don’t forget to join our Facebook group for quick support.

Join Inviul fb group

Leave a Reply