Perform actions on the basis of iteration count in Collection Runner

I am doing some speed testing and I have the following issue:

  • I am running the collection and uploading the CSV file
  • I want to list an array that grabbed some data from all the calls but only after the last call

I managed to do it when I know the number of the rows in the CSV file but how can I achieve this when I donā€™t know the number of rows.

So in my case, I am grabbing the times into an array like this:

response_array = globals[ā€˜response_timesā€™] ? JSON.parse(globals[ā€˜response_timesā€™]) : []
response_array.push(responseTime);
postman.setGlobalVariable(ā€œresponse_timesā€, JSON.stringify(response_array));

And in the end, I have this piece of code that lists the array in the console after all the rows finished:

if (response_array.length === 25) {
console.log("response_times are: " +pm.globals.get(ā€œresponse_timesā€));
}

This works because I know that there are 25 rows in my CSV, but how can I achieve the same thing without knowing how many rows the CSV has.

Thank you for taking the time to read this or respond!

Hey @gherghinoiu, the good news is that itā€™s solvable!

So, if you scroll down the docs - https://www.getpostman.com/docs/v6/postman/scripts/postman_sandbox_api_reference

Youā€™ll see that in the pm.info object, thereā€™s a property called as iterationCount which is the total number of iterations that are scheduled to run.

Now,
Total number of iterations === Number of rows in your CSV File

Also, instead of using the response_array.length you can use another property of the pm.info object viz. pm.info.iteration which gives you the current iteration number.

So, to sum it up:

In your test-script you can put the following code snippet:

// Since, pm.info.iteration starts from 0
// Thus, we'll check if iteration === iterationCount - 1
if (pm.info.iteration === pm.info.iterationCount - 1) {
    console.log(`response_times are: ${pm.globals.get('response_times')}`);
}
2 Likes

@singhsivcan Thank you for the fast reply. This is exactly what I needed! Best wishes!

1 Like

Most welcome!
Feel free to reach out in case you need any further help!