Extracting Cucumber Step name at runtime

后端 未结 6 2102
一生所求
一生所求 2021-01-18 21:03

I am trying to find out if there is an option to figure out the cucumber step currently getting executed, I am trying to perform certain action depending on the step name.

6条回答
  •  萌比男神i
    2021-01-18 21:45

    If you are using cucumber-jvm version that is 5.6.0 or latest please follow below solution. since Reporter class is deprecated you have to implement ConcurrentEventListener and add this class to your TestRunner plugin section.

    public class StepDetails implements ConcurrentEventListener {
        public static String stepName;
    
        public EventHandler stepHandler = new EventHandler() {
            @Override
            public void receive(TestStepStarted event) {
                handleTestStepStarted(event);
            }
    
        };
    
        @Override
        public void setEventPublisher(EventPublisher publisher) {
            publisher.registerHandlerFor(TestStepStarted.class, stepHandler);
        }
    
        private void handleTestStepStarted(TestStepStarted event) {
            if (event.getTestStep() instanceof PickleStepTestStep) {
                PickleStepTestStep testStep = (PickleStepTestStep)event.getTestStep();
                stepName = testStep.getStep().getText();
            }
    
    
        }
    }
    

    later add this class to your plugin section of cucumberOptions of your runner file

    @CucumberOptions(dryRun=false,plugin = {".StepDetails"})
    

    you can get the stepname wherever you want just by calling the stepName variable

    System.out.println(StepDetails.stepName);
    

提交回复
热议问题