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.

4 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.

Something to NOTE about this is that: it only works when you run via the Collection Runner. It does not work if you just hitting the send button on postman UI screen.

/* 1. create a global variable called iterationNumber with value as 0. This would be incremented after every request executes.
2. create one dummy request called driver with below code in test tab

if pm.global.get(iterationNumber)=0 {
pm.sendnextrequest(Testcase 1)}
if pm.global.get(iterationNumber)=1 {
pm.sendnextrequest(Testcase 2)}
if pm.global.get(iterationNumber)=2{
pm.sendnextrequest(Testcase 3)}

  1. Create a dummy request called End with code as
    pm.sendnextrequest(Driver)
    3.In Test case 1 ,at the end of test write code as
    pm.global.set(iterationNumber,1)
    pm.sendnextrequest(End)

4.In Test case 2 ,at the end of test write code as
pm.global.set(iterationNumber,2)
pm.sendnextrequest(End)

5.In Test case 3 ,at the end of test write code as
pm.global.set(iterationNumber,3)
pm.sendnextrequest(End)
Keep a excel (csv) file with test 1,2 and 3 in separate rows and load in runner with 3 iterations and run the collection. The runner will start with driver script, go to first test case and fetch data with first excel row, then the control would return to end test case and then to driver again and iteration 2 would start , test case 2 would get executed with data from row 2 in excel and likewise. */

Hey :wave:

We’ve created this collection to demonstrate how you can loop over a GET or a POST request while altering the data at each request:
https://www.postman.com/postman/workspace/postman-answers/collection/9215231-25b72aab-2c6a-4941-832d-dd47e142ff2a?ctx=documentation

If you’re logged into your account, you can fork it to your own workspace and try it out directly. :slightly_smiling_face:

I believe what you want to know is described here.

@arlem referred to you example but not sure why when i run in collection, request passed is always null.

body : "sla":"{{slaNew}}"

pre-requisite:

let usernames = pm.collectionVariables.get("usernames");

var currentUsename;

if(!usernames || usernames.length == 0) {

usernames = ["Express","Standard"];

}

let currentUsername = usernames.shift();

pm.collectionVariables.set("slaNew", currentUsename);

pm.collectionVariables.set("usernames", usernames);

Test:
var usernames;

if (usernames && usernames.length > 0){

postman.setNextRequest("Request name");

} else {

postman.setNextRequest(null);

}

my out of collection run shows null for the variable passed:

Can this method be used to pass an array of objects?

Hey @mskeith

Welcome to the Postman community :wave:

As the original post has been marked with a solution, could you create a new topic explaining all the details of your use case, please?

This will allow other community members to provide more contextually relevant assistance :trophy: