Running a request multiple times with different data sets

Hi,

I am trying make a request and call multiple times with different data but runs only one time.

In a GET request i have the URL

{{url}}/dev/users/isUserNameAvailable?username={{username}}

Then in Pre-request Script I have

var usernames = pm.environment.get(“usernames”);

if(!usernames){
usernames = [“AsdBsd”, “asd123”, 123456, “123asd”, “asdfghjklzxcvbnmqwertyuio”];
}

var currentUsername = usernames.shift();
pm.environment.set(“username”, currentUsername);
pm.environment.set(“usernames”, usernames);

And in the Tests I have

var usernames = pm.environment.get(“usernames”);

if (usernames && usernames.length > 0){
postman.setNextRequest("{{url}}/dev/users/isUserNameAvailable?username={{username}}");
}else{
postman.setNextRequest(null);
}

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});

After i go to Runner, select the collection and I let the Iteractions=1 and the test just run only one time. If I change the Iteractions, will run more times with different data (is what i whant)…but i need do this ? Because i dont know in all of cases how many interactions i need to put and all the tests will have diferent tests/interactions

Hi @ricardorio28,

If it helps, and if you are using the native Postman app (which I recommend), iterations will default to however many iterations you have in your data set. I can confirm this for CSV data, not sure about JSON.

I was researching a similar question myself and came across this article, which may help you: Is there a way of easily testing paged responses in Postman?.

It deals not with data as a source, but how to dynamically repeat a request based on response data (i.e., pagination). It may help steer you in the right direction.

Cheers,
Brad

1 Like

@ricardorio28

This is what I would do …

Create a JSON structured data file called UserNames.json …

[
{"username": "AsdBsd"},
{"username": "asd123"},
{"username": "123456"},
{"username": "123asd"},
{"username": "asdfghjklzxcvbnmqwertyuio"}
]

Then you just need to run the GET request …

{{url}}/dev/users/isUserNameAvailable?username={{username}}

in the Collection runner and select … UserNames.json … as your data file and run it. Once you select that file, in this example, the iteration count should automatically be set to 5.

For each iteration, it will pull a single “username”, in succession, in the file until it reaches the EOF. Each “username” in the file will be substituted into your {{username}} on the GET request.

You can then write something in the “test” script to see if aech of the GET requests was successful or not.

Hope this helps and was what you were looking for.

2 Likes

Thanks guys :wink:
But I changed the IF where was checking if the array ended, for this code and works…

if (usernames && usernames.length > 0){
postman.setNextRequest(“name_of_request”);
}else{
postman.setNextRequest();
}

ricardio28 , you are just changing URL to the name of the request ???

@ruthracena.8 Yep, the name needs to match.

In the screenshot, this is what the Tests tab looks like in the “[01] Get User Details” request.
I’m using postman.setNextRequest() to hit the “Before Each > Register User” request - notice the name has to match it exactly.

Also, take note if the request name is unique too. If you have multiple requests that have the exact same name in your collection, you’ll get undesired behaviour.

Hi @ricardorio28,

How did this work for you? I am using the same logic and for me instead of taking the values one by one for a request, the username is taking all the values at a time with comma separation. My values are all numbers, so modified the variable a bit so that it looks like below:

var userid = [1, 33, 5, 55];

Could you please let me know if you have made any changes according to the latest postman updates?

Thanks & Regards,
Sumithra Somu.

@Sumithra_Reddy I think you’ve set the user ID array directly, so it has been set as a string.
When setting the array to the environment variable, try using the following:

When you set the userIds in the envirnonment, use the following to stringify and set the data:

pm.environment.set('userIds', JSON.stringify(userIds);

Wherever you get it from the environment, use JSON.parse around it like so:

let userIds = JSON.parse(pm.environment.get('userIds'));

I think this should help you out.

HI I want to send 5 datas in a single GET url. how can i iterate them with different data set.

for e.g latitude ,longitude and zipcode.

please help

Hi Team,
How to execute multiple set of data when we are executing a collection? Above scenario works only if i execute a single endpoint.
Example:
Endpoint1 having 10 set of data.
Endpoint2 having 1 set of data.
Endpoint3 having 5 set of data.

In this case second and third request also executes 10 times.