SupimpaAllTheWay
SupimpaAllTheWay

Reputation: 1478

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

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!

Upvotes: 45

Views: 61708

Answers (16)

Ayushya
Ayushya

Reputation: 1920

I faced a similar issue recently.

And found two solutions to fix this problem.

  1. I installed puppeteer and added process.env.CHROME_BIN = require('puppeteer').executablePath() at the top of my karma.conf.js file, as documented here.
  2. I uninstalled Google Chrome and cleared up all the system folders (%Local Appdata%\Google and also from Program Files / Program Files x86), restarted by the system, and then installed Chrome x64 from the official site.

I was happy with the 1st solution as well, but since I wanted to fix the root problem, I went ahead and found the second solution as well.

Hope this solves the problem for anyone facing this issue.

Upvotes: 1

Shane Rich
Shane Rich

Reputation: 83

ng test --watch=false

Using the watch flag set to false in combination with adjustment of the following parameters in karma.config.js worked for me:

browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
  ChromeHeadlessNoSandbox: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox']
  }
},

Upvotes: 0

Laser42
Laser42

Reputation: 754

If someone faces this error only in gitlab-runner (but in shell by hand ng test work ok), you can apply decision from here: https://forum.gitlab.com/t/running-karma-tests-with-chrome-and-gitlab-ci/14476

The decision is: in karma.config.js, replace the section

browsers: ['Chrome'],

by

browsers: ['ChromeHeadlessNoSandbox'],
customLaunchers: {
  ChromeHeadlessNoSandbox: {
    base: 'ChromeHeadless',
    flags: ['--no-sandbox']
  }
},

The reason of error is that Chrome doesn't support no-sandbox anymore

Upvotes: 5

Ryan
Ryan

Reputation: 11

What worked for me:

  • npm un karma-chrome-launcher

  • npm i karma-chrome-launcher

  • npm i -g karma-cli

  • karma init (and follow the prompts)

Upvotes: 0

Kurkula
Kurkula

Reputation: 6762

In Karma.conf.js, Increase timeouts to 60000

captureTimeout: 60000,
browserDisconnectTimeout: 60000,
browserDisconnectTolerance: 3,
browserNoActivityTimeout: 60000,

Upvotes: 6

Gerros
Gerros

Reputation: 768

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.

Upvotes: 29

Alex Steinberg
Alex Steinberg

Reputation: 1466

I experienced this after updating macOS to Catalina. I solved it by updating Puppeteer to the latest version.

Upvotes: 0

alezhu
alezhu

Reputation: 484

In Windows Chrome was install to %LOCALAPPDATA%/Google/Chrome/Application earlier. Now it's install to %PROGRAMFILES%/Google/Chrome/Application. If you is very long time with Chrome then your have old version in %LOCALAPPDATA%/Google/Chrome/Application.

Karma-launcher is searches Chrome location in order LOCALAPPDATA->PROGRAMFILES-> 'PROGRAMFILES(X86)' , first found old version and try to run it.

Just delete %LOCALAPPDATA%/Google/Chrome/Application folder

Upvotes: 1

rajat deshpande
rajat deshpande

Reputation: 31

browsers: ['PhantomJS'], Here allowed browser is PhantomJs, but code is trying for Chrome, which is not a specified browser in the karma.conf.js.

Change karma.conf.js file :

browsers: ['PhantomJS','Chrome', 'ChromeHeadless'],

chrome- is for opening new chrome browser window.

ChromeHeadless- is for running tests without opening browser window

make sure Chrome is installed and added to PATH

Hope this helps

Upvotes: 3

Himanshu Shekhar
Himanshu Shekhar

Reputation: 1262

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

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

Upvotes: 0

relief.melone
relief.melone

Reputation: 3322

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.

Upvotes: 2

Brian
Brian

Reputation: 242

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.

Upvotes: 1

Alexander
Alexander

Reputation: 3245

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

 autoWatch: false,
 singleRun: true

Upvotes: 0

David
David

Reputation: 21

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.

Upvotes: 2

Vladimir M
Vladimir M

Reputation: 4489

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.

Upvotes: 9

Rafael Emshoff
Rafael Emshoff

Reputation: 2739

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.

Upvotes: 1

Related Questions