There was an error in evaluating the Pre-request Script: TypeError: Cannot read property 'shift' of undefined

Ok your last message was telling of what was wrong.

Your prerequest script in fact is resetting the array every time.

Pre-request script:

var buyerProgramId= [‘1961’, ‘1962’, ‘1972’, ‘1976’, ‘1977’];
pm.globals.set(“buyerProgramId”, JSON.stringify(buyerProgramId));

This is where you want to do your array manipulation. You should be popping the array and saving the new contents here.

let buyerProgramId = pm.globals.get('buyerProgramId');
if(!buyerProgramId){
  buyerProgramId= ['1961', '1962', '1972', '1976', '1977'];
}
else {
  buyerProgramId = JSON.parse(buyerProgramId);
}

let currentId = buyerProgramId.pop();
pm.globals.set('currentId', currentId);
pm.globals.set('buyerProgramId', JSON.stringify(buyerProgramId));

Then in your tests, you just need to evaluate if there are more items in the array

Tests:

let buyerProgramId = JSON.parse(pm.globals.get('buyerProgramId'));
if(buyerProgramId.length) {
  postman.setNextRequest(request.name);
} else {
  postman.setNextRequest(null);
}
2 Likes