1

I have a set of the API Requests (which are saved under the same folder). I need to execute those multiple times, based on the iterations # specified in the Runner. But there is one (the very first) request which needs to be executed only once for the whole run. This request is the authentication request that collects auth-token.

I.e. I have Req1, Req2, Req3, Req4, saved under the same collection/folder. I will need to run this set 100 iterations. But the Req1 should be run only once, while Req2, Req3 and Req4 should be executed all 100 times.

Is there a way to tell Postman (or set it some other way) to execute Req1 only once at the beginning of the whole run?

KVN
  • 863
  • 1
  • 17
  • 35

1 Answers1

2

Postman has building workflow feature where you can specify which request you want to call next.

After you reach the Req4, call the Req2, which is after Req1 based on a counter. This can be achieved in the Tests tab of the postman request window.

Pseudo code - 
set 2 global/environment variables , iteration = <some number you need>, iteration_ref = 0
<In Req1 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
     postman.setNextRequest('Req2')

<In Req2 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
     postman.setNextRequest('Req3')

<In Req3 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
     postman.setNextRequest('Req4')

<In Req4 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
{
     postman.setGlobalVariable("iteration_ref", 
     Number(postman.getGlobalVariable("iteration_ref"))+1);
     postman.setNextRequest('Req2')
}

Or only in the last request, if you are pretty confident about the order of the requests set in the collection.

<In Req4 window>
if(pm.globals.get("iteration_ref") < pm.globals.get("iteration")-1)
{
     postman.setGlobalVariable("iteration_ref", 
     Number(postman.getGlobalVariable("iteration_ref"))+1);
     postman.setNextRequest('Req2')
}

Make sure, you have the Req1(Post Request) at first in the collection and only 1 iteration in the collection runner. We are using a global/env variable for iterations.

PS: I highly recommend having a simple python or js script using request library to call the APIs, the above flow is a witty hack.

Reference - https://learning.getpostman.com/docs/postman/collection_runs/building_workflows/

Seto Kaiba
  • 140
  • 10