如何为单个步骤使用Extent Reports及生成其日志?
Hey there! Let's walk through how to add detailed step-by-step logs to your test using Extent Reports. Based on your existing test code, here's how you can enhance it to track every individual action clearly:
1. First, Ensure Extent Reports is Properly Initialized
Before your tests run, set up the core ExtentReports instance and attach a reporter (like an HTML reporter) to generate the final output file. This usually goes in a @BeforeSuite or @BeforeTest method:
import com.relevantcodes.extentreports.ExtentReports; import com.relevantcodes.extentreports.ExtentTest; import com.relevantcodes.extentreports.LogStatus; import org.testng.annotations.BeforeSuite; public class YourTestClass { protected ExtentReports extent; protected ExtentTest extentTest; @BeforeSuite public void setupExtent() { // Initialize ExtentReports with the path to your HTML report extent = new ExtentReports("./test-output/ExtentReport.html", true); // Add context info to the report (optional but helpful) extent.addSystemInfo("Environment", "QA") .addSystemInfo("Browser", "Chrome"); } }
2. Add Step-by-Step Logs to Your Test
In your SingleSection test method, after starting the test with extent.startTest(), use extentTest.log() to record every individual action. Each log entry can have a status (INFO, PASS, FAIL, etc.) and a clear description of what you're doing.
Here's a modified version of your test with detailed step logs:
@Test(testName = "Validate SinglePage and Multiple Page", enabled = true, priority = 1, groups = {"Section Formatting"}) public void SingleSection(String username, String password, String viewName, String r1, String r2, String r3, String r4, String r5, String SecItem1, String SecItem2, String DispStyle, String fType) throws InterruptedException { // Start the test instance (add dynamic data like viewName for clarity) extentTest = extent.startTest("SingleSection Test: " + viewName); try { // Step 1: Perform login action extentTest.log(LogStatus.INFO, "Attempting to login with username: " + username); // Insert your actual login code here extentTest.log(LogStatus.PASS, "Successfully logged in as: " + username); // Step 2: Navigate to target view extentTest.log(LogStatus.INFO, "Navigating to view: " + viewName); // Insert your navigation code here extentTest.log(LogStatus.PASS, "Successfully navigated to: " + viewName); // Step 3: Select section items extentTest.log(LogStatus.INFO, "Selecting section items: " + SecItem1 + " & " + SecItem2); // Insert your item selection code here extentTest.log(LogStatus.PASS, "Section items selected successfully"); // Step 4: Apply display style extentTest.log(LogStatus.INFO, "Applying display style: " + DispStyle); // Insert your display style code here extentTest.log(LogStatus.PASS, "Display style applied correctly"); // Step 5: Verify test results extentTest.log(LogStatus.INFO, "Verifying test results (r1 to r5)"); // Insert your result verification code here extentTest.log(LogStatus.PASS, "All test results verified successfully"); } catch (Exception e) { // Log failures with details, plus a screenshot if needed extentTest.log(LogStatus.FAIL, "Test failed with error: " + e.getMessage()); extentTest.log(LogStatus.FAIL, extentTest.addScreenCapture("path/to/failure-screenshot.png")); // Optional: Attach screenshot throw e; // Re-throw to mark the test as failed in TestNG } finally { // End the test instance to finalize logs extent.endTest(extentTest); } }
3. Don’t Forget to Flush the Report
After all tests finish running, flush the ExtentReports instance to write all collected logs to the HTML file. Add this in an @AfterSuite method:
@AfterSuite public void tearDownExtent() { extent.flush(); extent.close(); // Cleans up resources (optional but recommended) }
Quick Best Practices:
- Use LogStatus constants to categorize steps:
INFOfor ongoing actions,PASSfor successful steps,FAILfor failures,WARNINGfor non-breaking issues. - Include dynamic data (like
usernameorviewName) in logs to easily identify which parameterized test run the log belongs to. - Attach screenshots to failed steps to speed up debugging.
- Always end tests with
extent.endTest(extentTest)and flush the report to ensure no logs are lost.
内容的提问来源于stack exchange,提问作者Bob Jones

