Forum Discussion

feroz_k's avatar
feroz_k
Occasional Contributor
8 years ago
Solved

What is the best way to implement a loop with counter in readyAPI?

Trying to figure out a real simple way of implementing this:

1. Read a counter from some property 

2. Repeat a set of test steps for that counter or until an exit condition

 

Thanks

  • So if your reading a counter form custom property, first thing you will need to do is convert it to an integer
    def counter = testRunner.testCase.getPropertyValule("some property name")

    counter = counter.toInteger()

     

    for (counter = 0;counter <= 10;counter++){

       do somthing

    }

     

     

    let me know if this helps

  • You should be able to implement a loop over a set of test steps with something like the following:

     

    def final int MAX_RETRY_COUNT = 20
    def success = false
    def retryCount // Get the property value with getPropertyValue(String name) - exact syntax depends where it's stored
    retryCount = retryCount.toInteger()
    
    // Insert check for exit condition here, set boolean var "success" appropriately
    
    if(!success){
    	if(retryCount <= MAX_RETRY_COUNT){
    		log.info(logPrefix + 'Exit condition not met after ' + retryCount.toString() + ' attempts, try again...')
    		testRunner.gotoStepByName("Your Test Step Name")
    	}	
    }
    
    retryCount++
    // Store new value of retryCount with setPropertyValue(String name, String value)  - exact syntax depends where it's stored

     

    As you didn't mention where the property was stored, I can't give you the exact syntax, but as all of the possible objects that can store properties should implement the TestPropertyHolder interface, the getPropertyValue and setPropertyValue methods should be the same.

     

    I'm assuming that you are storing the incremented counter value in the property in question, the other interpretation of your is that just the max count value is stored in the property, if that is the case you should be able to adjust the above script, and store the running count in the context.

2 Replies

  • So if your reading a counter form custom property, first thing you will need to do is convert it to an integer
    def counter = testRunner.testCase.getPropertyValule("some property name")

    counter = counter.toInteger()

     

    for (counter = 0;counter <= 10;counter++){

       do somthing

    }

     

     

    let me know if this helps

  • Radford's avatar
    Radford
    Super Contributor

    You should be able to implement a loop over a set of test steps with something like the following:

     

    def final int MAX_RETRY_COUNT = 20
    def success = false
    def retryCount // Get the property value with getPropertyValue(String name) - exact syntax depends where it's stored
    retryCount = retryCount.toInteger()
    
    // Insert check for exit condition here, set boolean var "success" appropriately
    
    if(!success){
    	if(retryCount <= MAX_RETRY_COUNT){
    		log.info(logPrefix + 'Exit condition not met after ' + retryCount.toString() + ' attempts, try again...')
    		testRunner.gotoStepByName("Your Test Step Name")
    	}	
    }
    
    retryCount++
    // Store new value of retryCount with setPropertyValue(String name, String value)  - exact syntax depends where it's stored

     

    As you didn't mention where the property was stored, I can't give you the exact syntax, but as all of the possible objects that can store properties should implement the TestPropertyHolder interface, the getPropertyValue and setPropertyValue methods should be the same.

     

    I'm assuming that you are storing the incremented counter value in the property in question, the other interpretation of your is that just the max count value is stored in the property, if that is the case you should be able to adjust the above script, and store the running count in the context.