Forum Discussion

PramodYadav's avatar
PramodYadav
Contributor
6 years ago

How to get a steps index number using groovy?

While saving my log results for each script, I wanted to start them with the steps index number. When I add context.currentStepIndex in a groovy step and run it from that step, I get the correct step...
  • Alex99's avatar
    6 years ago

    If you are still looking into it, here is one possible solution.

    The little script will collect all test step names, according to their place//occurence inside a test case, then you can get their index by calling the .findIndexOf method. See example below.

     

    This should work for you, since you are calling all steps from one script step (Step 5). Let that step collect all names beforehand and everytime it runs one of the steps, check their index in the collection.

     

    Also, since no duplicate names are allowed within one test case, it should work perfectly. See also both screenshots.

     

     

     

    log.info context.currentStepIndex    //will give you 3
    
    def l = []
    
    context.testCase.getTestStepList().each {
    	l.add(it.getName())
    }
    
    log.info l.findIndexOf {it == 'script 2'}    //will return 3
    
    log.info "done"