简体   繁体   English

为什么Qunit无法赶上我运行的2个以上的测试?

[英]Why is Qunit not catching my 2+ tests being run?

I'm trying to get a sequence of tests to work in Qunit. 我正在尝试进行一系列测试以在Qunit中工作。 I'm working with JQM and am using their testsuite, which includes a $.mobile.testHelper object, which I'm adding methods to. 我正在与JQM一起使用,并且正在使用他们的测试套件,其中包括一个$.mobile.testHelper对象, $.mobile.testHelper对象添加方法。

Here is my code (with comments and logs): 这是我的代码(带有注释和日志):

// my test page is loaded inside an iframe
var frame = document.getElementsByTagName("iframe")[0];
var d = frame.contentDocument;
var w = frame.contentWindow;
var $i = w.$(d);
var $body = w.$("body");

// forcing $(body) as event target
$.testHelper.eventTarget = $body;

// sets "one" listener on "step" event and jumps to next method when triggered
$.testHelper.stepSequence = function (fns) {
    $.testHelper.eventSequence("step", fns);
};

// run a test
$.testHelper.runTest = function (command, condition) {
    console.log("RUNNING TEST...");
    ok(condition, command);
};

// try 10x if a condition is met, wait 1000ms in between
$.testHelper.countDown = function (arr, command) {
    var condition, is_done;
    var ticker = 0;
    var i = w.setInterval(function () {

    switch (command) {
        case "verifyAttribute":
            condition = $i.find(arr[0]).eq(0).attr(arr[1]).indexOf(arr[2]) > -1;
            break;
        case "waitForElementPresent":
            condition = $i.find(arr[0]).length > 0;
            break;
        }
        if (condition) {
            console.log("CONDITION PASSED, RUN TEST");
            $.testHelper.runTest(command, condition);
            w.clearInterval(i);
        }
        ticker += 1;
        console.log(ticker);
        if (ticker === 10) {
            console.log("FAILED, RUN WITH undefined to fail test");
            $.testHelper.runTest(command, condition);
            w.clearInterval(i);
        }
    }, 1000);
};

// my test module
module("UI Basic Interaction");
asyncTest("${base_url}", function () {
    expect(2);

    // here is my sequence of methods
    $.testHelper.stepSequence([
        function () {
            w.setTimeout(function () {
                $body.trigger("step");
            }, 800);
            $.testHelper.countDown(["div#global-panel", undefined, undefined],         "waitForElementPresent");
        },
        function () {
            w.setTimeout(function () {
                $body.trigger("step");
            }, 800);
            $("a:contains('Menu')").trigger("click");
        },
        function () {
            w.setTimeout(function () {
                $body.trigger("step");
            }, 800);
            $.testHelper.countDown(["div#global-panel", "class", "ui-panel-open"], "verifyAttribute");
        },
        function () {
            w.setTimeout(function () {
                $body.trigger("step");
            }, 800);
            $("h1:contains('My Account')").trigger("click");
        },
        function () {
            start();
        }
    ])
});

I need to trigger "step" AFTER the test conditions runs, but can't get it to work, so I'm using the no-nice setTimeout 测试条件运行后,我需要触发“步骤”,但无法使其正常工作,所以我使用的是不错的setTimeout

My problem is that the first test passes, the 2nd test correctly starts the interval, while the UI renders, but when the element is found, QUNIT errors out with Expected 2 assertions, but 1 were run pretty much at the same time my console reports the condition to be true. 我的问题是,第一个测试通过了,第二个测试正确地开始了间隔,同时UI呈现了,但是当找到元素时,QUNIT错误出现了Expected 2 assertions, but 1 were run在我的控制台报告的同时几乎Expected 2 assertions, but 1 were runExpected 2 assertions, but 1 were run条件为真。

Question: 题:
From the code above, is there an error in my test routine, that does not run runTest "fast" enough, because Qunit errors out? 从上面的代码中,我的测试例程中是否有错误,因为Qunit错误出来,它不能足够快地运行runTest Also I would be happy for a nicer way to triggering "step" . 我也很高兴以更好的方式触发"step"

Thanks! 谢谢!

Ok. 好。 After much meddling: 经过很多干预:

What was wrong: 什么问题:

  • My click selector was $(element:contains(...) which searched the document vs the iframe $i.find("eleme... fixed this. 我的点击选择器是$(element:contains(...) ,它搜索了文档,而iframe是$i.find("eleme...修复了这个问题。
  • I added a 2nd listener for test_runner , which triggers once a test runs. 我为test_runner添加了第二个侦听 ,该侦听器将在测试运行时触发。 Only after all tests are run, I trigger start() . 仅在运行所有测试之后,我才会触发start() This way Qunit has to wait :-) 这样Qunit必须等待:-)

and the working code (see comments (1), (2), (3) for changes): 和工作代码(有关更改,请参见注释(1),(2),(3)):

var frame = document.getElementsByTagName("iframe")[0];
var d = frame.contentDocument;
var w = frame.contentWindow;
var $i = w.$(d);

// (1) set counter for tests ran 
// This allows to trigger start() after all tests are done
var test_log = 0;
var $body = w.$("body");

$.testHelper.eventTarget = $body;
$.testHelper.stepSequence = function (fns) {
    $.testHelper.eventSequence("step", fns);
};
$.testHelper.runTest = function (command, condition) {
    ok(condition, command);
    $body.trigger("step");
    // (2) When running a test, also trigger a runner on body to log no of tests
    $body.trigger("test_runner");
};
$.testHelper.countDown = function (arr, command) {
    var condition, is_done;
    var ticker = 0;
    var i = w.setInterval(function () {
        switch (command) {
        case "verifyAttribute":
            condition = $i.find(arr[0]).eq(0).attr(arr[1]).indexOf(arr[2]) > -1;
            break;
        case "waitForElementPresent":
            condition = $i.find(arr[0]).length > 0;
            break;
        }
        if (condition) {
            console.log("PASSED TEST");
            $.testHelper.runTest(command, condition);
            w.clearInterval(i);
        }
        ticker += 1;
        if (ticker === 10) {
            console.log("FAILED");
            $.testHelper.runTest(command, condition);
            w.clearInterval(i);
        }
    }, 1000);
};

module("UI Basic Interaction");
asyncTest("${base_url}", function () {
    expect(2);
    $.testHelper.stepSequence([
        function () {
        // (3) set a listener for tests ran
        // once all tests are done, start()
            $body.on("test_runner", function (e) {
                test_log += 1;
                if (test_log === 2) {
                    start();
                }
            });
            $body.trigger("step");
        },
        function () {
            $.testHelper.countDown(
                ["div#global-panel", undefined, undefined], 
                "waitForElementPresent"
            );
        },
        function () {
            $i.find("a:contains('Menu')").trigger("click");
            $.testHelper.countDown(
                ["div#global-panel", "class", "ui-panel-open"], 
                "verifyAttribute"
            );
        },
        function () {
            $i.find("h1:contains('My Account')").trigger("click");
        }
    ])
});

Viola. 中提琴。 Works nicely. 效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM