Challenges with API Runner Loops - Help?

Hi there! Iā€™m having issues where I need to send multiple identical API calls to my endpoint with different attribute values. I am attempting to do this using multiple runner iterations with a pre-request script. Iā€™ve scrubbed this but this is the behavior Iā€™m getting. My first 4 variables are synchronized though not starting at variable value 0, my 5th variable is totally out of synch with the rest. I am no JS expert by any means, in fact I havent touched JS in about 8 years so Iā€™m struggling quite a bit. I know the .shift() method is going to shift my arrays so Iā€™ve added a prepending null value to each of my arrays however, my 5th variable is still off by X. Any help would be appreciated!!
{
ā€œHotelnameā€: ā€œ{{hotelname}}ā€,
ā€œHotelOwnerā€: ā€œ{{hotelOwner}}ā€,
ā€œAddressā€: ā€œ{{address}}ā€,
ā€œStateā€: ā€œ{{state}}ā€,
ā€œzip-codeā€: ā€œ{{zip}}ā€
}

let hotelnames = pm.collectionVariables.get(ā€œhotelnamesā€);
let hotelOwners = pm.collectionVariables.get(ā€œhotelOwnersā€);
let addresses = pm.collectionVariables.get(ā€œaddressesā€);
let states = pm.collectionVariables.get(ā€œstatesā€);
let zips = pm.collectionVariables.get(ā€œzipsā€);

if(!hotelnames || hotelnames.length == 0){hotelnames = [ā€œHotel-1ā€, ā€œHotel-2ā€ ā€œHotel-3ā€, ā€œHotel-4ā€, ā€œHotel-5ā€];
}

if(!hotelOwners || 'v.length == 0){hotelOwners = [ā€œHotelBrand-1ā€, ā€œHotelBrand-2ā€ ā€œHotelBrand-3ā€, ā€œHotelBrand-4ā€, ā€œHotelBrand-5ā€];
}

if(!addresses || addresses.length == 0){addresses = [ā€œHotelAddress-1ā€, ā€œHotelAddress-2ā€ ā€œHotelAddress-3ā€, ā€œHotelAddress-4ā€, ā€œHotelAddress-5ā€];
}

if(!states || states.length == 0){states = [ā€œHotelState-1ā€, ā€œHotelState-2ā€ ā€œHotelState-3ā€, ā€œHotelState-4ā€, ā€œHotelState-5ā€];
}

if(!zips || zips.length == 0){zips = [ā€œHotelZip-1ā€, ā€œHotelZip-2ā€ ā€œHotelZip-3ā€, ā€œHotelZip-4ā€, ā€œHotelZip-5ā€];
}

let currentHotel = hotelnames.shift();
pm.collectionVariables.set(ā€œHotelnameā€, currentHotel);
pm.collectionVariables.set(ā€œhotelnamesā€, hotelnames);

let currentOwner = hotelOwners.shift();
pm.collectionVariables.set(ā€œHotelOwnerā€, currentOwner);
pm.collectionVariables.set(ā€œhotelOwnersā€, hotelOwners);

let currentAddress = addresses.shift();
pm.collectionVariables.set(ā€œAddressā€, currentAddress);
pm.collectionVariables.set(ā€œaddressesā€, addresses);

let currentState = states.shift();
pm.collectionVariables.set(ā€œStateā€, currentState);
pm.collectionVariables.set(ā€œstatesā€, states);

let currentZip = zips.shift();
pm.collectionVariables.set(ā€œzip-codeā€, currentZip);
pm.collectionVariables.set(ā€œzipsā€, zips);

status 200, output:

Hotel-2
HotelBrand-2
HotelAddress-2
HotelState-2
HotelZip-4

Hotel-3
HotelBrand-3
HotelAddress-3
HotelState-3
HotelZip-5

Hotel-4
HotelBrand-4
HotelAddress-4
HotelState-4
HotelZip-1

Hotel-5
HotelBrand-5
HotelAddress-5
HotelState-5
HotelZip-2

Hotel-1
HotelBrand-1
HotelAddress-1
HotelState-1
HotelZip-3


is it possible to force all of my values to an initial value using:

let hotelnames = pm.collectionVariables.get(ā€œhotelnamesā€);

hotelnames++

let hotelOwners = pm.collectionVariables.get(ā€œhotelOwnersā€);

hotelOwners++
let addresses = pm.collectionVariables.get(ā€œaddressesā€);

adresses++
let states = pm.collectionVariables.get(ā€œstatesā€);

states++
let zips = pm.collectionVariables.get(ā€œzipsā€);

zips++

pm.collectionVariables.set(ā€œHotelnameā€,ā€œHotel-1ā€)
pm.collectionVariables.set(ā€œHotelOwnerā€,ā€œHotelOwner-1ā€)
pm.collectionVariables.set(ā€œAddressā€,ā€œHotelAddress-1ā€)

pm.collectionVariables.set(ā€œStateā€,ā€œHotelState-1ā€)

