Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a request in Postman multiple times with different data only runs once

I am new to Postman and running into a recurrent issue that I can’t figure out. I am trying to run the same request multiple times using an array of data established on the Pre-request script, however, when I go to the runner the request is only running once, rather than 3 times.

Pre-request script:

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

if(!uuids) {
uuids= [“1eb253c6-8784”, “d3fb3ab3-4c57”, “d3fb3ab3-4c78”];
}

var currentuuid = uuids.shift();
pm.environment.set(“uuid”, currentuuid);
pm.environment.set(“uuids”, uuids);

Tests:

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

if (uuids && uuids.length>0) {
postman.setNextRequest(myurl/?userid={{uuid}});
} else {
postman.setNextRequest();
}

I have looked over regarding documentation and I cannot find what is wrong with my code.

Thanks!

like image 225
BDM Avatar asked Mar 07 '19 13:03

BDM


2 Answers

Pre-request script is not a good way to test api with different data. Better use Postman runner for the same.

First, prepare a request with postman with variable data. For e.g

enter image description here

Then click to the Runner tab

enter image description here

Prepare csv file with data

uuids
1eb253c6-8784
d3fb3ab3-4c57
d3fb3ab3-4c78

And provide as data file, and run the sample.

It will allow you run the same api, multiple times with different data types and can check test cases.

enter image description here

like image 160
Divyang Desai Avatar answered Oct 18 '22 23:10

Divyang Desai


You are so close! The issue is that you are not un-setting your environment variable for uuids, so it is an empty list at the start of each run. Simply add pm.environment.unset("uuids") to your exit statement and it should run all three times. All specify the your next request should stop the execution by setting it to null.

So your new "Tests" will become:

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

if (uuids && uuids.length>0) {
    postman.setNextRequest(myurl/?userid={{uuid}});
} else {
    postman.setNextRequest(null);
    pm.environment.unset("uuids")
}
like image 1
Lee James Avatar answered Oct 18 '22 23:10

Lee James