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: