The following should work. (This is running against postman echo and I’ve just put the {{id}} into the URL instead of the body.
Pre-request script.
var array = JSON.parse(pm.collectionVariables.get("resources"));
if(array.length === 0){
array = [
"ba08971c-83de-777c-beb2-2dbe91e727e6",
"c6ec9412-e19e-999-bcc8-0a9900f3c0ba",
"268e8bd9-3381-0000-81d3-38fd4fa11110"
];
}
pm.test("pre-req array is not empty", () => {
pm.expect(array).to.be.an("array").that.is.not.empty;
});
var currentResource = array.shift();
pm.collectionVariables.set("id",currentResource);
pm.collectionVariables.set("resources", JSON.stringify(array));
Tests tab.
var array = JSON.parse(pm.collectionVariables.get("resources"));
var expectedResponse = pm.collectionVariables.get("id");
var actualResponse = pm.response.json().args.test;
pm.test(`tests-tab id = ${expectedResponse}`, () => {
pm.expect(actualResponse).to.eql(expectedResponse);
});
if (array.length > 0){
postman.setNextRequest("multiple_resources_delete_inonego");
} else {
postman.setNextRequest(null);
}
Run history. One iteration (run three times).
I think the bit you are missing is the storing of the array. Variables are stored as strings, so you have to Stringify them when storing, and JSON.parse when retrieving otherwise they will just be viewed as a string (not an array).
However I swear that the example I had previously saved and tested for this exact scenario didn’t need to do this.
It took me a while to work out what was going wrong, and amending the storing and retrieving of the array fixed it.
You need to have something in the resources array, or the first line of the pre-request script will error (as it needs something, even if its a blank “” array. It can’t just be empty though.
Pretty sure I didn’t need to do this previously either.
Not sure if something changed with how variables are stored in Postman 10, as it took me while to get this working again but it does work.