Forum Discussion

Tambu's avatar
New Contributor
3 years ago

Running test step multiple times to add records


I'm new to Readyapi. I want run a test step multiple times to create multiple records.
I have used following groovy script but test step has not been ran multiple times. 

if( context.loopIndex == null )
context.loopIndex = 0
if( ++context.loopIndex < 10 )
testRunner.gotoStepByName( "Name of TestStep to run n times" )


Can someone please suggest if the script is wrong or I need to add anything more

Many Thanks in advance

3 Replies

  • ChrisAdams's avatar
    Champion Level 3



    It looks like your code says.... If the count is less than 10, then go to some other step in the script.


    The key bit is "testRunner.gotoStepByName".  This will move the test runner to that step, leapfrogging any steps between that and the groovy step and won't come back.


    I've knocked up a simple loop that will call a step 10 times.  In order for this to work, you need to call the record creation step at least once before checking loop.  Think while loop over for loop.


    Here's what my test looks like.  You need to create a test case property called 'count'.



    Here are the contents of Initialise...

    testRunner.testCase.setPropertyValue("count", "0");


    The Rest request step just pings the BBC website and should be replaced with your step to create a record.


    Lastly, the loop step.  In here we test the count property and if less than 10, increment the count and go back to the rest step.

    // Get a test case property
    def count = testRunner.testCase.getPropertyValue("count").toInteger();"Count before - " + count);
    if (count < 10){
    	count = count +1;"Count after - " + count);
    	testRunner.testCase.setPropertyValue("count", count.toString());
    	testRunner.gotoStepByName( "Call BBC Website - REST Request");


    Note the calls to toInteger() and toString().  The Test Case Property is a string value pair.  You need to convert to int before use and write back as a string.




  • nmrao's avatar
    Champion Level 3


    If ReadyAPI is used, then data source step is the preferred option.

    • sonya_m's avatar
      SmartBear Alumni (Retired)

      Thanks everyone for helping!


      Tambu did the replies help? Please mark the best one as a Solution.