0

I tried to look everywhere, but no luck so far. I have a collection with 6 requests that run automatically, but I need the last request to run only once at the end of the last iteration. I tried to use this code in Pre-request Script of Request 5

if (pm.info.iteration === 0) {
    postman.setNextRequest("Request 6")
} else {
    postman.setNextRequest("Request 1")
}

and expected that the Request 6 would run only once after there is no more iteration to run, but I am doing something wrong since it is still not working.

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37

1 Answers1

0

you kind of mixed up pm.info.iterationCount and pm.info.iteration:

  • pm.info.iteration The value of the current iteration
  • pm.info.iterationCount The total number of iterations that are scheduled to run

See the documentation: Scripting with request info

You want to execute "Request 6" at the very end, this means, when iteration and iterationCount are equal.

Note that iteration start at 0, so you need to add + 1 when comparing it to iterationCount.

Also, if this condition does not match, you need to set the nextRequest to null.
"Request 1" will be executed automatically by the Collection Runner. If you set the nextRequest to "Request 1", you will end up in an endless loop.

The following will do you what you want, you can put it as pre-request script or test of "Request 5".

if (pm.info.iterationCount == pm.info.iteration + 1) {
    postman.setNextRequest("Request 6")
} else {
    postman.setNextRequest(null)
}
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37