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

解决Cucumber+Selenium测试报告生成后压缩邮件的时序问题

Solution: Fix Timing Issue with Extent Report Generation and Zip/Email

The core problem here is a lifecycle mismatch: your JUnit @AfterClass method was running before Cucumber's Extent Report formatter finished writing the final report, and you were also configuring the report after scenarios had already run (which meant those system details weren't being included properly). Here's how to fix it step by step:

1. Correct Extent Report Setup Timing

First, move all Extent Report configuration to run before any scenarios execute. This ensures system info and settings are included in the final report, and we avoid modifying the report after zip creation.

Update your runner class to use @BeforeClass instead of @AfterClass for setup:

@CucumberOptions( 
    features = {"featurefiles/DefineStaffType.feature"}, 
    glue = {"stepdefinitions"}, 
    monochrome = true, 
    plugin = {
        "pretty:STDOUT", 
        "json:target/cucumber.json", 
        "junit:target/cucumber.xml", 
        "com.cucumber.listener.ExtentCucumberFormatter:output/report.html"
    }, 
    tags = {"@Scenario1, @Scenario2, @Scenario3"} 
) 
public class DefineStaffTypeRunner { 

    @BeforeClass 
    public static void setupExtentReport() { 
        Reporter.loadXMLConfig(new File("configuration\\extentconfig.xml")); 
        Reporter.setSystemInfo("User Name", System.getProperty("user.name")); 
        Reporter.setSystemInfo("Time Zone", System.getProperty("user.timezone")); 
        Reporter.setSystemInfo("OS", "Windows 10 64 Bit"); 
        Reporter.setSystemInfo("Selenium Version", "3.1.0"); 
        Reporter.setSystemInfo("Maven Version", "1.9"); 
        Reporter.setSystemInfo("Java Version", "1.9"); 
        Reporter.setTestRunnerOutput("Define Staff Type Test Execution"); 
    } 
}

2. Run Zip/Email After All Scenarios (and Report Finalization)

Use Cucumber's @AfterAll hook instead of JUnit's @AfterClass. This hook runs after all scenarios complete and after Cucumber's plugins (like the Extent formatter) have finished writing their output. Add this to your CucumberHooks class:

public class CucumberHooks extends GenericBaseClass { 
    DriverMethods dm = new DriverMethods(); 
    CaptureScreenshot cs = new CaptureScreenshot(); 
    static MailHandlingUtility mhu = new MailHandlingUtility(); 

    @Before 
    public void launchBrowser(Scenario currentscenario) throws IOException { 
        this.scenario = currentscenario; 
        driver = getCurrentDriver(); 
        dm.maximizeWindow(); 
    } 

    @After 
    public void tearDownScenario(Scenario currentscenario) throws IOException { 
        scenario.write("Scenario finished: " + currentscenario.getName()); 
        cs.captureScreenshot(scenario); // Fixed typo: cature -> capture
        driver.close(); 
        driver.quit(); 
        driver = null; 
    } 

    @AfterAll 
    public static void postTestActions() throws IOException, EmailException { 
        // Optional: Add a short delay to ensure report file is fully written to disk
        Thread.sleep(2000); 
        FileConversion.convertToZip("output"); 
        mhu.sendMailWithAttachment(); 
    } 
}

Why This Works

  • @BeforeClass in Runner: Configures Extent Report settings before any scenarios run, so all system info is included in the final report.
  • @AfterAll in Hooks: Runs after every scenario and Cucumber's plugins have finalized their output. This guarantees the report.html file is fully written to the output folder before zipping.
  • Optional Sleep: A 2-second delay adds a safety buffer to handle any file system write delays (especially useful on slower systems).

Key Notes

  • Ensure your FileConversion.convertToZip method correctly includes all files in the output folder (including report.html and screenshots).
  • Verify the output folder exists before test execution (the Extent formatter usually creates it automatically, but adding a check in setupExtentReport can prevent errors).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:06:25