How to override new Date() on Protractor tests

2019-05-19 06:02发布

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条回答
疯言疯语
2楼-- · 2019-05-19 06:44

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());

查看更多
登录 后发表回答