TestNG Reporting with Selenium WebDriver

Welcome to the “TestNG Reporting with Selenium WebDriver” blog. TestNG is a popular testing framework that provides comprehensive reporting capabilities to help us understand the results of our tests. With TestNG reporting, we can gain valuable insights into our test results, identify defects, and optimize our testing process.

In this blog, we will explore how to use TestNG reporting with Selenium WebDriver to improve our web testing.

TestNG Default Reports

TestNG provides a default HTML report that displays the summary of the test results along with the details of each test method, including its status, execution time, and stack trace. To generate this report, we can add the following line to our testng.xml file:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="MyTestSuite" verbose="1">
  <test name="MyTest" >
    <classes>
      <class name="MyTest" />
    </classes>
  </test>

  <listeners>
    <listener class-name="org.testng.reporters.EmailableReporter" />
  </listeners>
</suite>

This will generate an HTML report named “emailable-report.html” in the output directory of our project.

TestNG Custom Reporters

TestNG also allows us to create custom reporters to generate reports in different formats, such as PDF, Excel, or JSON. To create a custom reporter, we need to implement the IReporter interface and override its methods. Following is an example of a custom reporter that generates a PDF report:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.xml.XmlSuite;

public class PdfReporter implements IReporter {

   @Override
   public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
      Document document = new Document();
      
      try {
         PdfWriter.getInstance(document, new FileOutputStream(new File(outputDirectory + "/report.pdf")));
         document.open();
         
         for (ISuite suite : suites) {
            for (String testName : suite.getResults().keySet()) {
               ISuiteResult suiteResult = suite.getResults().get(testName);
               
               for (ITestResult testResult : suiteResult.getTestContext().getPassedTests().getAllResults()) {
                  document.add(new Paragraph("Test Passed: " + testResult.getName()));
               }
               
               for (ITestResult testResult : suiteResult.getTestContext().getFailedTests().getAllResults()) {
                  document.add(new Paragraph("Test Failed: " + testResult.getName() + " Reason: " + testResult.getThrowable().getMessage()));
               }
            }
         }
      } catch (DocumentException | IOException e) {
         e.printStackTrace();
      } finally {
         document.close();
      }
   }
}

In the above example, we are using the iText library to create a PDF document and write the test results to it. We then register our custom reporter in the testng.xml file as follows:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="MyTestSuite" verbose="1">
  <test name="MyTest" >
    <classes>
      <class name="MyTest" />
    </classes>
  </test>

  <listeners>
    <listener class-name="PdfReporter" />
  </listeners>
</suite>

This will generate a PDF report named “report.pdf” in the output directory of our project.

Conclusion

In conclusion, TestNG provides several built-in and custom reporters that allow us to generate different types of reports and analyze the test results of our Selenium WebDriver tests. By using these reporters, we can quickly identify any failed tests, errors, or issues that need to be fixed in our web application. Additionally, we can use these reports to share the test results with other team members, stakeholders, or clients.

You Might Like: