【问题标题】:How to call a function after failure of selenium,junit,cucumber and maven projectselenium,junit,cucumber和maven项目失败后如何调用函数
【发布时间】:2018-04-20 21:17:45
【问题描述】:

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

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

第一次尝试: 在这里,我尝试在 ExtendedCucumberRunner 类中覆盖以下函数:-

import org.junit.runner.notification.Failure;

public void testFailure(Failure failure){
}

但如果我试图覆盖错误,则会显示为:

ExtendedCucumberRunner 类型的方法 testFailure(Failure) 必须 覆盖或实现超类型方法。

第二次尝试:

我也尝试过扩展 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 方法,因为即使任何测试用例成功,它也会每次调用。

我只想在测试用例失败时截屏

谁能告诉我任何方法让我能达到同样的效果?

【问题讨论】:

  • 您可以在 After 挂钩中通过将 Scenario 对象作为方法参数传递来执行此操作。当前执行的场景将由黄瓜注入。如果测试失败,Scenario 的方法 isFailed() 将返回 true。检查此条件后包含屏幕截图。
  • 你能分享一个例子吗
  • 酷它工作.. 我需要在每个 stepdefination 类中添加该功能吗?
  • 只有一次... After 和 Before 钩子由黄瓜自动调用,只要它们包含在提供给跑步者的黄瓜选项中的“胶水”中提到的包中。

标签: java maven junit cucumber cucumber-java


【解决方案1】:

正如@Grasshopper 正确指出的那样。在您的任何步骤定义中使用以下函数一次。 Cucumber 将识别钩子并在每次测试用例运行后运行下面的函数。万一失败,场景将运行 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-08
    相关资源
    最近更新 更多