When running manually then request is working fine, but when running using collection runner then only data at index 0 is getting added:
let contentTypes = [
{ type: "movie", id: "60ab9a14ed8cb6001d9413a3",pref:"movie_pref" },
{ type: "show", id: "60a74e4de0cc0e001cbca8aa",pref:"show_pref" },
{ type: "sport", id: "6304be62bb527520ba312e7c" ,pref:"sport_pref"},
{ type: "reels", id: "677f78267adfb1001e7042c0",pref:"entity_id" },
];
//let allProcessed = pm.environment.get("allProcessed") || false;
let index = pm.globals.get("index") || 0; // Use globals to persist between collection runs
if(index<contentTypes.length){
console.log("index at beginning"+ index);
console.log(contentTypes[index].type);
console.log(contentTypes[index].id);
pm.globals.set("content_type",contentTypes[index].type);
pm.globals.set("content_id",contentTypes[index].id);
pm.globals.set("pref",contentTypes[index].pref);
pm.globals.set("index",index+1);
console.log("Index after update: " + pm.globals.get("index"));
}
else{
pm.globals.unset("index");
pm.globals.unset("content_type");
pm.globals.unset("content_id");
pm.globals.unset("index");
pm.globals.unset("pref");
// pm.environment.set("allProcessed", true);
}
There is no loop in that code if that is what you are expecting.
You need to include setNextRequest to keep looping while your index is less then your contentType length.
Here is an example for you to consider.
if (typeof contentTypes === 'undefined' || contentTypes.length == 0) {
// new run - setup contentTypes array
contentTypes = [
{ type: "movie", id: "60ab9a14ed8cb6001d9413a3", pref: "movie_pref" },
{ type: "show", id: "60a74e4de0cc0e001cbca8aa", pref: "show_pref" },
{ type: "sport", id: "6304be62bb527520ba312e7c", pref: "sport_pref" },
{ type: "reels", id: "677f78267adfb1001e7042c0", pref: "entity_id" },
];
// new run - clear down existing collection variables
pm.collectionVariables.unset("content_type");
pm.collectionVariables.unset("content_id");
pm.collectionVariables.unset("pref");
}
let currentContent = contentTypes.shift(); // retrieve first object from array, then delete it from the array
// set collection values from current object
pm.collectionVariables.set("content_type", currentContent.type);
pm.collectionVariables.set("content_id", currentContent.id);
pm.collectionVariables.set("pref", currentContent.pref);
// are the collection variables set correctly?
console.log(pm.collectionVariables.get("content_type"));
console.log(pm.collectionVariables.get("content_id"));
// loop while contentType array is more than zero.
if (contentTypes.length > 0) {
currentRequest = pm.info.requestName;
pm.execution.setNextRequest(currentRequest);
}
Running against Postman Echo using Query Parameters
With the associated console logs.
cruxto
(Paul C)
February 20, 2025, 6:20pm
3
Hi @michaelderekjones ,
I like what you have done there.
I had a similar situation a couple of weeks ago, to avoid attaching a CSV file each time I ran the test.
I implemented it using the iteration parameter (pm.info.iteration) and set the number of iterations in the runner I needed to cover all tests. So by default, it would just run the first scenario.
I have 3 steps in my test, so if I implemented this approach, then on step 3 I add the pm.execution.setNextRequest(firstStepId) do you think that would work too?
Yes, but you will have to keep saving the array controlling the loop to an collection variable, as the global variable that I’m using to store the array won’t be fully available to the other requests.
It will get treated as [object Object],[object Object],[object Object]
I always JSON stringify the array when storing, and then JSON parse when retrieving.
1 Like