获取 Gherkin Scenario 故障行和消息

问题描述 投票:0回答:1

我正在使用 Cucumber 来编写我的场景。我的目标是获取错误结果,如果可能的话,获取破坏场景的行号,并将其作为变量传递,稍后我将使用该变量。

我进行了广泛的搜索,发现了以下代码行并在单独的文件中实现了事件监听器。

public class TestListener implements ConcurrentEventListener {
    @Override
    public void setEventPublisher(EventPublisher publisher) {
        publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);
    }

private void handleTestCaseFinished(TestCaseFinished event) {
    TestCase testCase = event.getTestCase();
    Result result = event.getResult();
    Status status = result.getStatus();
    Throwable error = result.getError();
    String scenarioName = testCase.getName();
}

}

我的目标是在场景失败时获取 result.getError() 并将其存储在变量中,但目前我不知道如何做到这一点。

@After
public void nameCatcher(Scenario scenario) throws Exception {
    if (seleniumCore.driver != null) {
        seleniumCore.driver.quit();
    }
    if(scenario.isFailed()) {
        testRailAPItBean.setExecutionResultStatus(TestRailAPIConst.TEST_FAILED);
    } else if (scenario.getStatus().name().equals("PASSED")) {
         testRailAPItBean.setExecutionResultStatus(TestRailAPIConst.TEST_PASSED);
    }
    TestrailRestApi.updateTestCaseResulForTheLastActiveRun();
}

我尝试在类中使用监听器,我将使用它,但是当我引用它时,它需要一个事件变量,当我按如下方式设置时,它会给我一个错误“将'null'参数传递给注释为@NotNull的参数”

EventPublisher eventPublisher = null;
    setEventPublisher(eventPublisher);

scenario.getError() 还给我“无法解析‘场景’中的方法‘getError’”

下面是我的 Gradle 配置,以防需要

    plugins {
    id("java")
}

group = "org.example"
version = "master-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testImplementation ("io.cucumber:cucumber-java:7.13.0")
    testImplementation ("io.cucumber:cucumber-junit:7.13.0")
    implementation ("io.cucumber:cucumber-java:7.13.0")
}

tasks.test {
    useJUnitPlatform()
}
selenium-webdriver gherkin cucumber-java
1个回答
0
投票

经过一番搜索,我找到了以下解决方案 我们从场景中获取委托类。然后我们将其设置为可访问,因为默认情况下可访问性设置为 false。

然后我们继续列出所有失败的步骤。 然后我们消除所有不需要的行并创建一个 stringbuilder 值以根据需要使用

    public static void testCompletionHandler(Scenario scenario) throws Exception {
    
    
    Result failResult = null;
    
        //Get the delegate field from Scenario
        Field delegate = scenario.getClass().getDeclaredField("delegate");
    
        //make delegate accessible since normally it is not
        delegate.setAccessible(true);
        TestCaseState testCaseState = (TestCaseState) delegate.get(scenario);
        
        Field testCaseField = testCaseState.getClass().getDeclaredField("testCase");
        Field stepResults = testCaseState.getClass().getDeclaredField("stepResults");
    
        testCaseField.setAccessible(true);
        stepResults.setAccessible(true);

        List<Result> results = (List<Result>) stepResults.get(testCaseState);
        //Get the failed step line
        for (Result result : results) {
            if (result.getStatus().name().equalsIgnoreCase("FAILED")) {
                failResult = result;
            }
        }

        assert failResult != null;
//Create a stringbuilder to convert the stacktrace to viewable and printable values
        StringBuilder sb = new StringBuilder();
        StackTraceElement[] stackTrace = failResult.getError().getStackTrace();
        for (StackTraceElement element : stackTrace) {
               sb.append(element.toString());
               sb.append("\n");
        }
        String[] train = sb.toString().split("\n");
        sb.setLength(0);
//Get the final 3 stackTraceElements that usually contain the exact location of the error
        int i = train.length - 4;
        for (; i < train.length; i++) {
               sb.append(train[i]);
               sb.append("\n");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.