zombie.js触发的AJAX请求没有完成
我试图用Zombie.js编写unit testing。 我的问题是,使用browser.evaluate
启动时,即使AJAX请求在包含在带有脚本标记的页面上时执行得很好,也不会完成。
我有三个文件:
-
index.html
:僵尸加载的HTML页面 -
hello.txt
:内容为hello, world
的纯文本文件 -
main.js
:加载index.html
的zombie.js程序
使用命令python -m SimpleHTTPServer 8009
来提供index.html
和hello.txt
。 (请注意,我正在使用dev.local
,这是我的hosts文件中的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
奇怪的是,僵尸似乎已经完成了请求:日志的最后一行显示收到了一个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()
browser.visit(rootURL, function(err) { browser.evaluate('console.log("browser.evaluate ✔");'); browser.evaluate('doRequest("zombie");'); browser.wait(); });