How to Generate Test Report in Selenium: Simple Guide
To generate test reports in Selenium, use a testing framework like
TestNG which automatically creates HTML reports after test execution. For more detailed and customizable reports, integrate ExtentReports with Selenium tests to log steps and generate rich HTML reports.Syntax
Here is the basic syntax to generate a test report using TestNG and ExtentReports in Selenium:
@BeforeTest: Setup report and driver.@Test: Write test steps and log results.@AfterTest: Flush and save the report.
java
import org.testng.annotations.*; import com.aventstack.extentreports.*; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; public class TestReportExample { ExtentReports extent; ExtentTest test; @BeforeTest public void setup() { ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("testReport.html"); extent = new ExtentReports(); extent.attachReporter(htmlReporter); } @Test public void sampleTest() { test = extent.createTest("sampleTest", "Sample test to demonstrate report"); test.pass("Step 1 passed"); test.pass("Step 2 passed"); } @AfterTest public void tearDown() { extent.flush(); } }
Output
A file named 'testReport.html' is created with a detailed test report showing passed steps.
Example
This example shows how to generate a simple Selenium test report using TestNG and ExtentReports. It creates a report file with test status and logs.
java
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.*; import com.aventstack.extentreports.*; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; public class SeleniumReportExample { WebDriver driver; ExtentReports extent; ExtentTest test; @BeforeTest public void setup() { ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter("seleniumTestReport.html"); extent = new ExtentReports(); extent.attachReporter(htmlReporter); System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); driver = new ChromeDriver(); } @Test public void openGoogle() { test = extent.createTest("openGoogle", "Open Google homepage"); driver.get("https://www.google.com"); if(driver.getTitle().contains("Google")) { test.pass("Google homepage opened successfully"); } else { test.fail("Google homepage did not open"); } } @AfterTest public void tearDown() { driver.quit(); extent.flush(); } }
Output
A file named 'seleniumTestReport.html' is generated showing the test result as pass or fail with logs.
Common Pitfalls
Common mistakes when generating Selenium test reports include:
- Not calling
extent.flush()after tests, so the report file is empty. - Not initializing the reporter before tests, causing null pointer errors.
- Logging test steps outside the test methods, which leads to missing logs.
- Using hardcoded paths without relative or configurable options, causing file not found errors.
java
/* Wrong way: Missing flush() call */ ExtentReports extent = new ExtentReports(); ExtentTest test = extent.createTest("Test"); test.pass("Step passed"); // Missing extent.flush(); so report file is empty /* Right way: Call flush() after tests */ extent.flush();
Quick Reference
Tips for generating Selenium test reports:
- Use
TestNGfor built-in HTML reports. - Use
ExtentReportsfor detailed, customizable reports. - Always call
extent.flush()to save the report. - Log meaningful test steps for clarity.
- Keep report file paths configurable.
Key Takeaways
Use TestNG or ExtentReports to generate Selenium test reports easily.
Always initialize and flush the report to save test results.
Log clear test steps inside test methods for better reports.
Avoid hardcoding file paths; use configurable locations.
Check generated HTML report files after test execution.