21

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!

3 Answers 3

27

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

9
  • Is there a way by which the Run can be saved as a job?
    – Saurabhcdt
    Commented May 22, 2020 at 16:12
  • @Saurabhcdt, Yes, using Newman Commented May 23, 2020 at 12:12
  • hello, could you give me the real example cause your image isn't load properly.
    – monti
    Commented Oct 18, 2021 at 10:11
  • @monti I can see images, could you change your network and check? Commented Oct 18, 2021 at 12:47
  • I already change the network into public wifi, but still not load properly
    – monti
    Commented Oct 18, 2021 at 14:42
1

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")
}
0

It seems as though the Runner tab has been removed now?
For generating 'real' data, I found this video a great help: Creating A Runner in Postman-API Testing

Sending 1000 responses to the db to simulate real usage has saved a lot of time!

1
  • 1
    You can directly go to your Collection > Click View More Action (3 dots) > Run Collection. Commented Jun 16, 2023 at 11:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.