I need to run a the same request for each user (10 users total)

Hi there,

I’m super new to postman and need to run the same request for 10 different users. Not sure what the most standard best practice is for doing this.

I used setNextRequest() and created an array of objects. Each object representing a user. I stored this HUGE stringified array as an environment variable (“allUsers”) and every time I need to run a request for all users I pull from the huge environment variable, JSON.parse(), set the current user, and then loop through all of them with setNextRequest(). (I need to parse “allUsers” for every loop and the index is updated through an env var, then reset on the last request test script. )

Is this bad practice? Is there a more standardized way of looping? Is it bad to have an environment variable that is a huge stringified array? (I used to give every param in the array its own env var name but the list got really long.) (I’m also afraid that if I stop the tests midway through the index I am using to traverse the array won’t get reset which could ruin tests on a re-run.)

[
{name: “Jon”, email: "[email protected], isHappy: false, userType: “intermediate”},
{name: “Sam”, email: "[email protected], isHappy: true, userType: “advanced”}
]

Here is my pre-request script:

if (!pm.environment.has('userIndex')) {
    pm.environment.set('userIndex',0);
}

let allUsersToTest = JSON.parse(pm.environment.get('allTestUsers'));
console.log(allUsersToTest);    
console.log(pm.environment.get("userIndex"));    
let currUser = allUsersToTest[pm.environment.get("userIndex")]; 
pm.environment.set('currUser',currUser);
console.log(currUser);
pm.environment.set('email', currUser.email); 
pm.environment.set('aad-userid', currUser.id); 
let newUserIndex = Number(pm.environment.get("userIndex")) + 1; 
pm.environment.set('userIndex',newUserIndex);

And the end of my test script:

let allUsersToTest = JSON.parse(pm.environment.get('allTestUsers')); 
    if (!(pm.environment.get("userIndex") >= allUsersToTest.length)) {
        postman.setNextRequest('same_test');
    } else {
        pm.environment.unset('userIndex');
    }

Could someone share best practices?

Personally, I’d probably be exporting the username etc from your array and saving it as a CSV that could be fed into the collection runner.

The option for this is as screenshotted here;

More infor can be read here

1 Like