Using setNextRequest to create a workflow

Hello,

My collection is generally built around doing contract testing. However, there are certain workflows I would like to test. I would like to reuse the existing requests so I can avoid having duplicate requests in my collection. My idea was to use setNextRequest to accomplish this. However, it is not working as expected.

As a proof of concept I setup a collection runner to run in a single folder. I created two placeholder requests: “Start Sequence” and “End Sequence”. I setup the collection runner to run those two requests (see snip). In the post request script for “Start Sequence” I have pm.execution.setNextRequest("player 539BD86C");. When I run the collection runner, only “Start Sequence” is executed.

console.log('Start Sequence Post request start');

pm.test("Status code is 404", function () {

pm.response.to.have.status(404);

});

pm.execution.setNextRequest("player 539BD86C");

console.log('Start Sequence Post request end');

From playing around with this some more, it seems like I still need to select the request that is being set by setNextRequest when I setup the collection runner. If this were a more complicated workflow, this would mean manually selecting several requests spread out in my collection.

In an ideal scenario, I could create a request like “User Management” and running that via a collection runner would auto-magically make the 8 different requests needed without me needing to manually select those requests. Is using setNextRequest the way to do this or is there another option?

Thanks.

setNextRequest just sets the next request that will run after the current request and all of the code in the pre-request and post-response scripts.

If you remove the request from the run sequence then it won’t be available as if you removed it from the collection.

As the request to run next is invalid, it looks like its just stopping the execution instead of running the end sequence like I thought it might do.

Looks like you are trying to achieve a conditional work flow, where Postman Flows is the tool more suited so that.

You could potentially use the following workaround if you can’t use Flows.

Define the requests that you want to run in an Environment variable or a data file.

Define the requests as an array.

You can then create a script at the folder or collection level that checks the current request name against the array. If the request name isn’t in the list, use the skipRequest() function to skip the current request.

https://blog.postman.com/optimize-your-api-testing-workflow-with-postmans-skip-request-feature

https://www.w3schools.com/Jsref/jsref_includes_array.asp

Thanks @michaelderekjones . I like your suggestion of coming at this from the other way by having a list of allowed requests.