How to add a pause between 2 requests in POSTMAN

2020-05-22 10:07发布

I have a collection of requests in POSTMAN. I wanted to add a pause between 2 requests but I couldn't find a way to do so from reading their docs. Any idea?

UPDATE I only wanted to put a pause after one request instead of after every request in the collection.

9条回答
我想做一个坏孩纸
2楼-- · 2020-05-22 10:35

I know two possible ways to do this

Method I

Run your request as a collection. (https://www.getpostman.com/docs/collections) Use Newman (Postman's collection runner from command line) to run your collection with --delay flag. The delay input value is in milliseconds.

Method II

This is a nice hack which I found here https://github.com/postmanlabs/postman-app-support/issues/1038. You can add a delay function to your test script in Postman.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-05-22 10:36

Using javascript's busy wait is a good hack but it makes your CPU hot and the app unresponsive. I figured out this solution using postman-echo.

Assuming you want to add a long delay between Request_A and Request_B.

First, in Request_A's test script set up an env var to mark the start.

environment.delayTimerStart = new Date();

Then, create a GET request in the creation (here called 'Delay 10s'). It makes a GET on https://postman-echo.com/delay/10 (It returns after 10s)

In its test script, add

var curDate = new Date();
if (curDate - environment.delayTimerStart < delay_time_in_sec*1000) {
    postman.setNextRequest('Delay 10s');
} else {
    postman.setNextRequest("Request_B");
}

In this way you can add a delay of any length.

Note: 10 sec is the maxium value that postman-echo accepts. If you just need a short delay, simply GET https://postman-echo.com/delay/[1~10].

查看更多
地球回转人心会变
4楼-- · 2020-05-22 10:38

In case someone is still looking for this - You can add delay after/before 1 of many test in a collection you can use:

setTimeout(function(){}, [number]);

where 'number' is the milliseconds. If it's added at 'Tests' it will be executed after request is send. If it's added at 'pre-request tests' it will be executed before request is send.

查看更多
叛逆
5楼-- · 2020-05-22 10:40

You can use JavaScript setTimeout method. This will make sure that your method will be executed after given delay.

enter image description here

  setTimeout(checkStatusCode, 500);

    function checkStatusCode() {

        pm.sendRequest('https://postman-echo.com/get', function (err, res) {

            tests['status code should be 200']= res.code ===200;

        });
    }
查看更多
倾城 Initia
6楼-- · 2020-05-22 10:41

Try something like this-

if(jsonBody.responseCode=="SUCCESS"){

  setTimeout(function(){
          console.log("Sleeping for 3 seconds before next request.");
  }, 3000);

  postman.setNextRequest("nextAPI");

}
查看更多
戒情不戒烟
7楼-- · 2020-05-22 10:42

looking at the current documentation if you are using Postman Runner

Delay

This is the interval (in ms) between each request in your collection run.

https://www.getpostman.com/docs/postman/collection_runs/starting_a_collection_run

and if you are using newman

--delay-request [number] Specify a delay (in ms) between requests [number]

https://www.getpostman.com/docs/postman/collection_runs/command_line_integration_with_newman

查看更多
登录 后发表回答