0

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

user3624378
  • 417
  • 4
  • 22

1 Answers1

2

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");
    }
}
Chris Adams
  • 1,376
  • 1
  • 8
  • 15
  • Thanks for your reply. I tried your script. I can't get any values from Groovy scripts waitTime + maxRetries. When I tried log.info waitTime + maxRetries they are empty so when the script reaches: if(retryCount < Integer.parseInt(maxRetries)) it ofcourse fails beacause maxRetries is empty. – user3624378 Mar 04 '22 at 08:06
  • waitTime and maxRetries will be empty if you run the script in isolation without running the whole test. You need to run the test case as a whole (see first screenshot). Then each script will be executed and have a value when you reach the retry script. – Chris Adams Mar 04 '22 at 10:24
  • Hi Chris, I did run the whole testcase. But still I got the error. – user3624378 Mar 04 '22 at 19:46
  • Hi, this ran fine for me, so there must be something else. For the fourth step, did you call a call to a webservice? It doesn't matter what webservice, but one need to be there. It also has to have the exact same name as that in the Groovy script E.g. ["REST Request"] – Chris Adams Mar 05 '22 at 08:39