解决Cucumber+Selenium测试报告生成后压缩邮件的时序问题
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
@BeforeClassin Runner: Configures Extent Report settings before any scenarios run, so all system info is included in the final report.@AfterAllin Hooks: Runs after every scenario and Cucumber's plugins have finalized their output. This guarantees thereport.htmlfile is fully written to theoutputfolder 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.convertToZipmethod correctly includes all files in theoutputfolder (includingreport.htmland screenshots). - Verify the
outputfolder exists before test execution (the Extent formatter usually creates it automatically, but adding a check insetupExtentReportcan prevent errors).
内容的提问来源于stack exchange,提问作者Rohit

