
🏹Introduction
How to Customize: In Selenium automation testing, generating detailed reports is crucial to track test execution status, detect failures, and share results with the development team or stakeholders. TestNG provides built-in reports, but we can also customize reports, generate PDFs, and send reports via email.
In this guide, you’ll learn:
✅ How to generate & customize TestNG reports
✅ Creating PDF reports with screenshots on test failures
✅ Sending TestNG reports via email
🏹TestNG Reporting in Selenium
What Are TestNG Reports?
How to Customize: After test execution, TestNG generates reports automatically in the test-output folder.
There are two main TestNG reports:
1️⃣ index.html – Detailed report with logs, execution times, and test details.
2️⃣ emailable-report.html – Summary report with passed/failed test cases.
🔹 How to View Reports?
- Navigate to test-output/ folder.
- Open index.html or emailable-report.html in a web browser.
🏹How to Customize TestNG Reports
How to Customize: Sometimes, built-in TestNG reports may not meet project requirements. We can customize reports using:
ITestListener Interface (Real-time Reports)
Use Case: Customize reports during test execution (real-time reporting).
Steps to Implement ITestListener Interface:
1️⃣ Create a new class (CustomTestNGListener.java).
2️⃣ Implement the ITestListener interface.
3️⃣ Override methods to capture test status.
4️⃣ Add the listener to test classes.
🏹Code Example: Customizing TestNG Reports Using ITestListener
java
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
public class CustomTestNGListener implements ITestListener {
@Override
public void onTestStart(ITestResult result) {
Reporter.log(“Test Started: ” + result.getName(), true);
}
@Override
public void onTestSuccess(ITestResult result) {
Reporter.log(“Test Passed: ” + result.getName(), true);
}
@Override
public void onTestFailure(ITestResult result) {
Reporter.log(“Test Failed: ” + result.getName(), true);
}
@Override
public void onTestSkipped(ITestResult result) {
Reporter.log(“Test Skipped: ” + result.getName(), true);
}
@Override
public void onFinish(ITestContext context) {
Reporter.log(“Test Execution Completed”, true);
}
}
🏹How to Use ITestListener in TestNG?
java
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.Assert;
@Listeners(CustomTestNGListener.class)
public class TestCases {
@Test
public void testPass() {
Assert.assertTrue(true);
}
@Test
public void testFail() {
Assert.fail(“This test is intentionally failed”);
}
}
Test execution results will now be logged in real time.
🏹 IReporter Interface (Custom Final Reports)
How to Customize: Use Case: Customize final TestNG reports after test execution.
Steps to Implement IReporter Interface:
1️⃣ Create a new class (CustomTestNGReport.java).
2️⃣ Implement the IReporter interface.
3️⃣ Override generateReport() to modify final reports.
Code Example: Customizing Final TestNG Report with IReporter
java
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.xml.XmlSuite;
import java.util.List;
public class CustomTestNGReport implements IReporter {
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
System.out.println(“Custom Report Generated in: ” + outputDirectory);
}
}
🏹Generating TestNG Reports as PDFs
Use Case: Convert TestNG reports into a PDF format for sharing.
🔹 Steps to Generate PDF Reports:
1️⃣ Use IText library to generate PDFs.
2️⃣ Capture test results and store them in a PDF.
3️⃣ Attach screenshots for failed test cases.
Code Example: Generate PDF Report in Selenium TestNG
java
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
public class TestNGPDFReport {
public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(“TestNGReport.pdf”));
document.open();
document.add(new Paragraph(“TestNG Report”));
document.add(new Paragraph(“Test Passed: 5”));
document.add(new Paragraph(“Test Failed: 2”));
document.add(new Paragraph(“Test Skipped: 1”));
document.close();
System.out.println(“PDF Report Created Successfully!”);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This will create TestNGReport.pdf with test results.
🏹Taking Screenshots on Test Failure & Attaching to PDF Reports
How to Customize: Use Case: Capture screenshots only when tests fail & attach them to the PDF.
Steps to Capture Screenshots in Selenium WebDriver:
1️⃣ Typecast WebDriver to TakesScreenshot
2️⃣ Use getScreenshotAs() method to capture screenshots
3️⃣ Save screenshot & add to PDF report
Code Example: Capturing Screenshots on Failure
java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class ScreenshotOnFailure {
WebDriver driver;
public void takeScreenshot(String testName) {
try {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File(“./screenshots/” + testName + “.png”));
} catch (Exception e) {
e.printStackTrace();
}
}
}
This will capture a screenshot when a test fails and save it in the /screenshots/ folder.
🏹Sending TestNG Reports via Email
How to Customize: Use Case: Email TestNG reports to the team after test execution.
Steps to Send Email in Java:
1️⃣ Use JavaMail API (mail.jar, smtp.jar, pop3.jar)
2️⃣ Attach TestNGReport.pdf to email
3️⃣ Send email using Gmail SMTP
Code Example: Sending PDF Report via Email
java
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;
public class EmailReport {
public static void sendReport() {
String to = “recipient@example.com”;
String from = “sender@example.com”;
String host = “smtp.gmail.com”;
Properties properties = System.getProperties();
properties.setProperty(“mail.smtp.host”, host);
properties.setProperty(“mail.smtp.port”, “465”);
properties.setProperty(“mail.smtp.auth”, “true”);
properties.setProperty(“mail.smtp.starttls.enable”, “true”);
Session session = Session.getDefaultInstance(properties);
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(“TestNG Report”);
message.setText(“Find the attached TestNG Report.”);
Transport.send(message);
System.out.println(“Email Sent Successfully!”);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
📌 This will send the TestNG report as an email attachment.
🏹Summary
✅How to Customize: Customize TestNG reports using ITestListener & IReporter.
✅ Convert TestNG reports to PDFs with IText library.
✅ Capture screenshots on test failure & attach to reports.
✅ Send TestNG reports via email after execution.
Implement these techniques to enhance your Selenium TestNG reporting!
Testing Reports
Testing Selenium Download