問題描述
簡短版:
如何啟動 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.
這篇關于啟動時打開 Karma debug.html 頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!