Postman : how to rerun a subset of postman request multiple times

481 views Asked by At

I have a postman request in the below order

  1. GET Request
  2. GET Request
  3. POST Request
  4. POST Request <<<
  5. POST Request <<< Need to repeat only 4,5,6 - 10 times
  6. POST Request <<<

POST request 4 , 5 and 6 needs to repeated (say 10 times)

I tried copy of Request like below....

  1. GET Request
  2. GET Request
  3. POST Request
  4. POST Request
  5. POST Request
  6. POST Request
  7. POST Request 4 - Copy <<<
  8. POST Request 5 - Copy <<< i made a copy of 4, 5 , 6 :(
  9. POST Request 6 - Copy <<<

Is there a better way than copy ?

2

There are 2 answers

0
Jeeva On BEST ANSWER

postman.setNextRequest("request_name") should be helping your case.

Note: This will help only when you run the collection using postman runner and the name of the requests in the collection has to be unique.

in your case, rename the requests in your collection like

GET Request 1
GET Request 2
POST Request 3
POST Request 4
POST Request 5
POST Request 6

and place the below code in the test tab of the POST Request 6

var maxCount = pm.environment.get("maxCount"); //number of times you want to repeat requests 4,5,6
var currentCount = pm.environment.get("currentCount"); //set this to 0 when starting the test
if (currentCount < maxCount) {
  currentCount = currentCount + 1;
  pm.environment.set("currentCount", currentCount);
  postman.setNextRequest("POST Request 4")
}
1
Christian Baumann On

You can use pm.setNextRequest() to achieve that.

Put it at the end of the test script together with a counter/ condition to decide if you want to call a specific request or the next request in the collection.

I didn't totally understand the order/ condition about when you want to execute which request, so below code only explains the general idea:

let counter = pm.environment.get("counter");

if (counter < 3) {
    pm.setNextRequest("nameOfTheRequest");
    pm.environment.set("counter", counter++);
}