0
0
Postmantesting~10 mins

Delay between requests in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if a delay is correctly applied between two API requests in Postman. It verifies that the second request waits for the specified time before sending using a busy-wait loop in the pre-request script.

Test Code - Postman
Postman
pm.test("Delay between requests", function () {
    const startTime = new Date().getTime();
    pm.environment.set("startTime", startTime);
});

// In the second request's Pre-request Script:
const startTime = parseInt(pm.environment.get("startTime"));
const delay = 3000; // 3 seconds delay
const targetTime = startTime + delay;
console.log(`Target send time: ${targetTime}`);

while (new Date().getTime() < targetTime) {
    // Busy-wait loop to delay request sending
}
console.log(`Delay applied. Sending request after at least ${delay} ms.`);

// In the second request's Tests script:
pm.test("Request sent after delay", function () {
    const responseTime = new Date().getTime();
    const startTime = parseInt(pm.environment.get("startTime"));
    const delay = 3000;
    pm.expect(responseTime - startTime).to.be.at.least(delay);
});
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1First request's Tests script records current time in environment variable 'startTime'.Postman environment variable 'startTime' set to timestamp after first request completes.-PASS
2Second request's Pre-request Script reads 'startTime' and calculates targetTime = startTime + 3000ms.Pre-request script prepares for busy-wait delay.-PASS
3Script enters while loop, busy-waiting until current time >= targetTime.Request sending blocked until exactly 3 seconds from startTime.-PASS
4After busy-wait completes, second request is sent.Request sent to API endpoint after enforced delay.-PASS
5Second request's Tests script verifies total time elapsed.Test confirms at least 3000ms passed since startTime (includes delay + response time).Assert that (responseTime - startTime) >= 3000 ms.PASS
Failure Scenario
Failing Condition: The delay is not applied or is shorter than 3000 ms between requests (e.g., no busy-wait or incorrect logic).
Execution Trace Quiz - 3 Questions
Test your understanding
What does the first request do to help enforce the delay?
AIt sends two requests at once.
BIt waits for 3 seconds before sending.
CIt saves the current time in an environment variable.
DIt disables the delay for the second request.
Key Result
Using environment variables and a busy-wait loop in Pre-request Scripts enables precise delays between sequential requests in Postman Collection Runner. Note: Busy-wait consumes CPU; suitable for short test delays. For production, consider runner-level delays.