我需要测试公告是否在 6 个月后过期。目前我正在成功测试该公告是否正确。
6 个月后,它必须从特定选项卡中删除。问题是:如何用量角器模拟未来 6 个月的系统时间?
我需要测试公告是否在 6 个月后过期。目前我正在成功测试该公告是否正确。
6 个月后,它必须从特定选项卡中删除。问题是:如何用量角器模拟未来 6 个月的系统时间?
换个角度想。
模拟您的应用发送的包含公告的 HTTP 响应,并使用protractor-http-mock
库设置所需的日期。也就是说,让前端认为有一个过期的公告。
Sinon.js
替代选项包括在or的帮助下模拟时间TimeShift.js
,但就我个人而言,我认为涉及这些工具会过于复杂。无论如何,请参阅:
如果您确实需要在使用量角器进行测试时操作浏览器的日期时间,我在这里整理了一篇文章:如何在量角器中更改浏览器日期。
我同意您在使用这种方法之前应该三思而后行,因为大多数时候模拟响应会得到您想要的
我们可以在 node-windows 的帮助下实现这一点。首先使用命令 npm i node-windows 安装 node-windows ..
代码:
const win = require('node-windows')
const sys = require('util')
let dateTime = new Date("10-10-2007) //Convert string or number to date
let day = dateTime.getDate()
let month = dateTime.getUTCMonth() + 1
let year = dateTime.getFullYear()
let updateD = `${month}-${day}-${year}` //Format the string correctly according to thesystme format
console.log(updateD)
//Add a callback function (this can be somewhere else)
function execCallback(error, stdout, stderr) {
if (error) {
console.log(error)
} else {
console.log(stdout)
}
}
console.log(" execute in command prompt now")
var exec = win.elevate(`cmd /c date ${updateD}`,undefined, execCallback);
console.log(" execution completed ")