Two small but life quality improvements from me.
- Small improvement to
You can use pm.environment.has
instead of that if( notโฆ).
Standalone function code
// HotFix Retry function
const statuscode = pm.collectionVariables.get("StatusCode")
if(pm.response.code != statuscode){
let retries = pm.collectionVariables.has("retries") && pm.collectionVariables.get("retries") || 0;
let attemptRetry = pm.collectionVariables.get("attemptRetry");
console.warn("retries used "+retries+" of " + attemptRetry);
// time is in ms
setTimeout(function(){}, 200);
if(retries < attemptRetry){ // Main condtion
postman.setNextRequest(pm.info.requestName);
pm.collectionVariables.set("retries", ++retries);
console.warn("Increase retries "+retries);
}
else{
// Reset variable
pm.collectionVariables.set("retries", 0);
console.warn("Reset retries. Failed after " + retries);
// OR remove it
//pm.variables.unset("retries");
postman.setNextRequest("Next_request");
}
}
else{
pm.collectionVariables.set("retries", 0);
console.warn("Reset retries on request Success "+ pm.collectionVariables.get("retries"));
postman.setNextRequest("Next_request");
}
Similar but with variables set in collection not in script.
- attemptRetry
- retries
- statusCode
I Use setNextRequest
as Newman sometimes stopped execution after retry ended.
- As a final approach you can Pack it as a global function:
Collection predefine helpers
pm.globals.set('helper', function loadhelper() {
let helper = {};
helper.retry= (statuscode, maxRetries, Next_request, sleep=200) => {
if(pm.response.code != statuscode){
let retries = pm.collectionVariables.has("retries") && pm.collectionVariables.get("retries") || 0;
console.warn("retries used "+retries+" of " + maxRetries);
// time is in ms
setTimeout(function(){}, sleep);
if(retries < maxRetries){ // Main condtion
postman.setNextRequest(pm.info.requestName);
pm.collectionVariables.set("retries", ++retries);
console.warn("Increase retries "+retries);
}
else{
// Reset variable
pm.collectionVariables.set("retries", 0);
console.warn("Reset retries. Failed after " + retries);
// OR remove it
//pm.variables.unset("retries");
postman.setNextRequest(Next_request);
}
}
else{
pm.collectionVariables.set("retries", 0);
console.warn("Reset retries on request Success "+ pm.collectionVariables.get("retries"));
postman.setNextRequest(Next_request);
}
};
//// and next Global helper function.
//// helper.testModelXYZ () => {};
return helper;
} + '; loadhelper();');
Two liner usage:
// Use helper function from directory
const helper = eval(pm.globals.get("helper"));
pm.test("Make some retry- Generally test name", helper.retry(200,3,"some next req", 5000));
You can store it in CollectionVariables but If you use version control mechanism it is annoying to see this variable changed in almost all PR.
Put this on some Make stuff
request number one in collection.
Or on the directory level of the collection but this may be expensive as this JS will be executed too frequently in my humble opinion. You can always wrap it with pm.environment.has
and skip if it is already available.
Predefined Helpers function is the way for me for large repeated part of JS code in Postman.
Take it and share it as it make retries as part of your โSwiss knife toolkitโ