16

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!

BDM
  • 554
  • 2
  • 7
  • 16

3 Answers3

23

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

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
  • Is there a way by which the Run can be saved as a job? – Saurabhcdt May 22 '20 at 16:12
  • @Saurabhcdt, Yes, using [Newman](https://learning.postman.com/docs/postman/collection-runs/command-line-integration-with-newman/) – Divyang Desai May 23 '20 at 12:12
  • hello, could you give me the real example cause your image isn't load properly. – monti Oct 18 '21 at 10:11
  • @monti I can see images, could you change your network and check? – Divyang Desai Oct 18 '21 at 12:47
  • I already change the network into public wifi, but still not load properly – monti Oct 18 '21 at 14:42
  • let me ask you something about testing, if I have a url endpoint and I have to test with two condition like status code 200 if success and 400 if bad request argument. What should I do ? should I create two different request with several argument ? I hope you will answer, thank you so much before. – monti Oct 18 '21 at 14:43
  • It is up to you, you can manage both separately or you can define common response structure in success and error, then if the response body has error related field, check the status code is 400. – Divyang Desai Oct 18 '21 at 15:18
  • thank you so much for the answer, "you can define common response structure in success and error, then if the response body has error related field, check the status code is 400" could you give me example real in postman how to manage it in one request ? – monti Oct 18 '21 at 15:23
  • Hard to add code and samples in comments, can you ask a new question? there are other people who can help with different approaches – Divyang Desai Oct 18 '21 at 15:29
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")
}
Félix Paradis
  • 5,165
  • 6
  • 40
  • 49
Lee James
  • 91
  • 1
  • 5
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!

Jamey Hart
  • 31
  • 3