setNextRequest() doesn't work

I have the below tests shown in the ‘with default values’ folder. The first test is an initializer. The next 4 tests are unique and last two are common. Each unique test should call the common requests one after another(eg. ‘WC valid request with all default values’ calls Bump shipment which will then call Get Booked Status). So, I have set setNextRequest() in all 4 unique ‘tests’ to call common requests as mentioned previously.
I have the below in my initializer Tests:-

But when I run the folder through collection runner, it just runs the initializer, but not the rest of 4 unique tests:-

Can anyone suggest a solution for this?

Hey @oasis,

Not sure I fully understand what you’re trying to achieve here, with the way that you have this. Is the intention that it’s going to run the first request and then hit 5 other requests?

If it is, that’s not really how that function works - You should be setting up a workflow that moves between a request and once it’s completed, it moves on the to next specified request in the chain.

You mentioned running the folder in the Collection Runner, do any of those requests in that code live outside of that folder? All the requests mentioned would need to be selected in order for them to be run with that command.

Have you tried a simplified version of what you have to test your logic before trying to everything in one go?

The last case statement should also be setNextRequest(null) and not the way you have it or it would be looking for a request name called null.

@danny-dainton, This is what I am trying to achieve:-

WC valid request with all default values —> Bump Shipment --> Get Booked Status
WC valid request with pickup date on day 90 —> Bump Shipment --> Get Booked Status
WC valid request without ‘serviceCode’ —> Bump Shipment --> Get Booked Status
WC invalid request with pickup date>90 days —> Bump Shipment --> Get Booked Status

I do have set the above workflow implemented through ‘Tests’ in each of those above requests.
Eg. In ‘WC valid request with all default values’ I have—>
postman.setNextRequest(“Bump Shipment”);
Then in Bump Shipment I have postman.setNextRequest(“Get booked status”);

All requests reside inside the same folder.
I will correct the ‘null’ one.
Thanks

Are you trying to run parallel executions of these or multiple workflows at the same time?

As in send the first “dummy” request to start the workflow from the tests - loop through that request array and start each of those workflows, as you described above?

It’s not really parallel as the items will be picked up from array one after another as shown here:-

    myFunction();
function myFunction() {
var requests=[
    "WC valid request with all default values",
    "WC valid request with pickup date  on day 90",
    "WC valid request without 'serviceCode'",
    "WC invalid request with pickup date>90 days",
    "Stop"
];

for(i=0;i<requests.length;i++){
switch(requests[i]){
   
    case "WC valid request with all default values":   
    postman.setNextRequest("WC valid request with all default values");
    break;
    case "WC valid request with pickup date  on day 90":
    postman.setNextRequest("WC valid request with pickup date  on day 90");
    break;
    case "WC valid request without 'serviceCode'":
    postman.setNextRequest("WC valid request without 'serviceCode'");
    break;
    case "WC invalid request with pickup date>90 days":
    postman.setNextRequest("WC invalid request with pickup date>90 days");
    break;

    case "Stop":
    postman.setNextRequest("null");
    break;
default:
console.log("Array item not found");
}
}
1 Like

It it going back to this request and picking up the next one in the array though?

I’m confused by that function and the reason why you have that - what’s that doing, is something calling that?

Have you tried it for 1 request from that array and it worked through the flow the way you expected?

Hi @oasis,

If I understand your use-case correctly, the solution you have implemented would not work.

postman.setNextRequest() is not a “call” to the request it just sets which request will be executed, once the “current request” + “test-script” finish executing.

If postman.setNextRequest is called multiple times in a script, the last request set by postman.setNexRequest() will be executed.

In your initializer request, you have a for() loop iterating over request names and based on the current item you are setting the next-request to execute. Since the last request set by the for-loop is null, which means do not execute any other request, only your initializer request runs.

Read-up the documentation for more details: https://learning.postman.com/docs/running-collections/building-workflows/


Potential solution

On a high-level, you would want to store your array and an index in an environment variable.

For example in your initializer request’s Pre-request script do this

var requests = [ 
    "Stop",
    "WC invalid request with pickup date>90 days",
    "WC valid request without 'serviceCode'",
    "WC valid request with pickup date  on day 90"
];

pm.collectionVariables.set('requests', JSON.stringify(requests));

You can see I’ve removed “WC valid request with all default values” from the array, since this request will get executed automatically after the initializer-request completes. And I have reversed the order - since we will be using it as a stack.

Now - in your last request Get booked status - in the test script - at the very end - put the following.

var requests = JSON.parse(pm.collectionVariables.get(`requests`)); // Retrieve the collection variable's value
var nextRequest = request.pop(); // This will be next request's name we want to execute

// Update the collection variable's value
pm.collectionVariables.set('requests', JSON.stringify(requests));

if(nextRequest === 'Stop') { 
    postman.setNextRequest(null);
} else {
   postman.setNextRequest(nextRequest);
}

What this will do is, that if the popped item is ‘Stop’, Postman will stop execution - if not, it will use that value to set the next request to run.

1 Like

Hi @amit, That did the trick:) I have only started using Postman recently and came from a Java- RestAssured background… I really appreciate your answer. Thank you so much .
@danny-dainton, thanks for your input too.

