Generate PDF Report of Selenium Test Execution
When using Selenium WebDriver for testing, testers often need to generate test execution report. Sure, TestNG does a great job at producing a report in HTML and XML format. But what if there is a need to generate a PDF report? We can very much do this with help of iText PDF library and TestNG listeners. We will see how to do it in this post.
A small background:
To generate PDF report, we have used iText PDF library here. It’s an open source library which can be used to manage PDF files. We have already seen how to use iText to read PDF content using Selenium WebDriver code. In this tutorial, we’ll use this library with custom TestNG listener to generate a PDF report at the end of test execution. This custom TestNG listener is created by Thierry Janaudy in Java using ITestListener.
OK, enough with the talks. Let’s get into action.
- Download IText JAR file and custom listener.
- Reference the downloaded JAR file in your project.
- Copy the custom listener(JyperionListener.java) in your project’s source directory. Make required modification in that file to get rid of any errors.
- Add JyperionListener.class as listener in class file for which you want the report generated. You can add it as a listener by making its reference at your class declaration. Similar to below code.
123@Listeners(JyperionListener.class)public class PDFReportGen {} - Run your test using TestNG.
- At the end of your test execution, you should see PDF file generated in your project’s root directory.
Below is a screenshot of PDF report generated.
The report contains some useful information like stack trace and test cases duration.
So to summarize, iText API is pretty handy in case you want to deal with PDF files in your Selenium WebDriver project. Give it a try in your project and let us know how it goes for you.
Hello Guys,
Not able to see any PDF in my root directory as i have do all the required changes in the java file and make the JyperionListener as abstract class.
public abstract class JyperionListener implements ITestListener {
please suggest.
Try http://uttesh.github.io/pdfngreport/ its maven library, using this we can generate the pdf report.
Hi, after i downloading the ECLIPSE SAMPLE code and try to run it is showingerror as “An internal error occurred during: “Launching Pdfngreport-Sample_testng-1.xml”.
java.lang.NullPointerException”
I can not move further, any working code please , it will be appriciate
Hi guys,
Need help in Sleenium Webdriver.
I want a code to download pdf .
Scenario
When i click on certain button it directs me to a pdf page
Then I have used the code for Save button (C+S ). System shows the pop up of save.
I have used the code for Saving pdf but code is not working.
FirefoxProfile prof = new FirefoxProfile();
WebDriver wd = new FirefoxDriver(prof);
prof.setPreference(“browser.download.dir”,”D:\\JITENDRA\\Seleniumdata”);
prof.setPreference(“browser.download.folderList”, 2);
prof.setPreference(“browser.helperApps.neverAsk.saveToDisk”, “application/pdf”);
How to generate PDF report for more then once class e.g suppose we have 3 class Class A, Class B and Class C. We have added all 3 classes in testng.xml and run same time then how can we generate report of all 3 classes .
U can use testng listener.
Add up the listeners section to this suite level in testng.xml file. That way, all of your tests would essentially leverage the listener that you wrote.
sorry guys, do you know how to insert an image screenshot ?