When testing my API the logic is that a task is being locked. During this time backend is doing some calculations (I don’t have control over it). When finished - an endpoint will return: isLocked: false, otherwise is returning all the time isLocked: true. I need to stop collection requests execution until task is no longer locked and I am using it few times within same collection.
I have created a custom function to handle such scenario which works in most of cases, but in some of them is not working - meaning postman / newman will wait until task is no longer locked, but then will skip next (subsequent) request which obviously will fail the tests.
const responseJson = pm.response.json();
var taskId = pm.environment.get("taskId")
function checkIfTaskIsUnlocked(responseJson) {
if (responseJson.items && responseJson.items[taskId]) {
if (responseJson.items[taskId].isLocked === false) {
return true;
} else {
console.log('Task is still locked');
return false;
}
} else {
console.error('Task ID not found in the response JSON');
return false;
}
}
if (!checkIfTaskIsUnlocked(responseJson)) {
console.log('Retrying the request in 10 seconds...');
setTimeout(function() {
pm.execution.setNextRequest(pm.info.requestName);
}, 10000);