2013-10-05 30 views
20

我正在評估phantom.jszombie.js。我期望這種折衷是因爲幻影擁有更廣泛的文檔支持(因爲它使用真實的渲染器),而殭屍更快(因爲不使用渲染引擎)。不過殭屍在我的測試中似乎慢得多。這有意義嗎? ())(我使用casperjs),讓我繼續而不用等待,直到幻像返回(包括運行所有腳本和加載css)之前立即返回整頁。phantom.js vs zombie.js的表現

Phantom.js

casper.userAgent("Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13"); 

casper.test.begin('bing search', 2, function(test) { 
    casper.start('http://www.bing.com/', function() { 
     this.waitUntilVisible('#sb_form_q', function() { 
      this.sendKeys('#sb_form_q', "book", true); 
      this.click('#sb_form_go'); 
      this.waitUntilVisible('#count', function() {   
       var val = this.evaluate(function() { 
        return document.getElementById('count').innerText 
       }); 

       console.log(val) 
      }); 
     }); 
    }).run(function() { 
     test.done(); 
    }); 
}); 

Zombie.js

var Browser = require("zombie"); 
var browser = new Browser() 

browser.userAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13" 

browser.visit("http://www.bing.com/", function() { 
    browser.fill("#sb_form_q", "book"); 
    browser.pressButton("#sb_form_go"); 

    function resultArrived(window) { 
     return window.document.querySelector("#count") 
    } 

    browser.wait(resultArrived, function() { 
     console.log(browser.document.querySelector("#count").innerHTML)    
    }); 
}); 

回答

12

我不知道你爲什麼不利用殭屍的承諾語法(如你做卡斯帕)?你應該做這樣的事情:

browser.fill(...) 
    .then(browser.pressButton) 
    .then(something else) 

不使用承諾語法可能導致各種各樣的奇怪的效果,因爲執行中的異步API的順序是從你習慣於在腳本語言的自上而下的代碼不同。

對於你的問題,我不能完全確定,但從我的經驗來看,zombie.js和capser.js(在phantom.js之上)在速度上非常相似。另請注意,zombie.js文檔聲明:

要等待頁面完全加載和處理事件,請傳遞一個回調函數。

既然你確實傳遞了一個回調函數,你就會得到你所期待的 - 等待整頁加載。