1 Like

Is setNextRequest() only useful/relevant when running collections? And not when running individual requests? So, .setNextRequest() just queues up the next request to be ran but it doesn’t actually run it? @amit

Thanks.

Nevermind. The link you provided specifically says it only works when using executing as a collection, be it via Postman GUI or via Newman. Otherwise, it’s useless at the individual request level.

It’s not “useless” - There are many user who have used that function to great effect with their workflows.

What’s your use case? What are you trying to achieve?

Creating a new topic explaining all this might help others suggest potential solutions :pray:

Bu useless, I meant not usable at the individual request level. So on a day to day basis as I am trying to implement a single test and get the code working “just right”, I have to keep running a whole folder or a whole collection, right?

I can’t just run one test, i.e. the test I am working on?

I’m not sure if this topic/post has already been closed. I knew it was old but it seemed perfectly relevant to what I am doing. A new topic would just fragment the knowledgebase even more no?

Thanks.

Not really sure of the point you are trying to make here.

The function setNextRequest is just that. It sets the next request to run after the current request and all of the code in the test tab has completed, therefore I wouldn’t expect it to be usable at the individual level per se.

The function isn’t designed to just run the same request in a loop. It can do that (which sounds like your use case) but its designed to be able to run any other request within the collection.

That is what it was designed for, and for that purpose it works well but requires the collection runner.

It’s used to control work flows, and this only works within the collection runner.

It’s the same for looping through data files, this function also requires the collection runner to work.

I appreciate that the collection runner limits may be an issue while setting up and debugging the collection and tests, but that’s a different topic.

Correct @michaelderekjones . That is the impression I got as well.

I’m sorry, I realize I never explained I am trying to run the same individual request in a loop essentially. I’ve read all that I could find regarding data files, but that too is only available at the collection level it seemed to me. Also, I got the impression that the data file, be it CSV or JSON had to be pretty simple and basic. Basically a key:value structure or approach to the data file for a JSON file. No nesting of JSON objects and nothing hierarchical really.

Ultimately, I think I am trying to achieve data driven testing but for only one call/test, not the whole collection. And I think I am failing to connect the dots.

For example, a query to get a list of all the cities for a given State in the US. Rather than have 50+ individual calls/requests, I’d like to have just 1 and run it for each State. Without having to resort to using Newman or in a collection/folder/grouped way.

Is that doable in Postman?

The standard response would be “no”, not without the collection runner.

However, there is another way of creating a loop using sendRequest() in the tests tab, but this still means you have to send an initial request.

If you stored your test data centrally (for example in a Sharepoint list). You could call the API for Sharepoint to retrieve your test data (which would be the initial request), and then use a forEach loop in the tests tab in conjunction with sendRequest to loop through the test data. (Or you could potentially send a dummy request to Postman Echo for the initial request and just ignore the response and concentrate on the code in the tests tab).

This wouldn’t need the collection runner but will require writing more code but potentially not much more than using the array shift() method and setNextRequest().

Interesting, of those 2 workaround options I think the 2nd one seems more palatable and stable. I’ll have to look into that since I’d love to get data-driven testing working on an individual request level.

Thank you sir.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.