【问题标题】:zombie.js-triggered AJAX requests don't complete僵尸.js 触发的 AJAX 请求未完成
【发布时间】:2016-05-27 22:27:54
【问题描述】:

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

我有三个文件:

  • index.html: 僵尸加载的 HTML 页面
  • hello.txt:一个纯文本文件,内容为hello, world
  • main.js:一个加载index.html的zombie.js程序

index.htmlhello.txt 使用命令 python -m SimpleHTTPServer 8009 提供服务。 (请注意,我在 hosts 文件中使用的是 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>

这里是main.js。它导航到index.html 并尝试评估doRequest 函数,这应该会导致AJAX 请求。但是,请求永远不会完成,而是超时。

var Browser = require("zombie");
const browser = new Browser({debug: true});
const rootURL = "http://dev.local:8009/"

browser.visit(rootURL, function(err) {                                                                                                                                             
  browser.evaluate('console.log("browser.evaluate ✔");');
  browser.evaluate('doRequest("zombie");');
});

这是运行DEBUG=zombie node ./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 响应,但从未将事件提供给处理程序。

为什么僵尸发起的请求没有正确完成?

【问题讨论】:

    标签: javascript jquery node.js zombie.js


    【解决方案1】:

    我遇到了同样的问题,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()

    browser.visit(rootURL, function(err) {                                                                                                                                             
        browser.evaluate('console.log("browser.evaluate ✔");');
        browser.evaluate('doRequest("zombie");');
        browser.wait();
    });
    

    【讨论】:

      【解决方案2】:

      以上答案是正确的。使用wait,您可以获得ajaxed 的数据。通过创建一个接受浏览器作为参数的函数来适应另一个示例:

      async function getAjaxedData(url, browser) {
          return browser.visit(url).wait().then(() => {
              // Do stuff
              // e.g. browswer.queryAll(selector)
          });
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-07
        • 2012-03-11
        相关资源
        最近更新 更多