ZombieJS和Qunit整合

时间:2012-07-04 07:43:04

标签: node.js testing qunit headless zombie.js

有没有人用ZombieJS集成QUnit测试?我有一个脚本,所以我想传递一个“tests.html”文件并进行轮询直到测试完成,然后读取结果。与我正在使用的PhantomJS类似,它的工作原理非常好。我最喜欢将Phantom和Zombie与性能进行比较。此外,我们已经创建了很多Qunit测试,因此我不想只是转储它们并在Zombie环境中从头开始重写所有内容(如果我决定首先使用它:)

我面临的问题是我的测试永远不会完成,因此Qunit始终处于运行状态。还没有详细调试任何东西,但只是想确保我没有遗漏一些明显的东西。

var Browser = require("zombie");
var assert = require("assert");

// Load the page from localhost
browser = new Browser();

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 100000, //< Default Max Timout is 3s
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function() {
            if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
                // If not time-out yet and condition not yet fulfilled
                condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
            } else {
                if(!condition) {
                    // If condition still not fulfilled (timeout but condition is 'false')
                    console.log("'waitFor()' timeout");
                    process.exit(1);
                } else {
                    // Condition fulfilled (timeout and/or condition is 'true')
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 100); //< repeat check every 250ms
};

browser.visit("tests.html", function () {

 waitFor(function(){
            return browser.evaluate(function(){
                var el = browser.document.getElementById('qunit-testresult');
                if (el && el.textContent.match('completed')) {
                    return true;
                }
                return false;
            });
        }, function(){
            var failedNum = browser.evaluate(function(){
                var el = browser.document.getElementById('qunit-testresult');
                console.log(el.textContent);
                try {
                    return el.getElementsByClassName('failed')[0].innerHTML;
                } catch (e) { }
                return 10000;
            });
            process.exit((parseInt(failedNum, 10) > 0) ? 1 : 0);
        });
});

0 个答案:

没有答案