如何使用量角器更改计算机设备的日期和时间

How to change the date and time of my computer device, using protractor

我需要测试公告是否在 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 ")