How to override new Date() on Protractor tests

拜拜、爱过 提交于 2019-12-22 19:14:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!