groovysoapuiready-api

Rerun teststep if statuscode equals 401


How can I rerun a teststep in ReadyAPI x number of times if the teststep fails? My test is requesting a server that sometimes can take up to 30s to answer if it's not "pre-heated". Sometimes it goes much faster. So I've implemented a "Delay-step" to 20s. But I don't want to make the delay much larger than 20s.

Example:

Testcase
 --Teststep: GetMyData

Teststep GetMyData fails, I re-run it manually a couple of seconds after and now has the integration server got a response and the teststep works just fine.

I've tried this solution: Rerun teststep

But it gives me java.lang.NullPointerException


Solution

  • Here is an example...

    Test Layout

    The Get Number of Retries Groovy contains this...

    Number of Retries script

    This defines how many times to retry.

    The Get Wait Period groovy contains...

    Get Wait Period

    This is the number of millis between retries. So, 10 retries with a wait of 2secs is a max of 20 seconds.

    The Reset Retries groovy script contains...

    Reset Retries

    This sets our custom property to zero.

    The Rest Request step is whatever call you want to make and retry.

    The Check Request is where we do our checking and retrying. I've pasted the code instead of a screenshot.

    // Initialise some variables.
    def status = testRunner.testCase.testSteps["REST Request"].testRequest.response.responseHeaders["#status#"];
    def waitTime = context.expand( '${Get Wait Period - Groovy Script#result}' )
    def maxRetries = context.expand( '${Get Number of Retries - Groovy Script#result}' )
    def retryCountString = context.expand( '${#TestCase#RetryCount}' )
    
    // Convert the retry count from String to Int
    def retryCount = Integer.parseInt(retryCountString);
    
    // Let's check the request status.  If it worked its 200.
    if(status.toString().contains("200")){
        // We're good.  Do nothing more.
    } else {
        // Server not ready.  Let's retry...
        if(retryCount <  Integer.parseInt(maxRetries)) {
    
            log.info("Retry ${retryCount} of ${maxRetries}.");
    
            // A small sleep to give a gap inbetween retries.
            sleep( Integer.parseInt(waitTime));
    
            // Update the retry count and save it.
            retryCount += 1;
            testRunner.testCase.setPropertyValue( "RetryCount", retryCount.toString() );
    
            // Let's re-run the step.
            testRunner.gotoStepByName("REST Request");
        
        } else {
            log.info("Ran out of retries");
        }
    }