Workflow: how to run another test in loop

Have tried setNextRequest method however it doesn’t help when I have a need to run a test from another test in a loop (loop counter is the length of the response received in test1’s request).

Example:

collection structure
request1
request2
request3
request4
request5

Need this workflow

  • request 1 gets a list of data in response
  • for all the data received in request 1, execute request 2 (I also need to pass data to request 2 from here, but for now I can work around with environment variable )
  • move to request 3 and on (when above is done)

Tried
Request 1 test script:

length = responseBody.user_ids.length // let its 5
for (var counter = 0;  counter  < length; counter++){
   pm.setNextRequest("request2");
console.log("counter="+counter)
}
  • but the loop gets executed 5 times but calling the “request2” only once

Tried 1 more step
added pm.setNextRequest(“request1”); in the test on request2
now it makes infinte loop

Tired another step
To prevent infinite loop, thought to try out variable as counter to stop/enable next execution but in my it doesn’t help
I get the counter value in request1 which I can set to environment variable in its test script and access in request2 (decrease it here and call request1 again) but since testscript of request1 will be executed again the variable value will be reset and hence infinite loop again.

Checked this and this but it controls the execution based on control statements(response code), not the loop.

I only see the way to go is, sendRequest method, but it doesn’t look elegant because

  • the request2 also needs to have tests which can’t be achieved here (as the tests will be under parent now)
  • can’t maintain the reques2 in the collection as it will become part of the script now plus having request a post call, the script writing will make it less readable and maintainable.
for (var i=0; i<length; i++){
    pm.sendRequest({
        url: 'my_url',
        method: 'POST',
        body: {
            mode: 'formdata',
            formdata: [ {key: "key", value: ls[i]}]
        }
    }, function (err, res) {
        console.log(res.json());
    });
}

What is the decent way to achieve this?
Why can’t setNextRequest be ran in loop on the fly

Can you try postman.setNextRequest() instead of pm.setNextRequest()? The setNextRequest method is not available for the newer pm.* API.

Since defining your counter inside your for loop resets it, move your counter outside of the for loop. You don’t need the for loop at all, if you’re trying to loop through the requests.

Adding the following as a test script for request2 works for me. Using postman.setNextRequest to loop back to request1 until the counter is done.


// initialize counter as an environment variable, if not already done
if (!pm.environment.has("counter")) {
    console.log("Setting counter now");
    pm.environment.set("counter", 0);
 }
 
let currentCount = parseInt(pm.environment.get("counter")); 
let numberOfLoops = 5;

if (currentCount < numberOfLoops) {
    postman.setNextRequest("request1");
}

// increment the counter and re-save the value as an environment variable
currentCount++;
pm.environment.set("counter", currentCount);
2 Likes

Thank you @jetison yes found that I had a mistake in the flow as I was chaining multiple requests (8) requests based on the response of prev request), figured the problem. Your snippet helped in revisiting each line in my scripts again.
Sigh! working with these counters and variables together is damn messy, hence now after making setNextRequest work, I find sendRequest is much more readable :smiley:
Not sure if the sessions will bring any relief in this regard.

Hi Joyce,
I ran into another problem:
Postman flow’s scope as mentioned:

  • postman.setNextRequest() is always executed at the end of the current script. If you put this function before other code blocks, these blocks will still execute.

I’m having postman.setNextRequest() in pre-request, still for the last run, the current request’s test script is executed i.e. the task i’m doing in the “test” section, repeats 1 more additional time.
Shouldn’t only the pre-request be executed till end and move to the next request?
Am I missing something or is it a bug/not supported?
Does the statement means everything will be executed in that request (pre-request or test script) before moving to the next request?

This is the intended behavior (from the docs):

postman.setNextRequest() is always executed at the end of the current script. If you put this function before other code blocks, these blocks will still execute.

If it’s possible in your scenario, you could try setting the next request within the test script of the immediately preceding request.

1 Like

Apart from the docs that Joyce has already pointed out, Amber Race has published a blog post on a neat use case for postman.setNextRequest(). It might clear up some of your doubts :slight_smile:

2 Likes

What I meant here was - Does “current script” mean preRequest and Test both? Shouldn’t current script be any one of them wherever NextRequest is written.
If yes then document should be amended saying " always executed at the end of the current request"
Why: In the mentioned case, I expected only pre-request (current script) to be executed fully and doesn’t enter the Test section as NextRequest is triggered in the pre-request.

Yes, this is exactly correct :point_up:

It may be helpful to think about the difference between setting the next request and running the next request.

  • You can set (and reset) what will be the next request as many times as you’d like throughout the pre-request or test scripts. Whatever that value is at the end of the “current script” (i.e. any scripts associated with this request) will determine which request is run next.
  • The next request will run after the current request along with any scripts associated with that request.

If you don’t want your current request to run, instead of setting your next request in the pre-request script of the current request… :point_down:

And the Postman documentation is open source, if you would like to submit feedback or suggestions for the docs via an issue or even submit a pull request :+1:

1 Like

As this topic is still getting a lot of views I’ve created a collection (based on Amber Race’s blogpost) that demonstrates how you can loop and run tests over each element of the response of a previous request.

Fork it here and try it yourself! :arrow_right: Postman