启动时打开 Karma debug.html 页面

简短版:

如何启动 Karma 并让它在与 Karma 起始页相同的浏览器中自动打开 debug.html 文件?

How do I start up Karma and have it open the debug.html file automatically in the same browser as the Karma start page?

长版:

我不太喜欢使用控制台记者来记录 Karma,所以我一直在使用 karma-jasmine-html-reporter-livereload 输出到 Karma 的 localhost:9876/debug.html 文件.问题是,每次我开始调试会话时,我都必须单击 karma 打开的网页中的调试"按钮.

I'm not a huge fan of using the console reporters for Karma, so I have been using karma-jasmine-html-reporter-livereload which outputs to Karma's localhost:9876/debug.html file. The problem is, every time I start a debugging session, I have to click the 'debug' button in the web page that karma opens.

我想找到一种方法让 karma 通过 gulp 任务自动打开 debug.html 页面.我在多个浏览器中运行测试,因此我希望 debug.html 页面在 Karma 打开的每个浏览器中作为第二个选项卡打开.我还希望能够在业力关闭时关闭该 debug.html 选项卡.我尝试了很多东西,但没有成功.

I would like to find a way to have karma open the debug.html page automatically through a gulp task. I run the tests in multiple browsers, so I would like the debug.html page to open as a second tab in each of the browsers that Karma opens. I would also like to be able to close that debug.html tab when karma closes. I've tried a bunch of things, with no success.

这是我的 gulp 任务.监视"任务监视我的源 TypeScript 文件并将它们编译成 javascript……没什么特别的.

Here's my gulp task. The 'watch' task watches my source TypeScript files and compiles them into javascript...nothing fancy.

gulp.task('watch-test', ['watch'], function (done) {
    //start a livereload server so that the karma html 
    //reporter knows to reload whenever the scripts change
    livereload.listen(35729);
    gulp.watch('dist/src/**/*.js').on('change', livereload.changed);

    new KarmaServer({
        configFile: __dirname + '/karma.conf.js',
        singleRun: false
    }, done).start();
});

推荐答案

我找到了一种让它永久化的方法,虽然它并不完美.你可以在上下文中注入一个 javascript:

I found a way that makes it permanent although it is not perfect.. You can inject into the context a javascript:

files: [
    "test/init.js",
    ...
]

并将这段代码放入文件中:

and put inside the file this code:

(function (window) {
    if (!window.parent.initDone && window.location.pathname === '/context.html') {
         window.parent.initDone = true;
         window.open('/debug.html', '_blank');
    }
})(window)

这将确保该窗口仅在第一次运行测试时打开,并且仅在 context.html 中执行.

this will ensure that the window is open only the first time the tests are run and it will be executed only in context.html.

您可以在该块中添加任何您希望的初始化代码.

You can add any init code you wish inside that block.

相关文章