Can you edit your post and use the preformatted text option in the editor to post code or JSON.

It stops everything from being aligned to the left, which makes it hard to read, or copy for testing.

Being that the only difference on each run is the number at the end of the variable, I donā€™t think you need separate arrays for each key. You just need to track the iteration number in your array, and then set the appropriate variables.

You technically donā€™t need to set any collection variables for this.

For example.

Create a request with a blank body, but set it to raw\JSON.

image

The following code is used in the pre-request script.

if (typeof iterations === 'undefined' || iteration.length == 0) {
    iterations = ["1", "2", "3", "4", "5"];
}

let currentIteration = iterations.shift();

let requestJSON =
{
    "Hotelname": `Hotel-${currentIteration}`,
    "HotelOwner": `HotelBrand-${currentIteration}`,
    "Address": `HotelAddress-${currentIteration}`,
    "State": `HotelState-${currentIteration}`,
    "zip-code": `HotelZip-${currentIteration}`
}

pm.request.body.raw = requestJSON;

if (iterations.length > 0) {
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

One iteration, run five times.

Here is the matching console log, and Iā€™ve expanded out the bodies for request 1 and 5.

So, this was a sanitized example. My script contains complex values for each variable in each iteration, so I canā€™t simply append an integer for each iteration. I was just trying to show the example that the last variableā€™s value is out of sync with the rest of the variable values.

Noted for my next post on the preformatted text option though, thanks!

You can still use your original approach.

if (typeof hotelnames === 'undefined' || hotelnames.length == 0) {
    hotelnames = ["Hotel-1", "Hotel-2", "Hotel-3", "Hotel-4", "Hotel-5"]; 
}

if (typeof hotelOwners === 'undefined' || hotelOwners.length == 0) {
    hotelOwners = ["HotelBrand-1", "HotelBrand-2", "HotelBrand-3", "HotelBrand-4", "HotelBrand-5"]; 
}

if (typeof addresses === 'undefined' || addresses.length == 0) {
    addresses = ["HotelAddress-1", "HotelAddress-2", "HotelAddress-3", "HotelAddress-4", "HotelAddress-5"]; 
}

if (typeof states === 'undefined' || states.length == 0) {
    states = ["HotelState-1", "HotelState-2", "HotelState-3", "HotelState-4", "HotelState-5"]; 
}

if (typeof zips === 'undefined' || zips.length == 0) {
    zips = ["HotelZip-1", "HotelZip-2", "HotelZip-3", "HotelZip-4", "HotelZip-5"]; 
}

let currentHotel = hotelnames.shift();
let currentOwner = hotelOwners.shift();
let currentAddress = addresses.shift();
let currentState = states.shift();
let currentZip = zips.shift();

let requestJSON =
{
    "Hotelname": currentHotel,
    "HotelOwner": currentOwner,
    "Address": currentAddress,
    "State": currentState,
    "zip-code": currentZip
}

pm.request.body.raw = requestJSON;

if (hotelnames.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

Iā€™ll just show the console log for the last entry, to show that the values are still in sync.

image

However, being that all of your arrays have the same number of elements, you only really need to check one of the arrays and you will get the same outcome.

if (typeof hotelnames === 'undefined' || hotelnames.length == 0) {
    hotelnames = ["Hotel-1", "Hotel-2", "Hotel-3", "Hotel-4", "Hotel-5"]; 
    hotelOwners = ["HotelBrand-1", "HotelBrand-2", "HotelBrand-3", "HotelBrand-4", "HotelBrand-5"]; 
    addresses = ["HotelAddress-1", "HotelAddress-2", "HotelAddress-3", "HotelAddress-4", "HotelAddress-5"]; 
    states = ["HotelState-1", "HotelState-2", "HotelState-3", "HotelState-4", "HotelState-5"]; 
    zips = ["HotelZip-1", "HotelZip-2", "HotelZip-3", "HotelZip-4", "HotelZip-5"]; 
}

let currentHotel = hotelnames.shift();
let currentOwner = hotelOwners.shift();
let currentAddress = addresses.shift();
let currentState = states.shift();
let currentZip = zips.shift();

let requestJSON =
{
    "Hotelname": currentHotel,
    "HotelOwner": currentOwner,
    "Address": currentAddress,
    "State": currentState,
    "zip-code": currentZip
}

pm.request.body.raw = requestJSON;

if (hotelnames.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

Thank you! I used your example with the multiple arrays using typeof and that did the trick! I forgot to set my Test and thus I didnā€™t have my:
pm.test("Status code is 201", function () { pm.response.to.have.status(201); }
So it looped beyond where I wanted it to go. But, it stayed in sync finally!! Really appreciate the assist here!

The reason we use (typeof hotelnames === ā€˜undefinedā€™) instead of (if !hotelnames) is that the latter will cause a fatal error if the variable doesnā€™t exist and stop any other code from running, where typeof will check that the variable exists, that its not null, or undefined and not cause a fatal error when it runs, so any other code will still run.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.