Running specified requests in runner from Array

Hi,

Since the new version of Postman: Postman for Mac
Version 6.0.9
OS X 17.4.0 / x64

It seems the RUNNER is broken with the DATA.json file. Do you guys have any ideas about it?

Im uploading one simple file:
[{
“runnerVar”: true
}]

But the Environment variable that holds this, is not updated?
And now I was trying the globals, they are not updated. And I was trying the collection variables. I thought those where also just globals?

Did you hear this before?

Hi @calebrepkes - I am not sure why it’s not working for you.
Kindly recheck that you’ve selected the correct environment and checked ‘Persist Variables’ option.

This works: https://youtu.be/NuQD2Xmg3x8

Let me know if you’re still facing the issue or the steps to reproduce it because as you can see in the video, I have not been able to reproduce this issue.

Thanks

Uhm, no I dont have the Persist Variables option checked. Because I thought I deliberately dont want that. :slight_smile: I thought the variables should only persist if you want to reuse etc.?

i will try that out. Thnx

@calebrepkes - To make the variables persist outside the runner to the environment and globals in the whole app, you’ll have to check mark the ‘Persist’ button.

Else you can use your scripts in the runner only without persisting them. :slight_smile:

Im still lost. :slight_smile:

Maybe because I dont understand why anyone would want that?

With persistance, you can create variables (environment & globals) via runner, that you can then later use in Builder mode?

Perhaps it helps if I explain what I want to achieve. :slight_smile:
It is similar to a different post I saw around here, but maybe not quite it.

As app developer, I dont have control over my API’s, but I did put them all in one collection and then per category in a folder. But there are also requests that I dont want to fire in a runner.

So I decided to create a Pre-request piece of code, that creates an Array of Requests that I want to execute. Then I wrapped them in an IF Statement, so that it should only run this piece of code, when in Runner mode. And not in Builder mode.

Pre-request
{code}
// Below we have configuration to create our own flow of requests.
console.log(“^^^ BEFORE | IF in pre-request of Collection! ^^^”);
console.log(pm.info.requestName);

var runnerVar = pm.globals.get(“runnerVar”);

console.log(“This value: “+runnerVar);
if (runnerVar === true ){
console.log(”_____ IN IF in pre-request of Collection! ______”);
console.log(pm.globals.get(“runnerVar”));

var ArrayOfRequests = pm.globals.get("ArrayOfRequests");

if (!ArrayOfRequests) {
    var ArrayOfRequests = ["req1","Re2","Req3","Req4"];  
    pm.globals.set("ArrayOfRequests", ArrayOfRequests);
}
console.log("_____ ENDOF IF in pre-request of Collection! ______");

}
console.log(pm.globals.get(“ArrayOfRequests”));
console.log(“^^^ AFTER | IF/ELSE in pre-request of Collection! ^^^”);
{code}

So then I fire the actual requests. In my data file I actually have only this, because I just want to set the value to true, that we are in the runner mode.
{code} [{
“runnerVar”: true
}]
{code}

Then in the Test tab, on the collection level, I have this:
{code}
if (pm.globals.get(“runnerVar”) === true) {
var ArrayOfRequests = pm.globals.get(“ArrayOfRequests”);
var nextRequestOfArray = ArrayOfRequests.shift();
//pm.globals.set(“setNextRequestFlow”, nextRequestOfArray);

    var arrayLength = parseInt(ArrayOfRequests.length);
    console.log(arrayLength);
    console.log(ArrayOfRequests);
    
    console.log("<><><><> START THE IF in Collection level Tests <><><><>");
    if (arrayLength > 0) {
        postman.setNextRequest(nextRequestOfArray);
        console.log("<><><><> IF in Collection level Tests <><><><>");
        console.log(nextRequestOfArray);
    } else if ((nextRequestOfArray !== null) && (arrayLength === 0)) {
    //} else if (arrayLength === 0) {
        console.log("<><><><> ELSE in Collection level Tests 1 <><><><>");
        postman.setNextRequest(nextRequestOfArray);
        console.log(nextRequestOfArray);
    } else if (arrayLength < 0) {
        postman.setNextRequest(null);
        pm.globals.set("runnerVar", false);
        console.log("<><><><> ELSE in Collection level Tests 2 <><><><>");
    }
    console.log("!!!!!! WE DONE !!!!!!");

}
{code}

There is something that Im missing here.
But I want to select very specifically which requests should be run in the Runner, and not randomly everything. Plus I dont want to have this logic, fixed in each request it self. Because that is to me to low level. And not maintainable.

Plus if I have this working, I can set different Test scenarios in an Array to execute (comprised of multiple requests).

And when executing those, we could run several build jobs in i.e. Jenkins, to see if one of the Backend API’s is failing.

@shamasis << You must have some ideas too :wink: :sunny:

Thnx for the Youtube clip, its sooo elaborate :slight_smile:

And I notice you are setting the environment variable differently. But is that, because you are setting it directly from the ARRAYed data file? I saw you do: data[“runnerVar”].

I didnt know that was needed. Maybe I need to do that differently. Because right now I was just setting it by just overwriting the variable. That’s how I understood it from the documentation.

@calebrepkes - We are always here to assist you in the best way we can. :slight_smile:

There’s no specific reason as to why I did that but this is the method you can follow :
pm.environment.set("variable_key", "variable_value");

You can also use the other method as
postman.setEnvironmentVariable(key, value);

These won’t stop you from accessing the data object.

Let me know if you need anything else. :slight_smile:

Re-opened for the query regarding running specified requests in runner from Array.

I got my stuff working:

**Pre:**
// Below we have configuration to create our own flow of requests.
console.log("_____^^^_____ BEFORE | IF in pre-request of Collection! _____^^^_____");

if (data.runnerVar === true) {
    console.log("_____ IN IF in pre-request of Collection! ______");
   if (data.runnerCount === 0) {
       if (pm.environment.get("runnerCount") > 99) {
            console.log("_____ IN 2ND IF in pre-request of Collection! ______");
            pm.environment.set("runnerCount", 0);
            pm.globals.set("initialArrayLength", parseInt(data.ArrayOfRequests.length));
            pm.globals.set("ArrayOfRequests", data.ArrayOfRequests);
       }
   }
    console.log("_____ ENDOF IFS in pre-request of Collection! ______");
}
console.log(pm.globals.get("ArrayOfRequests"));
console.log("_____^^^_____ AFTER | IF/ELSE in pre-request of Collection! _____^^^_____");

Tests:

if (data.runnerVar === true) {
        var nextRequestOfArray = pm.globals.get("ArrayOfRequests").shift();
        var runnerCount = parseInt(pm.environment.get("runnerCount"));
        var arrayLength = parseInt(pm.globals.get("initialArrayLength"));
        console.log("Runner Count before if is: "+runnerCount)
        console.log("<><><><> START THE IF in Collection level Tests <><><><>");
        if (runnerCount < arrayLength) {
            console.log("<><><><> IF in Collection level Tests <><><><>");
            postman.setNextRequest(nextRequestOfArray);
            pm.environment.set("runnerCount", runnerCount+1);
        } else if (runnerCount >= arrayLength) {
            console.log("<><><><> ELSE in Collection level Tests <><><><>");
            postman.setNextRequest(null);
            pm.environment.set("runnerCount", 100);
            
        }

DATA:

[{
    "runnerVar": true,
    "runnerCount": 0,
    "ArrayOfRequests": ["Req1","req2","etc"]
}]

I just noticed that editing stuff, and then clicking, retry run. doesnt work with edited stuff.
Always stop runner and start again.

1 Like