覆盖date构造函数执行脚本

我试图使用黄瓜js自动化一些testing,一个是依赖于一个函数调用新的date()。

我试着执行脚本并重写date这样的。

const driver = (this.driver as ThenableWebDriver); await driver.get(`http://localhost:5000/`); await driver.executeScript(` var d = new Date(2012,0,20); Date = function(){return d;} `); 

如果我再次调用executeScript函数

 await driver.executeScript(` alert(new Date()) // returns the date i set `); 

新的date正确返回。 但是,当我模拟点击,并onClick做了一个新的date(),它获取本地date,而不是我设置的date。

任何小费? 提前致谢

[编辑]在这里看到这个: 如何覆盖量angular器testing新的date()

 var Date = (function() { var OldDate = Date; return function (){ return new OldDate(2012,0,20); } }()); 

但是还是没有运气。 executeScript在哪里运行?

重写构造函数Date

 await driver.executeScript(` var Date = function() { // new constructor return new window.Date.prototype.constructor(2013, 6, 14); } Date.prototype = window.Date.prototype; // inherit the methods from Date Date.now = Date; // override Date.now() Date.UTC = Date.prototype.constructor.UTC; // copy Date.UTC(...) Date.parse = Date.prototype.constructor.parse; // copy Date.parse(...) window.Date = Date; // override Date `);