Extracting Cucumber Step name at runtime

后端 未结 6 2050
一生所求
一生所求 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<TestStepStarted>() {
                @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<TestStepFinished>() {
                @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);
                    }
                }
            });
    
        }
    
    }
    
    0 讨论(0)
  • 2021-01-18 21:31

    Just wait for Cucumber 3.0.0 release, you can access the step names using @AfterStep and @BeforeStep Annotations.

    https://github.com/cucumber/cucumber-jvm/blob/master/CHANGELOG.md https://github.com/cucumber/cucumber-jvm/pull/1323

    Thanks to Aniket (Coding-Yogi) https://github.com/coding-yogi

    0 讨论(0)
  • 2021-01-18 21:32

    One way to get hold of the runtime variables is to use the plugin option. Though it seems like an abuse of the Reporter interface to access the StepDefinitionMatch variable for Cucumber version 1.2.5.

    For this create a custom class which implements the Reporter interface.

    public class CustomFormatter implements Reporter{
    
        public  CustomFormatter() { }
    
        @Override
        public void before(Match match, Result result) {}
    
        @Override
        public void result(Result result) {}
    
        @Override
        public void after(Match match, Result result) {}
    
        @Override
        public void match(Match match) {
            ThreadLocalStepDefinitionMatch.set((StepDefinitionMatch)match);
        }
    
        @Override
        public void embedding(String mimeType, byte[] data) {}
    
        @Override
        public void write(String text) {}
    }
    

    The ThreadLocal class to store the StepDefinitionMatch variable.

    public class ThreadLocalStepDefinitionMatch {
    
        private static final ThreadLocal<StepDefinitionMatch> threadStepDefMatch = new InheritableThreadLocal<StepDefinitionMatch>();
    
        private ThreadLocalStepDefinitionMatch() {
        }
    
        public static StepDefinitionMatch get() {
            return threadStepDefMatch.get();
        }
    
        public static void set(StepDefinitionMatch match) {
            threadStepDefMatch.set(match);
        }
    
        public static void remove() {
            threadStepDefMatch.remove();
        }
    }
    

    The declaration for custom plugin in the runner class

    @CucumberOptions(plugin = { "pretty", "html:report", "json:reports.json",
            "rerun:target/rerun.txt", "cusform.CustomFormatter" }
    

    Finally accessing the StepDefinitionMatch variable in the step definition class

    @When("^user gets count from \"([^\"]*)\"$")
        public void userGetsCountFromAndStores(String arg) {
            StepDefinitionMatch match = ThreadLocalStepDefinitionMatch.get();
            System.out.println(match.getStepName());
            System.out.println(match.getPattern());
            System.out.println(match.getArguments());
        }
    

    The console output gives the following

    user gets count from "Car1"
    ^user gets count from "([^"]*)"$
    [Car1]
    

    You are using a pretty old Cucumber version, if you upgrade to Cucumber 2 there will be some changes. The StepDefinitionMatch replaced by PickleTestStep. The declaration in the runner remains the same. The ThreadLocal class change the stored class to PickleTestStep.

    The custom formatter becomes

    public class CustomFormatter implements Formatter {
    
        public CustomFormatter() {}
    
        private EventHandler<TestStepStarted> stepStartedHandler = new EventHandler<TestStepStarted>() {
            @Override
            public void receive(TestStepStarted event) {
                handleTestStepStarted(event);
            }
        };
    
        @Override
        public void setEventPublisher(EventPublisher publisher) {
            publisher.registerHandlerFor(TestStepStarted.class, stepStartedHandler);
        }
    
        private void handleTestStepStarted(TestStepStarted event) { 
            if(event.testStep instanceof PickleTestStep) {
                ThreadLocalPickleStep.set((PickleTestStep)event.testStep);
            }
    
        }
    }
    

    Accessing the StepDefinitionMatch variable in the step definition class

    @When("^user gets count from \"([^\"]*)\"$")
        public void userGetsCountFromAndStores(String arg) {
    
            System.out.println(ThreadLocalPickleStep.get().getStepText());
            System.out.println(ThreadLocalPickleStep.get().getPattern());
            System.out.println(ThreadLocalPickleStep.get().getDefinitionArgument());
        }
    

    The output is the same as before.

    0 讨论(0)
  • 2021-01-18 21:33

    Below code can be used to get current step method name

    new Throwable().getStackTrace()[0].getMethodName()
    

    Example: Given user is on Login page

    @Given("^user is on Login page$") public void user_is_on_Login_page() throws Throwable {

    }
    

    new Throwable().getStackTrace()[0].getMethodName() => it will return current step method name as "user_is_on_Login_page"

    0 讨论(0)
  • 2021-01-18 21:39

    Here is what I did in cucumber 4,

    Create a event listenter class and add it as a plugin at cucumber options:

    public class TestStepWatch implements ConcurrentEventListener {
        private EventHandler<TestStepStarted> stepHandler = new EventHandler<TestStepStarted>()
        {
            @Override
            Public Void receive(TestStepStarted event)
            {
                handleTestStep(event)
            }
    
            private synchronized void handleTestStep(TestStepStarted event)
            {
                System.out.println("Running step:"+ event.testStep.getStepText());
            }
        }
    }
    
    0 讨论(0)
  • 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<TestStepStarted> stepHandler = new EventHandler<TestStepStarted>() {
            @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 = {"<yourPackage>.StepDetails"})
    

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

    System.out.println(StepDetails.stepName);
    
    0 讨论(0)
提交回复
热议问题