非Maven Java项目Selenium Extent Report 2.41.2饼图全黑无结果
Hey Rashmi, let's tackle this Extent Reports issue you're facing with your non-Maven Selenium Java project. I’ve worked extensively with Extent Reports 2.x, so here’s exactly what’s likely going wrong and how to fix it:
1. You’re not properly creating/using ExtentTest instances
Extent Reports 2.41.2 requires a dedicated ExtentTest object for every test case to track PASS/FAIL status. If you only initialize ExtentReports but don’t bind test steps to an ExtentTest instance, no results will be logged.
Fix:
- Create an
ExtentTestat the start of each test method - Use
test.log(LogStatus.PASS/FAIL/INFO)to record each step - Always call
extent.endTest(test)after the test finishes - Don’t forget
extent.flush()at the end of all tests to write results to the HTML file
2. Missing dependent JAR files
Non-Maven projects often miss critical dependencies that Extent Reports 2.x relies on. The pie chart turning black is almost always a sign of missing the Freemarker JAR (used to render the report’s UI).
Required JARs for Extent Reports 2.41.2:
extentreports-2.41.2.jar(your main Extent JAR)freemarker-2.3.20.jar(mandatory for HTML rendering)json-simple-1.1.1.jar(handles data serialization)
Fix:
Download all three JARs, add them to your project’s build path, and ensure there are no ClassNotFound exceptions in your console.
3. Forgetting to call flush()
If you skip extent.flush(), none of your test results will be written to the HTML report. This is a super common oversight!
Fix:
Call extent.flush() in your teardown method (after all tests have run and endTest() has been called for each ExtentTest instance). You can also call extent.close() afterward to clean up resources.
Here’s a complete, tested code snippet tailored to your setup:
import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class GenerateExtentReports { private static ExtentReports extent; private static WebDriver driver; // Setup: Initialize Extent Reports and Selenium WebDriver public static void setup() { // Set ChromeDriver path (update to your actual path) System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe"); driver = new ChromeDriver(); // Initialize Extent Reports - true = overwrite existing report, false = append extent = new ExtentReports("test-output/SeleniumExtentReport.html", true); // Add system info for context extent.addSystemInfo("OS", "Windows 10") .addSystemInfo("Browser", "Chrome") .addSystemInfo("Selenium Version", "3.141.59"); } // Sample test method public static void testValidLogin() { // Create a test instance for this specific test case ExtentTest loginTest = extent.startTest("Valid Login Test", "Verify user can login with correct credentials"); try { driver.get("https://your-test-application.com/login"); loginTest.log(LogStatus.INFO, "Navigated to login page"); // Enter username driver.findElement(By.id("username")).sendKeys("test_user"); loginTest.log(LogStatus.PASS, "Successfully entered valid username"); // Enter password driver.findElement(By.id("password")).sendKeys("test_pass"); loginTest.log(LogStatus.PASS, "Successfully entered valid password"); // Click login button driver.findElement(By.id("login-btn")).click(); loginTest.log(LogStatus.PASS, "Clicked login button"); // Verify login success if (driver.getTitle().equals("Dashboard")) { loginTest.log(LogStatus.PASS, "Login successful - navigated to dashboard"); } else { loginTest.log(LogStatus.FAIL, "Login failed - unexpected page title: " + driver.getTitle()); } } catch (Exception e) { // Log failure with exception details loginTest.log(LogStatus.FAIL, "Test failed due to exception: " + e.getMessage()); // Optional: Attach a screenshot to the report // loginTest.log(LogStatus.FAIL, "Screenshot: " + loginTest.addScreenCapture("C:\\screenshots\\login_failure.png")); } finally { // End the test instance to finalize results extent.endTest(loginTest); } } // Teardown: Clean up resources and write report public static void teardown() { if (driver != null) { driver.quit(); } // Critical: Flush results to HTML file extent.flush(); // Close Extent Reports to release resources extent.close(); } public static void main(String[] args) { setup(); testValidLogin(); teardown(); } }
- Double-check that all three required JARs are in your project’s build path
- Ensure no
ClassNotFoundExceptionorNoClassDefFoundErrorappears in your console - Verify that every test method has its own
ExtentTestinstance withstartTest(),log(), andendTest()calls - Confirm
flush()is called after all tests complete
Once you implement these fixes, your report should show proper PASS/FAIL statuses and a correctly colored pie chart.
内容的提问来源于stack exchange,提问作者Rashmi S

