In Cucumber, is it possible to programmatically get the current step being executed?

后端 未结 11 2641
清歌不尽
清歌不尽 2020-12-04 00:22
Scenario: As a user, I want to login to the system
Given I am on my website
When I enter valid credentials
Then I am taken to the home page

The sce

11条回答
  •  一向
    一向 (楼主)
    2020-12-04 00:58

    Here's an update to handle the framework changes. The "testCase" field is hidden under the "delegate". I got this working with io.cucumber.java version 5.7.0

    public String getStepText(io.cucumber.java.Scenario scenario){      
        String  currentStepDescr = null;
    
        //value currentStepDefIndex is tracked in the another class
        int currentStepDefIndex = OtherClass.getStepIndex();
    
        Field f = scenario.getClass().getDeclaredField("delegate");
        f.setAccessible(true);
        TestCaseState tcs = (TestCaseState) f.get(scenario);
    
        Field f2 = tcs.getClass().getDeclaredField("testCase");
        f2.setAccessible(true);
        TestCase r = (TestCase) f2.get(tcs);
    
            List stepDefs = r.getTestSteps()
                    .stream()
                    .filter(x -> x instanceof PickleStepTestStep)
                    .map(x -> (PickleStepTestStep) x)
                    .collect(Collectors.toList());
    
    
            PickleStepTestStep currentStepDef = stepDefs
                    .get(currentStepDefIndex);
            currentStepDescr = currentStepDef.getStep().getText();
            currentStepDefIndex += 1;
            OtherClass.setStepIndex(currentStepDefIndex);
             return currentStepDescr ;
           }
    

    Below are the dependencies in my pom.xml

    
            
                io.cucumber
                cucumber-core
                5.7.0
            
    
    
            
            
                io.cucumber
                cucumber-testng
                5.7.0
            
    
    
            
            
                io.cucumber
                cucumber-java
                5.7.0
            
    
    
            
            
                io.cucumber
                cucumber-jvm-deps
                1.0.6
                provided
            
    

提交回复
热议问题