0

I am writing a test for an Async GET request. This test needs to retry every 5 seconds for up to 120 seconds before it fails and moves on to the next request.

At the moment as a quick fix, I keep retrying without a limiter and that means it would keep retrying forever until it sees an expected outcome in the response.

Find my code below:

var jsonData = pm.response.json(); 
tests["Assert id is a string"] = typeof(jsonData.id) === "string";

if (jsonData._async.status !== "done" && 
globals.async_inv_byAddress == jsonData.id){
  postman.setNextRequest("E2E get_Async_current_request");
} else {
    pm.test("Async Test Passed");
    postman.setNextRequest("E2E post_Async_next_request")
}

The code above can lead to a runtime of hours without failing.

I need to find a way of failing the test if it cannot find the expected output within 120 seconds and it needs to retry after every 5 seconds

Jhanz
  • 127
  • 3
  • 3
  • 12

1 Answers1

2

You can try this:

var moment = require('moment');
var timeStamp = pm.globals.get("timeStamp");

if (!timeStamp) {
    timeStamp = moment().add(2, 'minutes');
    pm.globals.set("timeStamp", timeStamp);
}

if (moment() < moment(pm.globals.get("timeStamp"))) {
    console.log("retry");
} else {
    console.log("stop");
    postman.setNextRequest(null);
    pm.globals.unset("timeStamp");
}

To define your 120 second limit and add this

setTimeout(function(){}, 5000);

to you pre-request script to definie your delay of 5 seconds between each retry.

Combined with your code your tests would look like this:

var jsonData = pm.response.json();
var moment = require('moment');
var timeStamp = pm.globals.get("timeStamp");

if (!timeStamp) { //create new Timestamp (now + 120 seconds)
    timeStamp = moment().add(120, 'seconds');
    pm.globals.set("timeStamp", timeStamp);
}

tests["Assert id is a string"] = typeof(jsonData.id) === "string";

if (moment() < moment(pm.globals.get("timeStamp"))) {//request is within 120 seconds
    if (jsonData._async.status !== "done" &&
        globals.async_inv_byAddress == jsonData.id) {
        postman.setNextRequest("E2E get_Async_current_request");
    } else {
        pm.test("Async Test Passed");
        postman.setNextRequest("E2E post_Async_next_request");
    }
} else { //120 seconds exceeded
    console.log("Stopping tests. 120 seconds exceeded.");
    postman.setNextRequest(null);
    pm.globals.unset("timeStamp");
}

and your pre-request-script would look like this:

setTimeout(function(){}, 5000);

Alternatively you can set a request-delay for each request over the postman runner or newman in cli.

DieGraueEminenz
  • 830
  • 2
  • 8
  • 18