Trying to use .setNextRequest efficiently

Hi, I’m a very fresh QA Tester currently teaching myself JavaScript, Postman and Jenkins from scratch for work.

I’m fiddling around with a practice API at the moment, trying to write up test cases and get a better workflow going with my requests.
I’m basically trying to achieve: Get List of Games>Post a Game>Get List of Games>Delete added Game>Get List of Games.

However, rather that duplicate the GET request, I wanted to use .setNextRequest and conditional logic to achieve this workflow with just the 1 GET request.
I’m sure there’s probably a much smarter way of doing what I’m trying to attempt. It’s my 1st time trying out this kind of thing. Essentially I figured if I had a counter, I could use that to create conditionals that would direct my requests the way I want. I’m having difficulty understanding where I should define variables because they’re in separate windows.

The Request names are just placeholders.
Code below–

On the GET request Tests Tab:
pm.globals.set(“numberOfGames”, 1);
pm.globals.set(“currentGameCount”, 0);
if (gameCount === pm.globals.get(numberOfGames)) {
postman.setNextRequest(“DEL”);
} else {
postman.setNextRequest(“POST”);
}

On the POST request Tests tab:
let gameCount = pm.globals.get(“currentGameCount”);
gameCount++;
pm.globals.set(“currentGameCount”, gameCount);

if (gameCount < pm.globals.get(“numberOfGames”)) {
postman.setNextRequest(“POST”);
} else {
postman.setNextRequest(“GET”);
}

On the DEL request Tests tab:
if (pm.response.to.have.status(204)) {
postman.setNextRequest(“DEL”);
} else {
postman.setNextRequest(“POST”);
}

I would do it more or less like this:

  • the post and delete will always return to get, so use postman.setNextRequest(“Get”);
  • the get request needs to figure out where to go next, post or delete. You can make the decision by evaluating a variable

Here is a sample collection exemplifying this:

https://www.getpostman.com/collections/8f80d004a0574c62ecce

It will stop after 3 iterations. I hope it helps.

1 Like

Hi there @LiamC -

As you said, there’s a bunch of ways you can do this kind of stuff. I would take a look at 1 of these templates that walk through how to use postman.setNextRequest(). You can import the template to play around with the examples. Or read through the written tutorials.

Both examples show you how to get and set your counter or state using environment and local variables respectively.

Link Checker - https://blog.getpostman.com/2017/06/23/check-for-broken-links-on-your-website-using-a-postman-collection/
DynamicLoop - https://ambertests.com/2019/01/28/postman-how-to-dynamic-iteration-within-a-collection/

1 Like

Thank you so much for the help!

A few things have just clicked in my head, so I get it now! :slightly_smiling_face:

I’m trying to get the simple setNextRequest function to work - and it seems to never call the next request. I tried your sample, in a new blank workspace, and when I call the Get - it just runs that one step.

Is there a setting or something I need to do to enable this call to be triggered? I’m scratching my head, seems like I am missing something simple but I don’t see it. (I tried my own simple test of 2 blank calls where all that is done is a console log statement and a setNextRequest call - calls in same folder, calls both one word names, lower case, set value to name of second call - just doesn’t trigger the second call…)

@jonsworkalias I forgot to specify this but you do need to run the collection in an automatic way, by using the Collection runner or Newman. Otherwise, it does not work.

1 Like

@vdespa Thanks, that would explain it!

I’ve found your video tutorials very helpful, thanks for putting them out there.

1 Like