You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Extent Report的每个步骤中附加截图?混合框架下截图位置异常问题问询

Can I Attach Screenshots to Corresponding Steps in Extent Reports (Hybrid Framework)?

Absolutely, this is totally achievable! The key fix here is making sure you're attaching screenshots to individual test step nodes in Extent Reports, rather than dumping them directly to the root report object. Let's walk through how to get this working:

Core Issue Why Screenshots Are Bunching at the Bottom

Your current setup is likely adding screenshots to the global ExtentReports instance instead of tying them to specific ExtentTest nodes (which represent your individual steps or test actions). That's why all screenshots end up stacked at the bottom of the report.

Step-by-Step Solution

1. Initialize Extent Properly

First, set up your main report and create a parent test node for your entire flow. If you're using a framework like TestNG/JUnit, handle this in your test setup method:

// Initialize Extent Reports (run once per test suite)
ExtentReports extent = new ExtentReports();
extent.attachReporter(new ExtentSparkReporter("test-report.html"));

// Create a parent test node for your 4-page flow
ExtentTest mainTestFlow = extent.createTest("My App 4-Page End-to-End Flow");

2. Attach Screenshots Directly to Step Logs/Child Nodes

For each step you execute, take the screenshot and attach it to the corresponding step's log entry or a dedicated child test node. Here are two straightforward approaches:

Approach A: Attach to Step Log Entries

Use addScreenCaptureFromPath (or addMedia for newer Extent versions) directly with your step's info/pass/fail log:

// Step 1: Navigate to Page 1
mainTestFlow.info("Navigated to Page 1");
// Capture screenshot
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String screenshotPath = saveScreenshotToDisk(screenshot); // Custom method to save file to a accessible path
// Attach screenshot to this step's log
mainTestFlow.addScreenCaptureFromPath(screenshotPath, "Page 1 Loaded Successfully");

// Step 2: Complete action on Page 2
mainTestFlow.info("Filled out form on Page 2");
screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
screenshotPath = saveScreenshotToDisk(screenshot);
mainTestFlow.addScreenCaptureFromPath(screenshotPath, "Form filled on Page 2");

Approach B: Use Child Nodes for Structured Steps

If you want cleaner separation between steps, create child test nodes for each action and attach screenshots to those:

// Child node for Page 1 navigation
ExtentTest page1Step = mainTestFlow.createNode("Step 1: Access Page 1");
page1Step.info("Successfully navigated to Page 1");
page1Step.addScreenCaptureFromPath(screenshotPath, "Page 1 Initial State");

// Child node for Page 3 action
ExtentTest page3Step = mainTestFlow.createNode("Step 3: Confirm payment on Page 3");
page3Step.pass("Payment confirmed successfully");
page3Step.addScreenCaptureFromPath(screenshotPath, "Payment Confirmation Screen");

3. Hybrid Framework Specific Tips

  • Avoid Shared ExtentTest Objects: If you're using a page object model or reusable components, make sure each step references the correct ExtentTest node (pass it as a parameter if needed) to prevent screenshots from attaching to the wrong step.
  • Reusable Screenshot Utility: Build a helper method to capture, save, and return the screenshot path—this cuts down on repetitive code across your framework.
  • Extent 5.x+ Compatibility: For newer Extent versions, use MediaEntityBuilder for more flexibility:
    mainTestFlow.info("Step completed")
            .addMedia(MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath)
            .build());
    

Final Result

Once you implement this, each screenshot will appear directly under its corresponding step in the Extent Report, making your test results much easier to trace and debug.

内容的提问来源于stack exchange,提问作者Ashish Piplodiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 11:12:39