gpt4 book ai didi

java - selenium、junit、cucumber、maven项目失败后如何调用函数

转载 作者:行者123 更新时间:2023-11-30 06:26:08 24 4
gpt4 key购买 nike

我创建了一个java Cucumber Maven项目。现在我想添加一个监听器或任何方式,以便在任何 cucumber 步骤/测试失败时调用我的屏幕截图捕获函数。

我没有在 Junit 的 TestRunner 类中使用 @RunWith(Cucumber.class) 。我已经集成了 @RunWith(ExtendedCucumberRunner.class)

第一次尝试:在这里,我尝试重写 ExtendedCucumberRunner 类中的以下函数:-

import org.junit.runner.notification.Failure;

public void testFailure(Failure failure){
}

但是如果我尝试覆盖,错误将显示为:

The method testFailure(Failure) of type ExtendedCucumberRunner must override or implement a supertype method.

第二次尝试:

我还尝试扩展 RunListener 类,但它仍然不起作用。

我尝试添加Lister的代码如下:

import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import automationframework.AutomationLog;
import automationframework.ScreenshotAndTestNgReporterListener;

public class JUnitExecutionListener extends RunListener{
/**
* Called when an atomic test fails.
* https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/
* */
@Override
public void testFailure(Failure failure) throws java.lang.Exception{
super.testFailure(failure);
if (!failure.getDescription().isSuite()) {
ScreenshotAndTestNgReporterListener.customScreenshot();
AutomationLog.error("In Custom Failer Class of Junit");
System.out.println("FAILED!!!!!"); //Here pass your screenshot capture event
}
ScreenshotAndTestNgReporterListener.customScreenshot();
AutomationLog.error("In Custom Failer Class of Junit");
System.out.println("FAILED!!!!!"); //Here pass your screenshot capture event
}
}

第三次尝试:

我还尝试在 TestRunner 类中添加以下规则

@Rule
public TestWatcher watchman= new TestWatcher() {
private String watchedLog;
@Override
protected void failed(Throwable e, Description description) {
ScreenshotAndTestNgReporterListener.customScreenshot();
AutomationLog.error(e.getMessage());
watchedLog+= description + "\n";
}

@Override
protected void succeeded(Description description) {
watchedLog+= description + " " + "success!\n";
}
};

我不想添加 @After 方法,因为即使任何测试用例成功,它也会每次调用。

我只想在测试用例失败时捕获屏幕截图

任何人都可以告诉我任何方法,以便我可以实现同样的目标吗?

最佳答案

正如@Grasshopper 正确指出的那样。在任何步骤定义中使用以下函数一次。 Cucumber 将识别钩子(Hook),并在每个测试用例运行后运行下面的函数。如果失败,场景将运行 if 条件,您可以在其后编写您想要运行的任何代码。

引用:

Hook up in cucumber check for scenario failure in java

代码:

@After
public void afterMethod(Scenario scenario) {
if(scenario.isFailed()) {

}
}

导入:

import cucumber.api.java.After;

关于java - selenium、junit、cucumber、maven项目失败后如何调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com