3

有没有人将 QUnit 测试与 ZombieJS 集成?我有一个脚本,所以我想传递一个“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);
        });
});
4

0 回答 0