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

3

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!

Chamonix answered 7/3, 2019 at 13:38 Comment(0)
S
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

Scaleboard answered 8/3, 2019 at 7:45 Comment(9)
Is there a way by which the Run can be saved as a job?Evocator
@Saurabhcdt, Yes, using NewmanScaleboard
hello, could you give me the real example cause your image isn't load properly.Finella
@Finella I can see images, could you change your network and check?Scaleboard
I already change the network into public wifi, but still not load properlyFinella
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.Finella
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.Scaleboard
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 ?Finella
Hard to add code and samples in comments, can you ask a new question? there are other people who can help with different approachesScaleboard
D
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")
}
Danny answered 30/7, 2020 at 19:51 Comment(0)
C
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!

Circe answered 24/11, 2022 at 8:28 Comment(1)
You can directly go to your Collection > Click View More Action (3 dots) > Run Collection.Strophanthin

© 2022 - 2024 — McMap. All rights reserved.