timestamp - 如何验证邮递员范围内的时间戳?
问题描述
我有 setTimeout(function(){}, [8000]); 在预请求脚本中
GET 响应,时间戳在 10 秒内变化
{"deviceId": null,
"deviceName": null,
"timestamp": "2021-04-08T22:28:26Z"
}
我写的测试用例
var current_timestamp = new Date();
current_timestamp.setSeconds(current_timestamp.getSeconds() - 9);
var current_timestamp1 = new Date();
current_timestamp1.setSeconds(current_timestamp1.getSeconds() - 8);
var current_timestamp3 = new Date();
current_timestamp3.setSeconds(current_timestamp3.getSeconds() - 15);
pm.test("Retrieve currentTimeStamp ", function(){ pm.expect(jsonData.items[0].timestamp).to.be.oneOf([current_timestamp.toISOString().split('.')[0] + 'Z',current_timestamp1.toISOString().split('.')[0] + 'Z']);
)];
上面的代码有效,但我想要
10 秒范围内的东西,因为响应时间更长我无法继续在数组中添加项目,所以我尝试了
var jsonData = pm.response.json();
var responseTime = jsonData.items[0].timestamp;
tests["Response time is acceptable"] = _.inRange(responseTime, current_timestamp1.toISOString().split('.')[0], current_timestamp3.toISOString().split('.')[0]);
这不起作用。有人可以帮忙吗?
解决方案
let moment = require('moment');
pm.test("Check the timestamp range", function () {
pm.expect(moment( "2021-04-08T22:28:26Z").isBetween("2021-02-08T22:28:26Z", "2021-04-08T22:28:27Z")).to.be.true;
});
您可以在脚本沙箱中使用moment库。
推荐阅读
- javascript - React Router 4 通过 Route 渲染传递道具不起作用
- node.js - 切换节点版本给我“无法从 npm 读取未定义的属性“解析”
- ios - 如何从 iOS 应用安全地向自己发送电子邮件?
- vue.js - Nuxtjs/Vuejs/Laravel 5.7 API:动作/突变后的组件反应
- vuejs2 - 路由器推送后在 asyncData 中获取路由参数
- javascript - 在 Javascript 中正确链接 ajax 请求
- python - 如何使用 python API 保存/显示 giphy gif?
- codeceptjs - CodeceptJS locator.stringify 在使用 Appium 运行 codecept 时不是函数错误
- python - 为我的 web-scraper 传递循环中的错误
- javascript - 打字稿(类型“未定义”不可分配给类型)为什么我的数组有一个 | 不明确的?