20

我正在评估phantom.jszombie.js。我希望权衡是幻影具有更广泛的文档支持(因为它使用真正的渲染器),而僵尸更快(因为没有使用渲染引擎)。然而,在我做的测试中,僵尸似乎要慢得多。这有意义吗?

我在想可能zombie会在visit()返回之前等待整个页面加载(包括运行所有脚本和加载css),而phantom在start()之后立即返回(我使用了casperjs)允许我继续而不等待整个页面。

幻影.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();
    });
});

僵尸.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)               
    });
});
4

1 回答 1

12

我不确定你为什么不使用僵尸的承诺语法(就像你使用 casper 一样)?您应该执行以下操作:

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

不使用 promises 语法可能会导致各种奇怪的效果,因为异步 api 中的执行顺序与您在脚本语言中习惯的自顶向下代码不同。

对于您的问题,我不能完全确定,但根据我的经验,zombie.js 和 capser.js(在 phantom.js 之上)在速度上非常相似。另请注意,zombie.js 文档指出:

要等待页面完全加载并处理事件,请通过访问回调函数。

由于您确实传递了回调,因此您会得到预期的结果 - 等待整页加载。

于 2013-12-14T20:32:48.310 回答