Javascript zombie.js触发的AJAX请求不';不完整

Javascript zombie.js触发的AJAX请求不';不完整,javascript,jquery,node.js,zombie.js,Javascript,Jquery,Node.js,Zombie.js,我正在尝试用Zombie.js编写单元测试。我的问题是,当使用浏览器启动AJAX请求时,AJAX请求没有完成。请评估,即使它们在包含脚本标记的页面上时执行良好 我有三个文件: index.html:僵尸加载的html页面 hello.txt:包含内容的纯文本文件hello,world main.js:加载index.html index.html和hello.txt使用命令python-msimplehttpserver 8009提供。(请注意,我使用的是dev.local,它是主机文件中1

我正在尝试用Zombie.js编写单元测试。我的问题是,当使用
浏览器启动AJAX请求时,AJAX请求没有完成。请评估
,即使它们在包含脚本标记的页面上时执行良好

我有三个文件:

  • index.html
    :僵尸加载的html页面
  • hello.txt
    :包含内容的纯文本文件
    hello,world
  • main.js
    :加载
    index.html
index.html
hello.txt
使用命令
python-msimplehttpserver 8009
提供。(请注意,我使用的是
dev.local
,它是主机文件中
127.0.0.1
的同义词)

这里是
index.html
。它发出一个AJAX请求来检索
hello.txt
。这很好:

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8' />
    <title></title>
</head>
<body>
  <script type='text/javascript' src='https://code.jquery.com/jquery-2.2.0.min.js'>
  </script>
  <script type='text/javascript'>
    $.support.cors = true;
    var doRequest = function(msg) {
      $.support.cors = true;
      return $.ajax({
        url: "http://dev.local:8009/hello.txt",
        success: function(v) { console.log("success ", msg, v); }
      });
    };
  </script>
  <script type="text/javascript">
    console.log("page loaded");                                                                                                                                                    
    doRequest('script');
  </script>
</body>
</html>
下面是运行
DEBUG=zombie节点的日志。/main.js

  zombie Opened window http://dev.local:8009/  +0ms
  zombie GET http://dev.local:8009/ => 200 +38ms
  zombie Loaded document http://dev.local:8009/ +36ms
  zombie GET https://code.jquery.com/jquery-2.2.0.min.js => 200 +55ms
page loaded
  zombie XHR readystatechange http://dev.local:8009/hello.txt +64ms
  zombie XHR loadstart http://dev.local:8009/hello.txt +1ms
  zombie GET http://dev.local:8009/hello.txt => 200 +9ms
  zombie XHR readystatechange http://dev.local:8009/hello.txt +1ms
  zombie XHR readystatechange http://dev.local:8009/hello.txt +0ms
  zombie XHR readystatechange http://dev.local:8009/hello.txt +1ms
  zombie XHR progress http://dev.local:8009/hello.txt +0ms
success  script hello, world

  zombie XHR load http://dev.local:8009/hello.txt +2ms
  zombie XHR loadend http://dev.local:8009/hello.txt +0ms
  zombie Fired setTimeout after 0ms delay +1ms
  zombie Event loop is empty +0ms
browser.evaluate ✔
  zombie XHR readystatechange http://dev.local:8009/hello.txt +4ms
  zombie XHR loadstart http://dev.local:8009/hello.txt +0ms
  zombie GET http://dev.local:8009/hello.txt => 200 +6ms
奇怪的是,zombie似乎已经完成了请求:日志的最后一行显示收到了HTTP
200
响应,但事件从未发送给处理程序


为什么僵尸启动的请求没有正确完成?

我在zombie.js中遇到了同样的问题,因此我查看了源代码,发现了以下几行:

// -- Event processing --

// Grabs next event from the queue, processes it and notifies all listeners.
// Keeps processing until the queue is empty or all listeners are gone. You
// only need to bootstrap this when you suspect it's not recursing.
//... more lines ...
// Is there anybody out there?
if (this.waiting === 0)
    return;
因此,如果无人等待,则不会触发任何事件。您需要添加
browser.wait()


以上答案是正确的。使用
等待
,您可以获取ajaxed数据。这是另一个通过创建接受浏览器作为参数的函数进行调整的示例:

async function getAjaxedData(url, browser) {
    return browser.visit(url).wait().then(() => {
        // Do stuff
        // e.g. browswer.queryAll(selector)
    });
}
browser.visit(rootURL, function(err) {                                                                                                                                             
    browser.evaluate('console.log("browser.evaluate ✔");');
    browser.evaluate('doRequest("zombie");');
    browser.wait();
});
async function getAjaxedData(url, browser) {
    return browser.visit(url).wait().then(() => {
        // Do stuff
        // e.g. browswer.queryAll(selector)
    });
}