Postman runner - fix "failed" counter on doing retries

Hi,

I built a retry-mechanism like this:

// track success of each check
let checksStarted = 0;
let checksEnded = 0;

pm.test("Status code is 200", function () {
  ++checksStarted;
  pm.response.to.have.status(200);
  ++checksEnded;
});

var jsonData = JSON.parse(responseBody || '{}'); 
pm.test("Status VALIDATION_FAILED is shown", function () {
  ++checksStarted;
  pm.expect(jsonData.order.status).to.equal("VALIDATION_FAILED");
  ++checksEnded;
});

// retry up to 4 times - means 5 requests in total
let attemptsLeft = pm.globals.has("attempts_left") && pm.globals.get("attempts_left") || 5;
if ((checksStarted != checksEnded) && (attemptsLeft > 1)) {
  postman.setNextRequest(pm.info.requestName);
  console.log(pm.test.index(), pm.globals.toJSON());
  pm.globals.set("attempts_left", attemptsLeft - 1);
  setTimeout(function(){}, [3000]);
} else {
  postman.setNextRequest(null);
  pm.globals.unset("attempts_left");
}

I works fine. Unfortunately, the “failed” counter increases on every attempt due to my test-clauses. Is there a wait to read the “passed” and “fixed” counter in the beginning and reset it once I decide to retry to request?

I test this using the Postman runner in my local environment but I use newman. I hope there is a solution which works for both of them - or at least for newman.

Thanks,
Stephan

1 Like