66

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.

RoundOutTooSoon
  • 9,821
  • 8
  • 35
  • 52

9 Answers9

84

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 Scripts it will be executed before request is send.

Example:

setTimeout(function(){}, 1000);
Krithick S
  • 408
  • 3
  • 15
Ivo
  • 941
  • 6
  • 3
  • 1
    Thanks! This worked great for me in Postman 6.5 and I found [a reference that says it works all the way back to Postman 5.2](https://github.com/postmanlabs/postman-app-support/issues/1038#issuecomment-328503581) too. – Jesse Webb Nov 20 '18 at 16:47
  • 3
    Thanks for the help! Minor comment: 'pre-request tests' should be 'pre-request scripts' – Nikita Jerschow Dec 10 '19 at 06:34
  • 14
    To be clear on the syntax, the brackets should not be there when you insert a number: for example `setTimeout(function(){}, 1500);` – AdrienW Sep 24 '20 at 14:03
  • I'm getting timeouts when I use this in the pre-request + when I run the call manually, it runs for a full minute. Any workarounds/enhancements? I need a minute between 3 requests, specifically. Thanks. – Kreidol Dec 16 '21 at 22:38
14

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].

Siyu
  • 11,187
  • 4
  • 43
  • 55
13

Try something like this-

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

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

  postman.setNextRequest("nextAPI");

}
Mukul Bansal
  • 878
  • 8
  • 10
  • What goes in place for the nextAPI? The name of the request file? – Naman Jain Apr 15 '20 at 00:46
  • I suggest logging before the timeout, with a noop like `()=>{}` inside it. Otherwise you won't see it till the timeout is over. – Noumenon May 28 '20 at 20:33
  • this solution is not working right now, please refer this latest solution -> https://stackoverflow.com/a/66989928/1263512 – hendra Jun 01 '23 at 07:54
11

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.

Netham
  • 1,168
  • 6
  • 16
  • 2
    Thanks for your answer. Method 1 is what I've been doing actually. But I only wanted to put a pause after only *one* request instead of after every request. Looks like I'm going with your method 2. Postman should add this feature. Runscope has it so I guess I am spoiled. – RoundOutTooSoon Nov 08 '16 at 06:33
  • Method II will not work if you set delay more then 30s – Icet Sep 29 '17 at 08:19
  • 1
    https://postman-echo.com/delay/ put this request between two request that you want to execute with delay – Pratik Charwad Dec 01 '18 at 15:32
11

Just simple example, I'm sure you will be understand.

setTimeout(() => {}, 15000); 

15000 it's a value in miliseconds

Ivan Bezrukavyi
  • 111
  • 1
  • 2
4

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

Community
  • 1
  • 1
workabyte
  • 3,496
  • 2
  • 27
  • 35
2

If you have the standalone Postman App (supports ES7) and you are intending to test only on Postman, not on newman(which doesn't support ES7), then you can have something like this in the Pre-Request script of the Request you want to delay:

function foo() {
    return (new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("done!");   // passing argument is optional, can just use resolve()
        }, 10000)   // specify the delay time in ms here..
    }))
}

async function waitForMe() {
    await foo().then((val) => {
        console.log(val);  // not required, you can just do an await without then
    })
}

waitForMe();
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
1

I prefer to use an online service Postman Echo's delay endpoint (docs are here). Simply create a request that uses this service and call it between the two other requests you wish to add a delay between.

If you want to check the status of something before continuing, you can use postman.setNextRequest() in the Tests of a request to loop until something has been completed. Simply do the following:

1) Create a collection structured as:

  • Delay For 10 Seconds
  • Status Check
  • Continue Processing

2) In the Status Check request Tests:

if(responseBody.has("Success")) //or any other success condition
{
    postman.setNextRequest('Continue Processing');
    tests["Success found"] = true;
}
else
{
    postman.setNextRequest('Delay For 10 Seconds');
    tests["No success found"] = true;
}
Nathan M
  • 11
  • 2
0

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;

        });
    }
Amit
  • 438
  • 5
  • 5