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

Cucumber JVM报告附加文件时遇Scenario空指针问题求助

Cucumber-JVM 附加文件问题解决及替代方案

先解决你当前的Scenario为空问题

你的代码里有两个明显的bug导致Scenario为空:

  1. DemoDef构造函数赋值错误:
    原代码中构造函数的参数名和成员变量名相同,且未使用this区分,导致成员变量testContext未被初始化,始终为null:

    // 错误代码
    TestContext textContext;
    public DemoDef(TestContext textContext){
       textContext = textContext; // 只是把参数赋值给自己,成员变量还是null
    }
    

    修复为:

    TestContext testContext;
    public DemoDef(TestContext testContext){
       this.testContext = testContext; // 使用this绑定成员变量
    }
    
  2. 确认Hook的@Before注解来源:
    确保AssertHook中的@Before是Cucumber的io.cucumber.java.Before,而非JUnit的org.junit.Before,否则钩子不会被Cucumber执行,Scenario无法设置到TestContext中。


向Cucumber-JVM报告附加文件的其他实现方式

1. 直接在Step Definition中注入Scenario参数

无需通过TestContext传递,Cucumber会自动将当前Scenario实例注入到步骤方法中,避免上下文传递的空指针问题:

import io.cucumber.java.Scenario;
import java.nio.file.Files;
import java.nio.file.Paths;

@Given("Is file downloaded successfully")
public void userIsOnLoginScreen(Scenario scenario) {
    SoftAssertions.assertSoftly(softAssertions -> 
        softAssertions.assertThat(testContext.pageObjectManager().getDemo().isFileDownloaded())
            .as("File Not Downloaded")
            .isTrue()
    );
    
    // 读取目标文件为字节数组(必须传入文件内容,而非文件名)
    try {
        byte[] fileBytes = Files.readAllBytes(Paths.get("/path/to/your/downloaded/file.xlsx"));
        scenario.attach(
            fileBytes, 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
            "Downloaded File"
        );
    } catch (Exception e) {
        // 处理文件读取异常
    }
}

注意:原代码中attach方法传入的第一个参数是字符串"FileName",这是错误的,Cucumber的attach需要传入文件的字节数组内容,而非文件名。

2. 使用@AfterStep/@BeforeStep钩子附加文件

在钩子中直接获取Scenario实例,根据步骤状态附加文件,比如仅在步骤成功时附加下载文件:

import io.cucumber.java.AfterStep;
import io.cucumber.java.Scenario;
import io.cucumber.java.Status;
import java.nio.file.Files;
import java.nio.file.Paths;

@AfterStep
public void attachDownloadedFileOnSuccess(Scenario scenario) {
    if (scenario.getStatus() == Status.PASSED) {
        // 判断当前步骤是否是文件下载验证步骤(可选,避免所有成功步骤都附加)
        if (CucumberListener.stepName.equals("Is file downloaded successfully")) {
            try {
                byte[] fileBytes = Files.readAllBytes(Paths.get("/path/to/downloaded/file.xlsx"));
                scenario.attach(
                    fileBytes, 
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
                    "Downloaded File"
                );
            } catch (Exception e) {
                // 记录异常日志
            }
        }
    }
}

3. 借助第三方Reporter扩展

使用Cucumber的第三方报告扩展(如Extent Reporter、Allure Reporter),这类工具提供更便捷的文件附加API,无需手动处理Scenario:
以Extent Reporter为例:

import com.aventstack.extentreports.ExtentTest;
import java.io.File;

@Given("Is file downloaded successfully")
public void userIsOnLoginScreen() {
    SoftAssertions.assertSoftly(softAssertions -> 
        softAssertions.assertThat(testContext.pageObjectManager().getDemo().isFileDownloaded())
            .as("File Not Downloaded")
            .isTrue()
    );
    
    ExtentTest currentTest = ExtentManager.getCurrentTest();
    // 附加Excel文件
    currentTest.addAttachment(
        "Downloaded File", 
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
        new File("/path/to/downloaded/file.xlsx")
    );
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 17:46:27