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

如何在QAF中获取当前BDD步骤?自定义报告场景实现问询

在QAF中获取带参数的当前BDD步骤(用于自定义报告)

一、StepDefinition中的实现

你之前的问题出在使用getBundle()获取属性,getBundle()是用于读取资源配置文件的工具,并非测试执行上下文,所以会直接返回属性key而非实际步骤内容。以下是两种正确的实现方式:

方式1:通过TestBase上下文获取

import com.qmetry.qaf.automation.core.TestBaseProvider;
import com.qmetry.qaf.automation.step.QAFTestStep;

public class MyStepDefinitions {
    @QAFTestStep(description = "Test step with {param1} and {param2}")
    public void testStep(String param1, String param2) {
        // 从测试上下文获取解析后的当前BDD步骤
        String currentStep = (String) TestBaseProvider.instance().get().getContext().getProperty("current.teststep");
        System.out.println("===CURRENT Test Step ===> " + currentStep);
        // 此处可将currentStep传入自定义报告逻辑
    }
}

方式2:继承QAFTestStepBase获取StepContext

如果步骤类继承自QAFTestStepBase,可以直接获取步骤上下文,拿到解析后的步骤描述:

import com.qmetry.qaf.automation.step.QAFTestStep;
import com.qmetry.qaf.automation.step.QAFTestStepBase;
import com.qmetry.qaf.automation.step.QAFTestStepContext;

public class MySteps extends QAFTestStepBase {
    @QAFTestStep(description = "Verify {element} displays {text}")
    public void verifyElementText(String element, String text) {
        QAFTestStepContext stepContext = getStepContext();
        // 获取已解析参数的完整步骤描述
        String resolvedStep = stepContext.getResolvedDescription();
        System.out.println("===CURRENT Test Step ===> " + resolvedStep);
        // 传入自定义报告
    }
}

二、监听器中的实现

通过实现QAF的TestStepListener接口,可在步骤执行前后统一捕获步骤信息,适合全局报告逻辑:

1. 实现自定义监听器

import com.qmetry.qaf.automation.core.TestBaseProvider;
import com.qmetry.qaf.automation.step.TestStepListener;
import com.qmetry.qaf.automation.step.TestStepResult;
import java.lang.reflect.Method;

public class CustomReportListener implements TestStepListener {
    @Override
    public void beforeStep(Method method, Object[] args) {
        // 获取当前解析后的BDD步骤
        String currentStep = (String) TestBaseProvider.instance().get().getContext().getProperty("current.teststep");
        // 将步骤信息传入自定义报告(例如写入报告文件、存储到变量等)
        System.out.println("Listener - 即将执行步骤: " + currentStep);
    }

    @Override
    public void afterStep(Method method, Object[] args, TestStepResult result) {
        String currentStep = (String) TestBaseProvider.instance().get().getContext().getProperty("current.teststep");
        // 可结合步骤结果(成功/失败)完善报告内容
        String stepStatus = result.isPassed() ? "成功" : "失败";
        System.out.println("Listener - 步骤执行完成: " + currentStep + ",状态: " + stepStatus);
    }

    @Override
    public void onStepFailure(Method method, Object[] args, Throwable t) {
        // 步骤失败时的额外处理(例如记录异常信息到报告)
        String currentStep = (String) TestBaseProvider.instance().get().getContext().getProperty("current.teststep");
        System.out.println("Listener - 步骤执行失败: " + currentStep + ",异常: " + t.getMessage());
    }
}

2. 注册监听器

在QAF的配置文件(如application.properties)中添加监听器配置:

qaf.listeners=com.yourpackage.CustomReportListener

内容的提问来源于stack exchange,提问作者Renish K.R

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 12:36:01