I figured it out. For the “run” process I need to add the iterations amount. This doesn’t seem correct, why doesn’t it just loop based on the array length?
Is that first setNextRequest basically the same request looping back on itself.
When you say Tests, is this in the Tests tab on the request so it runs after the request.
I sort of get the logic. No idea if it actually works. I don’t have time today but may try and replicate this tomorrow as I can see where this could be useful.
I finally got round to testing this and it does work with some slight modifications.
I didn’t need to set the iterations to 3 in the run options.
This is the pre-request script.
var array = pm.collectionVariables.get("pkeys");
if(array.length === 0){
array = [31922, 456789, 75225];
}
pm.test("pre-req array is not empty", () => {
pm.expect(array).to.be.an("array").that.is.not.empty;
});
// console.log("pre-req array: " + array);
var currentpkey = array.shift();
// console.log("pre-req current key: " + currentpkey);
// console.log("pre-req updated array " + array);
pm.collectionVariables.set("clientpkey",currentpkey);
pm.collectionVariables.set("pkeys", array);
This is the tests tab script.
var array = pm.collectionVariables.get("pkeys");
var expectedResponse = pm.collectionVariables.get("clientpkey");
var actualResponse = parseInt(pm.response.json().args.test);
pm.test(`tests-tab clientpkey = ${expectedResponse}`, () => {
pm.expect(actualResponse).to.eql(expectedResponse);
});
if (array.length > 0){
postman.setNextRequest("Request1");
} else {
postman.setNextRequest(null);
}
I’m just using the Postman echo URL for the test including the clientpkey variable which it will echo back to you. I’ve included an assertion in the tests tab that check the response included the key that was sent.
https://postman-echo.com/get?test={{clientpkey}
You can see in the results that it ran three times, but in a single iteration.
I can see some uses for this approach, where you want a particular request to loop through data from a previous request.