How can I add the loop?

Version - Postman 11

I have one of the request, I wanted to run 10 times…

1st request /Get - AUthenticate once

2nd Request / Post - I wanted to run 10 times…

CreateOrder - I got the body and Pre scripts as follows -

var currentCount = pm.environment.get("count");
var i =0;
while (i<=currentCount)
{
postman.setNextRequest("CreateOrder ");
}

Its not working…any help is appreciated?

setNextRequest() just sends the next request that will run after the current request and all of the code in the pre-request scripts and tests tab have completed.

Therefore you cannot use a while loop to control this.

Before we start providing code and example, can you please search the forum as this question has been asked plenty of times and there are quite a few examples out there.

Try one of those, and if you are still having issues, please post your code and an example response.

Please remember to use the preformatted text option in the editor when pasting code or JSON (to stop everything from being aligned to the left).

I found lot of links but none worked for me.

Share an example response, and I’ll show you how to develop an appropriate loop in a pre-request script.

1st request - Authenticate. - need to run only once. (1st request is done and working)
2nd request - CreateOrder - need to run 10 times…

I have added the environment variable count…

coming to 2nd request–

Pre-script
var currentCount = pm.environment.get(“count”);

Body:
Actual logic

Post-response:
pm.test(“Status code is 201”, function () {
pm.response.to.have.status(201);
});

if (currentCount > 0) {
currentCount = currentCount - 1;
pm.execution.setNextRequest("CreateOrder ");
pm.environment.set(“count”, currentCount);
}
else {
pm.execution.setNextRequest(null);
}

script run only once.
any help is appreciated.

Are you running this from the collection runner.

setNextRequest() only works with the collection runner.

On a side note, all of your code can be in the pre-request script as it sets the next request that will run after the request and the code in the pre-request scripts and tests tab has completed.

In relation to troubleshooting, please get used to console logging the variables.

Hey @docking-module-geol7 :wave:

Please wrap your code blocks with backticks (```) - It makes it so much easier for folks to read it. :pray:

This:
pm.test(“Status code is 201”, function () {
pm.response.to.have.status(201);
});

if (currentCount > 0) {
currentCount = currentCount - 1;
pm.execution.setNextRequest("CreateOrder ");
pm.environment.set(“count”, currentCount);
}
else {
pm.execution.setNextRequest(null);
}

vs this:

pm.test(“Status code is 201”, function () {
    pm.response.to.have.status(201);
});

if (currentCount > 0) {
    currentCount = currentCount - 1;
    pm.execution.setNextRequest("CreateOrder ");
    pm.environment.set(“count”, currentCount);
}
else {
    pm.execution.setNextRequest(null);
}

Thanks,

I am not using the collection runner as once its working, I am going to use Newman CLI.

Here is my effort.

This can all be done in the pre-request script.

x = 10; // number of iterations

if (typeof array === 'undefined' || array.length == 0) {
    array = Array.from({length: x}, (_, i) => i + 1)
    pm.environment.unset("currentCount");
}

let count = array.shift();
pm.environment.set("currentCount", count);

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

Running against a simple GET request with a query parameter.

With the corresponding console logs.

image

If you don’t need to use the counter elsewhere, you could potentially just use a local variable to control the loop like the following.

This works because local variables will persist throughout the collection run.

x = 10;

if (typeof array === 'undefined' || array.length == 0) {
    array = Array.from({length: x}, (_, i) => i + 1)
}

pm.variables.set("currentCount", array.shift())

if (array.length > 0) {  
    currentRequest = pm.info.requestName;
    pm.execution.setNextRequest(currentRequest);
} 

To test that its working, you will either need to use the collection runner, or do your testing in Newman.

If you run the request direct in Postman, then it will only ever run once.

Wouldn’t that need to be unset(), clear() doesn’t take an arg and would remove all variables from the environment.

You are correct, so I’ve edited the post.

1 Like

I was thinking that loop works in postman directly. This worked through Newman.