2016-02-16 82 views
1

我正在嘗試使用Zombie.js編寫單元測試。我的問題是,當使用browser.evaluate啓動AJAX請求時,即使它們在使用腳本標記包含在頁面上時執行得很好,AJAX請求仍未完成。zombie.js觸發的AJAX請求不完整

我有三個文件:

  • index.html:HTML頁面這殭屍負荷
  • hello.txt:它加載index.html
  • 一個zombie.js程序:與內容 hello, world
  • main.js一個純文本文件

index.html and hello.txt are s使用命令python -m SimpleHTTPServer 8009。 (請注意,我正在使用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並嘗試評估應該導致AJAX請求的doRequest函數。但是,請求永遠不會完成,而是超時。

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收到答覆,但事件從未被給予處理程序。

爲什麼殭屍發起的請求不能正確完成?

回答

0

我得到了同樣的問題與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(); 
});