How can I stop a runner after 3 of 10 iterations

Lets say I have a collection that contains a single POST request that contains a single piece of data in the body. his requires no authentication of any kind

PUT name httP://whatever/api/

name: “nameOne”

This works fine for adding names one at a time but I have 1000 names to add.

In this case I can put them in a file with the heading myName, change my body to

name: “{{myName}}”

and use a collection runner to run through the file and add all my names. This works just fine.

So now I want to change the request so that if for any reason there is an error I can make it stop the runner.

I need to do this in a script somewhere, but where and how?

I’m not interested in setNextRequest(null) this would only exit me out of the current iteration if I had multiple steps. The rest of the iterations would still run.

I’m not really interested in setting a variable and testing that in the pre-request script the setting setNextRequest(null) cos I still have to run though all 1000 iterations.

Same with throw new Error(‘halt’);

What I’d like to know is what function, script, variable anything really can utilise in a script to do the exact same thing as the Stop Run button?

If I have an error on record 50 I don’t want to have to iterate through another 950 records before it gets to the end of the file doing nothing but avoiding the request. I just want it to stop.

Doesn’t look like it’s possible which is strange as you would think this would be a basic feature for a test runner for data driven tests (to either bail outright, or bail after a % of failures).

How to stop all request in Collection Runner on any error? - Help - Postman

How to stop Collection Runner on error? - Just getting started - Postman

There is an option of storing your test data in an array and using array shift and sendRequest().

Using array shift() you can retrieve the first element in the array and also delete the element at the same time. You save the updated array back to an environment or collection variable and use SendRequest() to run the same request repeatedly while the array is larger > 0.

Using this method, the sendRequest(null) would kill the entire loop, so you can error handle the response and bail if needed. (Not just when the array gets to zero).

The following shows an example of the logic involved.

Post Looping through an Array not working - Help - Postman