Karma - Chrome failed 2 times (cannot start). Giving up

末鹿安然 提交于 2019-11-27 04:45:36

问题


I've been trying to run my tests using karma-chrome-launcher, but everytime I run my tests it throws this error:

INFO [launcher]: Starting browser Chrome ERROR [launcher]: Cannot start Chrome

INFO [launcher]: Trying to start Chrome again (1/2).
ERROR [launcher]: Cannot start Chrome

INFO [launcher]: Trying to start Chrome again (2/2).
ERROR [launcher]: Cannot start Chrome

ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.

Here's my karma.conf.js code:

// Karma configuration
// Generated on Mon Mar 23 2015 14:04:19 GMT-0300 (BRT)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: 'www',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
      'lib/ionic/js/angular/angular.js',
      'lib/ionic/js/angular/angular-animate.js',
      'lib/ionic/js/angular/angular-sanitize.js',

      '../node_modules/jasmine-core/lib/jasmine-core/jasmine.js',
      '../node_modules/mock-local-storage/lib/mock-localstorage.js',
      '../node_modules/angular-mocks/angular-mocks.js',
      //'../node_modules/requirejs/require.js',
      'lib/ionic/js/angular/angular-resource.js',
      'lib/ionic/js/angular-ui/angular-ui-router.js',
      'lib/ionic/js/ionic.js',
      'lib/ionic/js/ionic-angular.js',
      /*'../tests/libs/ngCordovaMocks.min.js',*/
      'js/lib/ng-cordova.min.js',
      'js/*.js',
      'js/controllers/*.js',
      'js/services/*.js',
      'js/factory/*.js',
      //'../tests/*.js',
      '../tests/**/*.js'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress', 'html'],

    htmlReporter: {
      outputFile: '../tests/report/index.html'
    },


    // web server port
    port: 9876,

    plugins : [
      'karma-junit-reporter',
      'karma-jasmine',
      'karma-phantomjs-launcher',
      'karma-chrome-launcher'
      //'karma-htmlfile-reporter'
    ],

    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

I'm installing the module here: https://www.npmjs.com/package/karma-chrome-launcher

Thanks!


回答1:


I had the same problem and tried a lot of the suggested solutions I found, but what finally solved it for me was to delete the node_modules folder and getting everything new via npm install




回答2:


Had the same issue with my build environment.

What i did is to follow the advice of Rafael Cichocki to enable the debugging:

logLevel: config.LOG_DEBUG

Then tried to launch the chrome-browser with exactly the same line that was visible int he debug output.

Turned out that chrome browser was crashing due to missing ttf fonts. So running:

apt-get install ttf-freefont

Solved that issue for me and karma started to launch chrome.




回答3:


I noticed when I had this error that when I changed the spec file and saved it, it seemed to work again. I had a few errors in typescript that didn't break the tests (passing in null arguments to a virtual component instance constructor). I don't know if it was resolving the errors since they existed before when it was working, or if changing the file and saving it updated the cache.

So this could mean that clearing the cache in Chrome could also potentially resolve it. It's working now again for me so I can't check to verify.




回答4:


Solution for us with angular cli was setting the following properties in the karma.conf.js

 autoWatch: false,
 singleRun: true



回答5:


Just in case you are running this behind a corporate proxy. Make sure you include your 0.0.0.0 in your NO_PROXY Environment variable.

Otherwise your test will first go out through your firewall where it will most likely not be able to reach 0.0.0.0. So just to be sure I include the following in my

NO_PROXY=127.0.0.1,localhost,0.0.0.0

Especially if you are running your tests in a container environment (e.g. your build pipeline) non set environment variables might be a common reason for ng test working fine on your local machine but failing to connect to google-chrome in the container.




回答6:


I got my inspiration partially from here: https://stackoverflow.com/a/33802985/1534823

Also use logLevel: config.LOG_DEBUG - it can help you get good information on what is causing your error`

Check following settings in karma.conf:

captureTimeout: 60000,
browserNoActivityTimeout: 360000
browser: ["Firefox"]
  • captureTimeout - your browser may take some time to start. LOG_DEBUG should show some error related to capturing your browser
  • browserNoActivityTimeout - PhantomJS is really slow(x10) on my machine, in comparison to Firefox and Chrome. Karma may timeout before your tests complete.
  • browser - our jenkins server runs on linux, where we had no binaries for chrome, so we had to switch to firefox

If any of these three settings were not set correctly, we would get the error you described above.




回答7:


I was able to resolve this by remove the absolute path (src/examplePath) and changing it to a relative path (../../examplePath).

Example change in spec:

import { myPackage } from 'src/myPath'; (seems to be the issues)

import { myPackage } from '../../../myPath'; (seems to resolve it)

Note I had tried deleting the node modules and npm installing but that didn't work. I'm so not sure why this matters.




回答8:


I was also facing this issue. I made below three changes in my karma.config.js file.

 autoWatch: false,
 browsers: ['Chrome'],
 singleRun: false


来源:https://stackoverflow.com/questions/31966434/karma-chrome-failed-2-times-cannot-start-giving-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!