
TestNG Reports Generation in Selenium
TestNG Reports Generation in Selenium: TestNG Reports are essential for tracking test execution status in Selenium. These reports provide insights into test case results, project status, and debugging information.
In this guide, you’ll learn:
✅ What are TestNG Reports?
✅ How to generate different TestNG reports (emailable-report.html
, index.html
)
✅ How to log custom messages using the Reporter Class
What are TestNG Reports?
TestNG Reports are automatically generated HTML reports that display:
- ✅ The number of passed, failed, and skipped test cases
- ✅ Execution time for each test case
- ✅ Detailed error logs for failed tests
While Selenium WebDriver automates web applications, it does not generate test reports. TestNG fills this gap by providing built-in reporting features.
How to Generate TestNG Reports in Selenium?
TestNG Reports Generation in Selenium: TestNG generates two types of reports:
1️⃣ Default HTML Reports (emailable-report.html
, index.html
)
2️⃣ Custom Reports using the Reporter Class
Let’s explore each method in detail.
Method 1: Generating emailable-report.html
Steps to Generate emailable-report.html
in TestNG
1️⃣ Execute the testng.xml
file
2️⃣ Refresh the project (Right-click on the project → Click Refresh or press F5)
3️⃣ Navigate to the test-output folder
4️⃣ Locate emailable-report.html
5️⃣ Right-click on emailable-report.html
→ Click Open With → Web Browser
Example Report Output (Passed Tests)
If all test cases pass, the report will look like this:
✅ DemoA Class: Passed
✅ DemoB Class: Passed
Example Report Output (Failed Tests)
If a test case fails, it will be displayed in red:
❌ DemoA Class: Passed
❌ DemoB Class: Failed
Method 2: Generating index.html
Report
Steps to Generate index.html
in TestNG
1️⃣ Execute testng.xml
file
2️⃣ Refresh the project → Open test-output folder
3️⃣ Locate index.html
4️⃣ Right-click on index.html
→ Click Open With → Web Browser
Features of index.html
Report
- Displays detailed execution summary
- Shows passed, failed, and skipped test cases
- Includes execution time for each test method
- Provides navigation to test logs and errors
Method 3: Using the Reporter
Class for Custom Logs
TestNG Reports Generation in Selenium: Along with the default TestNG reports, you can use the Reporter class to log custom messages in TestNG reports.
What is the Reporter Class in TestNG?
The Reporter.log()
method in TestNG allows you to add custom logs inside reports, making debugging easier.
Methods in Reporter Class
Method | Description |
---|---|
Reporter.log(String s); | Logs a simple message |
Reporter.log(String s, Boolean logToStandardOut); | Logs message and prints it in the console |
Reporter.log(String s, int level); | Logs message with a custom level |
Reporter.log(String s, int level, Boolean logToStandardOut); | Logs message with a level and prints to console |
Example: Using Reporter.log()
in Selenium TestNG
DemoA.java
java package com.test;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class DemoA {
@Test
public void testGoogleSearch() {
Reporter.log("Starting Google Search Test", true);
System.out.println("Google Search Test Passed");
Reporter.log("Google Search Test Completed Successfully", true);
}
}
DemoB.java
java package com.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class DemoB {
@Test
public void testInvalidElement() {
Reporter.log("Starting Invalid Element Test", true);
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com");
try {
driver.findElement(By.name("invalidElement")).sendKeys("Bye");
} catch (Exception e) {
Reporter.log("Element not found: " + e.getMessage(), true);
}
driver.quit();
Reporter.log("Invalid Element Test Completed", true);
}
}
How to View Reporter Class Logs in TestNG Reports?
1️⃣TestNG Reports Generation in Selenium: Execute testng.xml
2️⃣ Open test-output folder
3️⃣ Locate and open index.html
in a browser
4️⃣ Click on Reporter Output to see custom logs
Bonus: Viewing Execution Time in TestNG Reports
TestNG Reports Generation in Selenium: You can view the execution time of each test case using TestNG reporting tools.
Steps to View Execution Time:
1️⃣ Open index.html
2️⃣ Click on the Times tab
3️⃣ It will show how long each test method took to execute
Why Use TestNG Reports?
✅ Automatically generated reports after test execution
✅ Easily track passed, failed, and skipped test cases
✅ Custom logs using Reporter.log()
for better debugging
✅ Time analysis to optimize test execution
Conclusion
TestNG Reports Generation in Selenium: TestNG provides built-in report generation features to analyze test execution results.
Key Takeaways:
✔️ emailable-report.html
: Simple HTML report with test results
✔️ index.html
: Detailed report with logs, execution time, and errors
✔️ Reporter Class: Custom logs for debugging
By leveraging these TestNG reports, you can enhance your Selenium test automation and quickly identify test failures.