Ask a Question

Iterating through Data source values and verifying the matched value

SOLVED
mu123
Occasional Contributor

Iterating through Data source values and verifying the matched value

This is the scenario that I am trying to test: 

1. I have a REST API step that returns number of ids and names for locations. (Sometimes zero records are returned, sometimes one and sometimes multiple records are returned)

2. I have to write a test to iterate through the REST Response and verify the location name and get the corresponding id.

------------------------------------

I tried to test this by

1. Creating a Data source from the Json response of the REST API step. 11 rows of id and name were returned.

2. I am trying to write a groovy script to iterate through all the rows of Data source but couldn't do it.

  I can only get the last row in the groovy script. Please let me know how to test this scenario.

10 REPLIES 10
fgimenes
Occasional Contributor

Hi @mu123 , would you mind sharing your final script? I am trying a similar implementation and I haven't figured out how to advance the data source to the next line. I am not able to make the data source to go to the next line. It always prints the same value. I am not if it's because my Data Source is in another Test Suite??. This is my code:

 

def tStep = testRunner.testCase.testSuite.project.testSuites["Innitialization"].testCases["Test Data Setup"].getTestStepByName("Data Source for Registration")
def data = tStep.getDataSource()

//run the datasource test step
data.prepare(testRunner,context,data.getPreparedProperties() )
tStep.run(testRunner,context)

//loop through the datasource to find number of rows
def cnt = 1
while(data.isExhausted() == false) {
	data.next(testRunner,context,data.getPreparedProperties() )
	def MIP = context.expand( '${#[Innitialization#Test Data Setup#Data Source for Registration]#MIP}' )
	def IS_CMA_ACCOUNT_CREATED = context.expand( '${#[Innitialization#Test Data Setup#Data Source for Registration]#IS_CMA_ACCOUNT_CREATED}' )
	def mobileNumber = context.expand( '${#[Innitialization#Test Data Setup#Data Source for Registration]#mobileNumber}' )
	
	log.info("MIP: " + MIP)
	log.info("IS_CMA_ACCOUNT_CREATED: " + IS_CMA_ACCOUNT_CREATED)
	log.info("mobileNumber: " + mobileNumber)
	cnt++
}
log.info("Number of rows in the datasource is: " + cnt)

 

 

cancel
Showing results for 
Search instead for 
Did you mean: