Ask a Question

Running test step multiple times to add records

Tambu
Visitor

Running test step multiple times to add records

Hi,

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 3
ChrisAdams
Community Leader

Re: Running test step multiple times to add records

Hi,

 

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'.

 

ChrisAdams_1-1638262675343.png

 

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();

log.info("Count before - " + count);

if (count < 10){

	log.info(count);
	count = count +1;
	log.info("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
Community Hero

Re: Running test step multiple times to add records

@Tambu 

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



Regards,
Rao.
sonya_m
Community Manager

Re: Running test step multiple times to add records

Thanks everyone for helping!

 

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


Sonya Mihaljova
Community and Education Specialist

cancel
Showing results for 
Search instead for 
Did you mean: