Search code examples
postmanpostman-pre-request-script

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


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 ?


Solution

  • 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")
    }