【问题标题】:How to implement custom listeners in cucumber?如何在黄瓜中实现自定义监听器?
【发布时间】:2021-12-22 14:55:47
【问题描述】:

如何在cucumber中实现客户监听?

哪个可以记录到控制台/报告失败方法的发生?

使用黄瓜 4.0

注意:钩子在方法级别没有帮助

【问题讨论】:

  • 什么是“失败方法的发生”,为什么需要记录这个?给我们一个示例场景以及您需要记录的内容。

标签: java selenium selenium-webdriver cucumber cucumber-java


【解决方案1】:

黄瓜中没有像 TestNG 这样的自定义侦听器选项。我们应该只使用 Hooks。

【讨论】:

  • 那如何实现呢?因为使用 TestNG 侦听器没有给出预期的方法名称
【解决方案2】:

您可以实现 ConcurrentEventListener 以获取 events [example] 的某些挂钩。

活动可以在here找到

【讨论】:

    【解决方案3】:

    你可以像下面这样在黄瓜中实现监听器

        import io.cucumber.plugin.EventListener;
        import io.cucumber.plugin.event.*;
        
        import java.net.URI;
        import java.util.Map;
        import java.util.TreeMap;
        import java.util.UUID;
        
        public class    ReportPlugin implements EventListener {
        
         private final Map<String, UUID> startedSteps = new TreeMap<String, UUID>();
         private final Map<String, Status> finishedCases = new TreeMap<String, Status>();
        
           @Override
            public void setEventPublisher(EventPublisher publisher) {
                
                publisher.registerHandlerFor(TestStepStarted.class, this::handleTestStepStarted);
            publisher.registerHandlerFor(TestCaseFinished.class, this::handleTestCaseFinished);
    
            }
        
        private void handleTestStepStarted(TestStepStarted event) {
                startedSteps.put(event.getTestStep().toString(), event.getTestStep().getId());
                for (Map.Entry<String, UUID> entry : startedSteps.entrySet()) {
                        String location = entry.getKey();
                        UUID uuid = entry.getValue();
                        System.out.println(location + " ###fromTestStepStarted### " + uuid);
                
                 //above prints
                //io.cucumber.core.runner.PickleStepTestStep@5a5c128 ###fromTestStepStarted### 7f964f1c-9442-43fc-97e9-9ec6717eb47f
               // io.cucumber.core.runner.PickleStepTestStep@77b919a3 ###fromTestStepStarted### a5d57753-aecb-40a0-a0cf-76bef7526dd8
    
                    }
            }
    }
    

    要运行上述类 - 将类与您的步骤定义或支持类放在一起,然后在 junit-platform.properties(对于 Junit5)中提及这样的插件

    cucumber.plugin = com.test.support.ReportPlugin

    对于 Junit4,您可能需要将插件添加到您的运行器类

    当您运行测试时,您应该会在控制台上看到打印的所有内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2011-02-02
      • 2013-03-02
      • 2013-07-08
      相关资源
      最近更新 更多