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);
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.
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.