Extracting Cucumber Step name at runtime

后端 未结 6 2117
一生所求
一生所求 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条回答
  •  情书的邮戳
    2021-01-18 21:27

    You can implement a ConcurrentEventListener, setting it in the plugin section:

    @RunWith(Cucumber.class)
    @CucumberOptions(
    ...
    plugin = {
       ...,
       "com.mycompany.myproduct.AcceptanceStepNameLogger"
    }...
    

    AcceptanceStepNameLogger example (in this case I only needed to log PickleStepTestStep steps, not hooks):

    import cucumber.api.*;
    import cucumber.api.event.*;
    
    public class AcceptanceStepNameLogger implements ConcurrentEventListener {
    
        @Override
        public void setEventPublisher(EventPublisher publisher) {
    
            publisher.registerHandlerFor(TestStepStarted.class, new EventHandler() {
                @Override
                public void receive(TestStepStarted event) {
                    if (event.testStep instanceof PickleStepTestStep) {
                        final PickleStepTestStep ev = (PickleStepTestStep) event.testStep;
                        final String args = StringUtils.join(ev.getDefinitionArgument().stream().map(Argument::getValue).toArray(), ",");
                        String testDescription = ev.getStepText() + " : " + ev.getStepLocation();
                        if (StringUtils.isNotBlank(args)) {
                            testDescription += (" : args = (" + args + ")");
                        }
                        System.out.println("STARTING STEP: " + testDescription);
                    }
                }
            });
    
            publisher.registerHandlerFor(TestStepFinished.class, new EventHandler() {
                @Override
                public void receive(TestStepFinished event) {
                    if (event.testStep instanceof PickleStepTestStep) {
                        PickleStepTestStep ev = (PickleStepTestStep) event.testStep;
                        final String testDescription = ev.getStepText() + " : " + ev.getStepLocation();
                        System.out.println("FINISHED STEP: " + testDescription);
                    }
                }
            });
    
        }
    
    }
    

提交回复
热议问题