Calling setNextRequest

Reading https://learning.postman.com/docs/collections/running-collections/building-workflows/ it states that setNextRequest can be used in pre-request or test scripts. I have included a call to it in a function defined in the pre-request script for the collection but called in the Tests script, butit doesn’t seem to have an effect. Is this the expected behaviour?

In the pre-request script for the collection I have

Object.prototype.testResponseSpecifyResponseCode = (pm, responseCode) =>{
     ... 
     ...

    if( condition)
    {
        postman.setNextRequest(nextRequest);
    }
    else
    {
        postman.setNextRequest(null); 
    }
}

and then in the Test for each request I call the method. IT is executed but when it executes the setNextRequest(null) it continues to execute requests.

Hey @ceepan :wave:

Could you expand a little more of this workflow please?

You have your example code above, in your Pre-request script and you’re calling that function after the request execution, in the Tests script?

Hi Danny

The abbreviated example is within the pre-request script of the collection to make it common reusable code. I call the function testResponseSpecifyResponseCode from the Tests script in each request. However, the call to postman.setNextRequest(null) doesn’t stop the remaining request in the collection being executed. However if I add postman.setNextRequest(null) into the Tests script it does.

Thanks,
Paul

Hey @ceepan,

From what I can see and the little local version of your code I had running, you need to also pass in the postman arg into the function. Just like you have with the pm arg.

Object.prototype.testResponseSpecifyResponseCode = (pm, postman, responseCode)

Then add that to the function args on the Test script.

this.testResponseSpecifyResponseCode(pm,postman,200);

If you have nothing in there using the pm set of APIs, you could just change that to postman instead.

Thanks Danny. You were spot on.

2 Likes

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