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.