Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使噩梦强制超时_Javascript_Node.js_Typescript_Mocha.js_Nightmare - Fatal编程技术网

Javascript 如何使噩梦强制超时

Javascript 如何使噩梦强制超时,javascript,node.js,typescript,mocha.js,nightmare,Javascript,Node.js,Typescript,Mocha.js,Nightmare,正如标题所暗示的那样,我正试图强制使脚本超时,特别是当一个条件(返回done())不满足时 下面是一些代码: import * as Nightmare from "nightmare"; describe("Login Page", function() { this.timeout("30s"); let nightmare = null; beforeEach(() => { nightmare = new Nightmare({ show: true });

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

下面是一些代码:

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

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

您可以使用
Promise.race()
实现超时。我不知道您的测试代码,所以我将只显示在噩梦请求上给您一个超时的内部部分,您可以将其插入到您的测试框架中

// 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()
处理程序,那么这是因为噩梦请求失败或者您遇到了超时。您可以通过查看通过拒绝得到的错误对象来判断它是哪一个


注意,如中所述,噩梦中还内置了各种超时选项。您可能还会发现其中一个内置选项适合您超时的确切目的。

您需要向我们展示一些实际的噩梦代码,然后添加一些关于您希望超时的代码的解释,以便我们能够帮助您了解任何级别的细节。@jfriend00完成。我没有时间为了现在测试这个,如果您需要更新答案,我添加了测试代码。明天我会测试它,谢谢你的回答。这不会起作用,因为你正在将
。然后
。catch
从噩梦的范围(我需要一个
。catch
在它的范围内捕捉
。done()
来完成脚本)@AidanelGoste-Promise.race返回一个你可以使用的承诺。然后和。继续。这就是这里的重点。我没有看到任何范围问题。.then和.catch都会告诉你噩梦什么时候结束。问题是你在
.end()
中使用的逗号,因为promise.race在
超时()之后就结束了。我使用的是typescript,所以这可能是主要的issue@AidanelGoste-这个逗号是因为我们声明了一个由两个项组成的数组。其中一项是.end()的返回值,它是一个承诺。另一个是超时承诺。我们将此数组传递给Promise.race,它将为我们监视这两个承诺,并在上通知。然后和。从它返回的承诺中捕获。