Forum Discussion

PramodYadav's avatar
PramodYadav
Contributor
6 years ago
Solved

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 index (say 20). However I have a data driven groovy script (say step 5), from where all other scripts are called. When all steps are called from within this step 5, the index that comes out for all steps is 5 and not 1,2,3... 20 etc. I understand why it may do so, but can anyone suggest a workaround to get the index numbers for each script keepin the same framework model?

 

P.S: I could use a counter to set the property at testSuite level and read this in making log files but was hoping to find a cleaner approach than that. 

  • 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"

     

     

2 Replies

  • 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"