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.