简体   繁体   English

如何使用nightwatch.js和sinon.js的假定时器?

[英]How to use Fake timers with nightwatch.js and sinon.js?

I'm doing JavaScript e2e test with nightwatch.js, and I want to mock the clock with sinon.js's fake timer http://sinonjs.org/docs/#clock 我正在使用nightwatch.js进行JavaScript e2e测试,我想用sinon.js的假计时器来模拟时钟http://sinonjs.org/docs/#clock

But test stops before finish it, I got the log like below, and doesn't progress anymore. 但测试在完成之前停止,我得到了如下的日志,并且不再进展了。

[some test] Test Suite
===============================
 ✔ Element <body> was visible after 5000 milliseconds. 

My test code is like below. 我的测试代码如下。 How can I solve the problem? 我该如何解决这个问题? Thank you. 谢谢。

module.exports = {
  before: function(browser) {
    clock = sinon.useFakeTimers(new Date(2015, 7, 20).getTime());
  },

  after: function(browser) {
    clock.restore();
    browser.end();
  },

  'some test': function(browser) {
    const browserName = this.client.options.desiredCapabilities.browserName;

    browser
      .url('some/path/')
      .waitForElementVisible('body', 5000)
      .pause(5000); // I need to use pause here

    clock.tick(5001);

    browser
      .expect.element('.someElement').to.be.enabled;
  },
};

The problem is that when you are using sinon fake clock, the clock is freezed and everything async doesn't work (they wait for you to advance the clock). 问题是当你使用sinon假时钟时,时钟被冻结,所有异步都不起作用(它们等你推进时钟)。 If you just want to use a fake Date, you can write it like this: 如果你只想使用假日期,你可以像这样写:

clock = sinon.useFakeTimers(new Date(2015, 7, 20).getTime(), "Date")

Or you can use lolex (more compact) like this 或者你可以像这样使用lolex(更紧凑)

clock = lolex.install(new Date(2015,7,20), ["Date"]);

BUT there is a problem with your code. 但是您的代码存在问题。 When you are replacing the clock with the fake clock in your test, you are just faking the clock in the test code, NOT in the browser. 当您在测试中使用假时钟替换时钟时,您只是在测试代码中伪造时钟,而不是在浏览器中伪造。 For the browser clock to be faked, the page you want to test must be loaded, and you have to inject lolex (or sinon) code into it, then trigger the faking. 对于要伪造的浏览器时钟,必须加载要测试的页面,并且必须将lolex(或sinon)代码注入其中,然后触发伪造。 This took me a few hours to finally figure it out and i have created a nigthwatch command using lolex to make this much easier. 这花了我几个小时才弄清楚,我用lolex创建了一个nigthwatch命令,使这更容易。 You can find the code here : 你可以在这里找到代码:

https://gist.github.com/vjau/9a8db88d5f1f82d4f9c02e82b29b466f https://gist.github.com/vjau/9a8db88d5f1f82d4f9c02e82b29b466f

The actual command is the file fakeDate.js 实际命令是文件fakeDate.js

I found the solution. 我找到了解决方案。 As Vincent J and his gist mentioned, I needed to inject lolex into the browser. 正如Vincent J和他的要点所提到的,我需要将lolex注入浏览器。

I used just lolex. 我只用了lolex。 I inject it to the browser like below. 我把它注入浏览器,如下所示。

if (__E2E__) {
  window.lolex = require('lolex');
}

And I execute it for setting the time. 我执行它来设置时间。

browser
  .execute(function () {
    const clock = window.lolex.createClock(new Date(2015, 7, 20, 19, 45));
    window.Date = clock.Date;
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM