Loop with setNextRequest() not Happening in One Run

This issue I am facing is that the code should run 2 iterations, but will only run 1 at a time. The first time I send the request, it runs the first iteration. If I send the request again, it will then run the second iteration. I have looked at the docs and existing question around this issue, and I believe that the code I have created follows the basic guidelines. I am wondering if the fact that I am getting values from env variables is having some effect. The token vars should really be env vars, but if that is the problem I guess I can change them to something else. I have also tried altering the “level” of other variables, since I think I may have had those wrong at some point. I have tried setting the bearerToken variable at all different variable levels (started at global, moved to env, and now finally to collection, but it hasn’t made 1 bit of difference). I thought for sure the problem was that the bearerTokenList variable had to be a collection level var, but that didn’t help.

Pre-request script

let qaMortToken = pm.environment.get("bearerToken_QA_Mort_Kirstin_Non_SSO");
let kirstinTestToken = pm.environment.get("bearerToken_Kirstin_Test_KW");
let bearerTokenList = pm.collectionVariables.get("bearerTokenList");
console.log("token list before: " +bearerTokenList )
if(!bearerTokenList || bearerTokenList.length == 0) {
    bearerTokenList = [qaMortToken, kirstinTestToken]; 
    console.log("token list after: " +bearerTokenList )
    console.log("token list length: " +bearerTokenList.length )
}
let currentToken = bearerTokenList.shift();
pm.collectionVariables.set("bearerToken", currentToken)
pm.collectionVariables.set("bearerTokenList", bearerTokenList);
console.log("current token:" +currentToken)
console.log("prerequest token list: " +bearerTokenList)

Test script:

let bearerTokenList = pm.collectionVariables.get("bearerTokenList");
console.log("test token list: " +bearerTokenList);
console.log("length: " +bearerTokenList.length)
console.log(bearerTokenList)
if(bearerTokenList && bearerTokenList.length > 0) {  
    console.log("in if block")  
    postman.setNextRequest("Create Child Org");
} else {
    console.log("in else block")
    postman.setNextRequest(null)
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

Request 1 console log (sanitized):
token list before:

token list after: token1, token2

token list length: 2

current token:token1

prerequest token list: token2

POST https://api/organizations/child
400
397 ms

test token list: token2

length: 1

(1) [“token2”]

in if block

Request 2 console log (sanitized):

token list before: token2

current token:token2

prerequest token list:

POST https://api/organizations/child
403
306 ms

test token list:

length: 0

in else block

Using Postman for Windows V 10.13.5 OS Platform win32 10.0.19045

Let’s simplify this. You don’t need any code in the tests tab to control this loop.

setNextRequest can be set in the pre-request script as this just controls what request is run after the request has completed.

if (typeof bearerTokenList === 'undefined' || bearerTokenList.length == 0) {
    bearerTokenList = ["qaMortToken", "kirstinTestToken"]; 
}

let currentToken = bearerTokenList.shift();

pm.collectionVariables.set("bearerToken", currentToken);

if (bearerTokenList.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

In this example, the bearerTokenList is a local variable. Local variables are available during the whole collection run, so technically you do not need to save the array to a collection or environment variable.

You do need to set the currentToken to one of those scopes (or global) because local variables aren’t available to the body tab, or the authorization helper.

I also recommend giving any collection or environment variables a different name to any local variables to avoid any potential scope issues. It’s just easier to troubleshoot, so in this case, I’ve called the collection variable “bearerToken”.

In my example, I’m calling Postman Echo with a query parameter called test with the value being the aforementioned “bearerToken” collection variable.

You can see from the test runner, that it run one iteration, same request name, but two requests.

image

With the console log also aligning.

image

I used to save the array to a collection or environment variable, and you can still do so if it helps with legibility of the code, but its not needed. I would recommend calling the collection variable something different to the local variable. Same as we are doing with the currentToken\bearerToken. (You could just call the local variable “currentTokenList” or something similar).

I’m afraid I’m not clear on some of the things you say in your reply, like what you mean when you say “You do need to set the currentToken to one of those scopes (or global) because local variables aren’t available to the body tab, or the authorization helper.” but I don’t think it’s really important at this time, because I am still not able to get this working. This is actually a little worse, since now it only runs the first iteration every time.

Maybe I’m not running this correctly? Am I supposed to be something other than just hitting the “Send” button on the request?

let qaMortToken = pm.environment.get("bearerToken_QA_Mort_Kirstin_Non_SSO");
let kirstinTestToken = pm.environment.get("bearerToken_Kirstin_Test_KW");
if (typeof bearerTokenList === 'undefined' || bearerTokenList.length == 0) {
    bearerTokenList = [qaMortToken, kirstinTestToken]; 
}

let currentToken = bearerTokenList.shift();

pm.collectionVariables.set("bearerToken", currentToken);

if (bearerTokenList.length > 0) {  
    currentRequest = pm.info.requestName;
    postman.setNextRequest(currentRequest);
} else {
    postman.setNextRequest(null)
}

You need to run it in the collection runner.

setNextRequest only works with the collection runner. It will have no effect if you just click SEND from the GUI.

How do you use the collection runner? I can’t seem to find it?

The run button that is available at the folder and collection level.

image

You won’t see it on the actual request.

If you are new to Postman I would recommend the Postman training links which are located under “Resources” in the Learning Centre.

Resources | Postman Learning Center

The “Galaxy API’s 101” course gets you used to sending requests and the GUI.

The “Galaxy Testing and Automation” gets you used to testing your responses and using variables, basic scripting, etc.

Thank you, I see it now. The docs on the collection runner do not indicate that you have to be at the folder or collection level to see the button. I was looking where they said the button should be, but could not find it. Also, the docs never indicate that you have to use the collection runner in order for this command to work. Thank you for your help

There are a few topics in the Learning Centre that mention postman.setNextRequest() and they all relate to the Collection Runner (as its a collection runner feature).

For example… Customize request order in a collection run | Postman Learning Center

Hope its working for you now.

Yes, it is working for me now. And I had already read the article you linked, which in no place mentions that you have to use the collection runner, although I see there is a link on how to run collections. It seems like a lot of things are implied but never directly stated. I’m not good at picking up on things like that. As far as I was concerned, I didn’t want to run the collection, just this 1 request, so it wasn’t relevant. So I just skipped over that article. Also, I’ve just been using postman to test manually up until now, so I’ve only been sending single requests, and had no idea that there was any such thing as a collection runner. I read an answer on Stack Overflow that pointed in that direction which made me think I was not running it correctly.

That’s why I recommended the “Galaxy API’s 101” Learning Centre resource.

It goes through the core features in the application (including the Collection Runner).

The “Galaxy Testing and Automation” resource takes it a step further, and goes through the basics of asserting your responses.

Once you are comfortable with this. You have the 15 Days of Postman for Testers challenges, which goes even deeper into common activities and workflows.