问题
I've an app that changes it's behaviour with the date. On my mock tests I need to override the new Date()
function to test all the scenarios. Does anyone know how to override it?
I already tried to use the executeScript to change the return value but it doesn't work.
browser.driver.executeScript('' +
'Date = function(){return new Date(2012, 0, 20)};'
);
回答1:
The replacement function depends on the original Date that's been replaced, so keep a reference to it in a closure, e.g.:
var Date = (function() {
var OldDate = Date;
return function (){
return new OldDate(2012,0,20);
}
}());
alert(new Date());
来源:https://stackoverflow.com/questions/36432070/how-to-override-new-date-on-protractor-tests