如何让噩梦强行超时 [英] How to make nightmare forcefully timeout

查看:46
本文介绍了如何让噩梦强行超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所暗示的,我试图强制使我的脚本超时,特别是如果不满足条件(返回 done()).

As the title implies I am trying to make my script timeout forcefully, specifically if a condition (that returns done()) isn't met.

这是一些代码:

import * as Nightmare from "nightmare";

describe("Login Page", function() {
  this.timeout("30s");

  let nightmare = null;
  beforeEach(() => {
    nightmare = new Nightmare({ show: true });
  });

  let pageUrl;

  describe("give correct details", () => {
    it("should log-in, check for current page url", done => {
      nightmare
        .goto(www.example.com/log-in)
        .wait(5000)
        .type(".input[type='email']", "username")
        .type(".input[type='password']", "password")
        .click(".submit")
        .wait(3000)
        .url()
        .exists(".navbar")
        .then(function(result) {
          if (result) {
            done();
          } else {
            console.log("failure");
                // I want it to timeout here
          }
        })
        .catch(done);
    })
        .end()
        .then(url => {
          pageUrl = url;
          console.log(pageUrl);
        })
  });
});

如果我的代码中有任何其他错误,请随时告诉我.

If I have any other mistakes in my code feel free to let me know.

推荐答案

您可以使用 Promise.race() 来实现超时.我不知道你的测试代码,所以我只会展示让你在噩梦请求上超时的内部部分,你可以将它插入到你的测试框架中.

You can use Promise.race() to implement a timeout. I don't know your testing code so I'll show just the inner part that gives you a timeout on the nightmare request and you can insert that into your test framework.

// utility function that returns a rejected promise after a timeout time
function timeout(t, msg) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            reject(new Error(msg));
        }, t);
    });
}

Promise.race([
    nightmare
        .goto(www.example.com / log - in )
        .wait(5000)
        .type(".input[type='email']", "username")
        .type(".input[type='password']", "password")
        .click(".submit")
        .wait(3000)
        .url()
        .exists(".navbar")
        .end()
    , timeout(5000, "nightmare timeout")
]).then(result => {
    // process successful result here
}).catch(err => {
    // process error here (could be either nightmare error or timeout error)
});

这里的概念是您在噩梦请求的承诺和超时的承诺之间创建了一场竞赛.先解决或拒绝的一方获胜并导致承诺处理结束.如果 Promise.race(...).then() 处理程序触发,那是因为您的噩梦请求在超时之前完成.如果 Promise.race(...).catch() 处理程序触发,那是因为噩梦请求失败或者你超时了.您可以通过查看拒绝时获得的错误对象来判断它是哪个.

The concept here is that you create a race between the promise from your nightmare request and a promise from the timeout. Whichever one resolves or rejects first wins and causes the end of the promise processing. If the Promise.race(...).then() handler triggers, then it's because your nightmare request finished before the timeout. If the Promise.race(...).catch() handler fires, then it's because either the nightmare request failed or you hit the timeout. You can tell which it is by looking at the error object you get with the reject.

请注意,如文档此处所述.无论超时的确切目的是什么,您还可能会发现这些内置选项之一适合.

Note, there are also all sorts of timeout options built into nightmare as described in the doc here. You may also find one of those built-in options suits whatever the exact purpose of your timeout is.

这篇关于如何让噩梦强行超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