I'm trying to set tests for my APIs collection. The test should call three times, with three different parameters for each request in the collection.
I have added a pre-request script to my collection, containing two requests:
let countryIso3List = pm.collectionVariables.get("CountryIso3List");
if(!countryIso3List || countryIso3List.length == 0) {
countryIso3List = ["AFG", "SYR", "IDN"];
}
let countryIso3 = countryIso3List.shift();
pm.collectionVariables.set("CountryIso3", countryIso3);
pm.collectionVariables.set("CountryIso3List", countryIso3List);
Then in the test of the requests I have the following code:
const currentCountryIso3List = pm.collectionVariables.get("CountryIso3List");
if (currentCountryIso3List && currentCountryIso3List.length > 0){
postman.setNextRequest(request.name);
} else {
postman.setNextRequest(null);
}
pm.test("Status code is 200", function () {
pm.response.to.have.status(200);
});
It works fine only for the first call, putting "AFG" in the variable CountryIso3 which is then used to make the request.
After this initial run, I see that the CountryIso3List contains "SYR,IDN" as string and it fails to make the shift() command and it stops.
I've tried modifying the Pre-Request script by adding a split on the collection variable:
let countryIso3List = pm.collectionVariables.get("CountryIso3List");
if(!countryIso3List || countryIso3List.length == 0) {
countryIso3List = ["AFG", "SYR", "IDN"];
}
else{
countryIso3List = pm.collectionVariables.get("CountryIso3List").split(',');
}
let countryIso3 = countryIso3List.shift();
pm.collectionVariables.set("CountryIso3", countryIso3);
pm.collectionVariables.set("CountryIso3List", countryIso3List);
but it works fine on the first cycle, then if I repeat the test it brokes saying:
TypeError: pm.collectionVariables.get(...).split is not a function
How can I fix it?