检测 jasmine 测试何时完成

发布于 2024-11-06 14:16:12 字数 237 浏览 0 评论 0原文

我正在运行这样的 jasmine 测试;

   jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
   jasmine.getEnv().execute();

我想使用 JavaScript 检测测试何时完成。我该怎么办?

I am running jasmine tests like this;

   jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
   jasmine.getEnv().execute();

I would like to detect, using JavaScript, when the tests complete. How can I?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲影 2024-11-13 14:16:12

正如@Xv。建议添加一名记者即可。您可以做一些简单的事情:

jasmine.getEnv().addReporter({
    jasmineDone: function () {
        // the specs have finished!
    }
});

请参阅 http://jasmine.github.io/2.2/custom_reporter。 html

As @Xv. suggests, adding a reporter will work. You can do something as simple as:

jasmine.getEnv().addReporter({
    jasmineDone: function () {
        // the specs have finished!
    }
});

See http://jasmine.github.io/2.2/custom_reporter.html.

谷夏 2024-11-13 14:16:12

一些替代方法:

A) 使用 ConsoleRunner,接受 onComplete 选项。旧版本 (1.2rc1) 收到完整的回调作为独立参数。

由于您还提供了写入 (options.print) 的函数,因此您可以控制将测试报告写入控制台。

您可以让多个报告器同时处于活动状态 jasmineEnv.addReporter()

B) 还没有尝试过,但您可以创建自己的报告器,其中除了 jasmineDone()

C) 之外的每个公共方法都有空实现 检查作者保存的Jasmine google 群组中的旧帖子并覆盖 jasmine.getEnv().currentRunner().finishCallback:

    var oldCallback = jasmineEnv.currentRunner().finishCallback;
    jasmineEnv.currentRunner().finishCallback = function () {
        oldCallback.apply(this, arguments);
        $("body").append( "<div id='_test_complete_signal_'></div" );   
    };
    jasmineEnv.execute();

Some alternative ways:

A) Use the ConsoleRunner, that accepts an onComplete option. Older versions (1.2rc1) receive the complete callback as a standalone parameter.

Since you also supply the function that writes (options.print) you keep control about having the test reports written to the console.

You can have several reporters active at the same time jasmineEnv.addReporter().

B) Haven't tried, but you could create your own reporter, with empty implementations of every public method but jasmineDone()

C) Check an old post in the Jasmine google group, where the author saves and overrides jasmine.getEnv().currentRunner().finishCallback:

    var oldCallback = jasmineEnv.currentRunner().finishCallback;
    jasmineEnv.currentRunner().finishCallback = function () {
        oldCallback.apply(this, arguments);
        $("body").append( "<div id='_test_complete_signal_'></div" );   
    };
    jasmineEnv.execute();
花辞树 2024-11-13 14:16:12

我找到了两种不同的方法来解决这个问题。一种是破解 jasmine 在完成时抛出自定义事件。因为我想在加载测试后进行屏幕抓取,所以我将事件触发器插入到“reportRunnerResults”末尾的 jasmine-html.js 中

$( 'body' ).trigger( "jasmine:complete" );

然后这是监听事件的问题:

$( 'body' ).bind("jasmine:complete", function(e) { ... }

在我的例子中,我在 iFrame 中运行 jasmine并希望将结果传递到父窗口,因此我从第一个绑定触发父窗口中的一个事件:

$(window.parent).find('body').trigger("jasmine:complete");

也可以在没有 jquery 的情况下执行此操作。我的策略是轮询要添加到“完成时间”跨度的文本。在此示例中,我每 0.5 秒轮询一次,持续 8 秒。

var counter = 0;

function checkdone() {
    if ( $('#test-frame' ).contents().find('span.finished-at').text().length > 0) {
        ...
        clearInterval(timer);
    } else {
        counter += 500;
        if (counter > 8000) {
            ...
            clearInterval(timer);
        }
    }
}

var timer = setInterval( "checkdone()", 500 );

I found two different ways to solve this issue. One is to hack jasmine to throw a custom event when it completes. Because I wanted to screen scrape after the test loaded, I inserted the event trigger into jasmine-html.js at the end of "reportRunnerResults"

$( 'body' ).trigger( "jasmine:complete" );

Then it's a matter of listening for the event:

$( 'body' ).bind("jasmine:complete", function(e) { ... }

In my case, I was running jasmine in an iFrame and wanted to pass the results to a parent window, so I trigger an event in the parent from my first bind:

$(window.parent).find('body').trigger("jasmine:complete");

It is also possible to do this without jquery. My strategy was to poll for text to be added to the "finished-at" span. In this example I poll every .5 seconds for 8 seconds.

var counter = 0;

function checkdone() {
    if ( $('#test-frame' ).contents().find('span.finished-at').text().length > 0) {
        ...
        clearInterval(timer);
    } else {
        counter += 500;
        if (counter > 8000) {
            ...
            clearInterval(timer);
        }
    }
}

var timer = setInterval( "checkdone()", 500 );
ゝ偶尔ゞ 2024-11-13 14:16:12

我正在使用 HtmlReporter 运行 Jasmine 1.3.1。我最终像这样挂钩:

var orig_done = jasmineEnv.currentRunner_.finishCallback;
jasmineEnv.currentRunner_.finishCallback = function() {
    orig_done.call(this);
    // custom code here
};

I'm running Jasmine 1.3.1 with the HtmlReporter. I ended up hooking in like this:

var orig_done = jasmineEnv.currentRunner_.finishCallback;
jasmineEnv.currentRunner_.finishCallback = function() {
    orig_done.call(this);
    // custom code here
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文