你可以试试这个:
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");
}
要定义您的 120 秒限制并添加此
setTimeout(function(){}, 5000);
给您预先请求脚本来定义每次重试之间 5 秒的延迟。
结合您的代码,您的测试将如下所示:
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");
}
并且您的预请求脚本如下所示:
setTimeout(function(){}, 5000);
或者,您可以在 cli 中通过 postman runner 或 newman 为每个请求设置请求延迟。