Looping through variables in an array

Hi

I am getting a list of current currencies using a GET/currencies request and then saving all of the currencies as a variable.

I have got 4 POST requests to complete one end to end test and in the first of those requests I am using the currency variable I obtained at the beginning. I want to run all 4 POST requests but each time using a different currency until I have tested all of the currencies.

When searching for help, the only examples were running the same POST request and looping through all of the variables; however, I would like to run all of my variables through the end to end test using all 4 POST requests.

Could anyone offer any help or point me in the direction of where help for this could be found ?

Many thanks

Hi @carterusm69!

Here’s a Collection I built that works similarly to what you’re talking about. It gets a list of IDs in the first request, then loops through 3 other requests for each ID.

The docs page has an explanation of how it works, and you can click Run in Postman to get your own copy to play around with.

Hope that helps! Let me know if you have any questions.

1 Like

If I’m stepping through an array in successive Requests, I use the pop() function, which returns the last element from the array and removes it. Then update the array in the environment variable with the new reduced one.

So:

In the Pre-request of the first Request:

var currencies = ["Dollar", "Pound", "Euro", "Rupee"];
pm.environment.set("currencyArray", currencies);

In each of the 4 Request’s Tests:

var currencies = environment.currencyArray;
var currency = currencies.pop();

//Do stuff using currency

tests[currency] = true;

pm.environment.set("currencyArray", currencies);

Obviously this is destructive so if you need to keep the array, copy it to another variable.