How to pick id's alternately in Pre Request Script

Hi everyone! This is my Pre-Request script:


var id = ["2492e54b-c55c-424c-9ed8-195b2202580e", "d6d1caaf-9b44-4a06-a5f7-689ed2f7823c", "689052c5-ac8f-4e93-b1c7-e50add5f316b", "7a34885a-f214-4dcd-9ca0-74dee4de0425", "a637aa53-a009-4c0e-821d-90bc3013ad06", "f9962c7b-5979-43de-adcc-92495522cfba", "499284ba-a7cf-402a-a060-30ea307421b0", "d3a10759-60bd-4b2f-bb9a-b3ce8cbb3dff", "3b98de63-3c75-4f7c-89e9-9138b988a394", "496c8e31-6c0b-492b-a9dc-772c69aacbbd", "ce2ea505-1038-4159-836b-b43b0b29fb9d", "11c0c5a5-51af-4f78-957a-841342ac2636", "1afa49ac-30de-4465-840f-ccfab5b4bdec", "a44aa352-6577-46f5-be9b-dc0c88814698", "fe9d7b3c-4779-4695-b149-d7542cbfb3b3", "06fd0bca-dd81-465c-9170-41aede997349", "963d8f2e-a837-4072-b9dc-56617b9f7818", "87c7c931-6b4a-4f4f-b05f-a9712e316c8d", "09f55916-7f2a-4d95-821b-77c66eb61780"]

for (x = 0; x < id.length; x++){

pm.environment.set('userId',id[x]);

}

I have an array with different Idโ€™s and I want to pick every id for next request in each iteration, for example in 3th iteration picked id should be this one โ€œ689052c5-ac8f-4e93-b1c7-e50add5f316bโ€, but in my code it always picks the last one.

It will always be the last element as you are repeatedly setting the environment variable in that loop.

A better way to do this is to use the JavaScript function array.shift().

Consider the following exampleโ€ฆ

// Step 1: Retrieve list of Ids
// Create a blank array to start with "[]".
var array = JSON.parse(pm.environment.get("Ids"));

// Step 2: If Ids have all been used, then reset the list
if (array.length === 0) {
    array = [
        "2492e54b-c55c-424c-9ed8-195b2202580e", "d6d1caaf-9b44-4a06-a5f7-689ed2f7823c",
        "689052c5-ac8f-4e93-b1c7-e50add5f316b", "7a34885a-f214-4dcd-9ca0-74dee4de0425", 
        "a637aa53-a009-4c0e-821d-90bc3013ad06", "f9962c7b-5979-43de-adcc-92495522cfba", 
        "499284ba-a7cf-402a-a060-30ea307421b0", "d3a10759-60bd-4b2f-bb9a-b3ce8cbb3dff", 
        "3b98de63-3c75-4f7c-89e9-9138b988a394", "496c8e31-6c0b-492b-a9dc-772c69aacbbd", 
        "ce2ea505-1038-4159-836b-b43b0b29fb9d", "11c0c5a5-51af-4f78-957a-841342ac2636", 
        "1afa49ac-30de-4465-840f-ccfab5b4bdec", "a44aa352-6577-46f5-be9b-dc0c88814698", 
        "fe9d7b3c-4779-4695-b149-d7542cbfb3b3", "06fd0bca-dd81-465c-9170-41aede997349", 
        "963d8f2e-a837-4072-b9dc-56617b9f7818", "87c7c931-6b4a-4f4f-b05f-a9712e316c8d", 
        "09f55916-7f2a-4d95-821b-77c66eb61780"
    ];
}

// Step3: Ensure that the array of Ids is not empty
pm.test("pre-req array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty;
});

// Step4: Use array.shift() to retrieve the first value from the array (this also deletes that value from the array)
var currentId = array.shift();
console.log(currentId);

// Step5: Save the current ID
pm.environment.set('userId',currentId);

// Step6: Re-save the array (which will now have one less item)
pm.environment.set("Ids", JSON.stringify(array));

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