levelnis
levelnis

Reputation: 7705

Karma not running unit tests due to "No captured browser" message

I'm trying to set up Karma to run AngularJS unit tests using Jasmine, but I can't get the tests to run. I'm sure I'm overlooking something simple. I'm running this on a Windows 7 machine with Node.js installed and karma installed via npm.

My directory structure looks like this:

  • js/app/ - contains controllers, app, etc
  • js/config/ - contains karma.conf.js
  • js/lib/ - contains angular
  • js/test/ - contains jasmine specs

I'm starting a command prompt in the js directory and running this command:

karma start config/karma.conf.js

That causes Chrome to run on port 9876, but whenever I change any watched files and check the Karma output, I see this info message:

No captured browser, open http://localhost:9876/

Here's my config file:

module.exports = function(config) {
  config.set({
    basePath: '../',
    frameworks: ['jasmine'],
    files: [
      'lib/angular.js',
      'app/**/*.js',
      'test/**/*.js'
    ],
    exclude: [
    ],
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    captureTimeout: 60000,
    singleRun: false
  });
};

I'm using Angular 1.2.10 and Karma 0.10.9

Upvotes: 51

Views: 65402

Answers (14)

Trevor Karjanis
Trevor Karjanis

Reputation: 1724

As of Chrome 117.0.5938.89 and Karma 6.4.2 on Windows, deleting the following registry string value resolved the issue for me.

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome\UserDataDir

With the key, Chrome does not allow Karma to configure the user data directory. Chrome does not connect to Karma and does not close when Karma closes. Without the key, Karma opens a new Chrome window that connects and closes with Karma.

Alternatively, close all Chrome windows before running Karma. Yes, this is inconvenient.

Source

Upvotes: 0

splash
splash

Reputation: 13327

In my case the environment variable CHROME_BIN was set to a wrong path. Fix the value (might be defined in /etc/environment) or unset the environment variable temporarily with unset CHROME_BIN.

To find the cause of the problem it was helpful to set log level to debug in Karma's config file:

logLevel: config.LOG_DEBUG,

Upvotes: 5

Anton Rusak
Anton Rusak

Reputation: 936

I had the same message because there was no Chrome installed on my PC. Just Chromium. Well, it turns out that's not the same. Now after installing Chrome, both 'Chrome' and 'ChromeHeadless' browsers are able to start.

Otherwise you might specify 'Chromium' or 'ChromiumHeadless' in config and be fine with it.

TLDR: Make sure you have actually installed the browser you are testing in.

Upvotes: 2

Laszlo Sarvold
Laszlo Sarvold

Reputation: 963

I had the same problem and it was a bug in a test. Removing that code fixed the error.

Upvotes: 4

hoefling
hoefling

Reputation: 66191

Had the same error recently:

  • a project using angular-cli,
  • tests running on Jenkins in a headless environment.

Turns out I have cancelled a Jenkins job previously in the step where the tests were running, so ng test did not quit (had a handful of running ng processes, paired with [node] <defunct> ones, blocking the further test execution). Solution was to find and kill all hanging ng processes:

$ sudo -su jenkins
# ps aux | grep "ng$" | awk '{print $2}' | xargs kill -9

Upvotes: 1

Rahul Sharma
Rahul Sharma

Reputation: 337

I was facing the same issue and i resolved it by killing all the chrome instances from Task Manager. It seems that there were 'n' number of test runners running. When you close the karma test runner without stopping the server, it's instance is still alive.

So press 'alt + ctrl + delete', kill all sessions/instances of chrome and restart Karma.

It works!

Upvotes: 1

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.

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

Upvotes: 2

exp
exp

Reputation: 39

Installing karma-cli npm install -g karma-clisolved this issue for me. Hope this helps.

Upvotes: 1

AlignedDev
AlignedDev

Reputation: 8232

I had a face palm moment. karma start needs to be run in the same directory as your karma.conf.js file.

Upvotes: 14

user6385799
user6385799

Reputation:

I had the same problem and I did all what everyone from here said without any result. In my case I didn't do the init:

C:\Users>cd c:\xampp\htdocs\angularjs-pro\ch07

c:\xampp\htdocs\angularjs-pro\ch07>karma init

Which testing framework do you want to use ?
Press tab to list possible options. Enter to move to the next question.
> jasmine

Do you want to use Require.js ?
This will add Require.js plugin.
Press tab to list possible options. Enter to move to the next question.
> no

Do you want to capture any browsers automatically ?
Press tab to list possible options. Enter empty string to move to the next question.
> Chrome

The second time when I got in the same situation was when I tried to do karma start in the wrong folder, not where I put karma init.

In both cases after I fixed the problem I got in widows console:

22 11 2016 14:06:51.697:INFO [karma]: Karma v1.3.0 server started at http://localhost:9876/
22 11 2016 14:06:51.698:INFO [launcher]: Launching browser Chrome with unlimited concurrency
22 11 2016 14:06:51.711:INFO [launcher]: Starting browser Chrome
22 11 2016 14:06:53.936:INFO [Chrome 54.0.2840 (Windows 10 0.0.0)]: Connected on socket /#HnJUQrkRPqtGFa5ZAAAA with id 16058274

Upvotes: 1

MarkSh
MarkSh

Reputation: 149

I had the same problem, no code changes, just re-running tests that worked a month ago.

When I ran from webstorm, it seemed to be taking a long time to capture the browser, so I wondered if it was timing out.

So I tried setting browser to just Chrome (i was using phantomjs as well), and waiting. Then reset back to Chrome and PhantomJS, now it's fine.

Upvotes: 1

yndolok
yndolok

Reputation: 5607

This happened to me when I had an endless loop in my code. Commenting out (using xdescribe in jasmine) recent code I wrote got the tests running again.

Upvotes: 3

mcv
mcv

Reputation: 4429

This error can mean that the browser can't find the server. Check if you can reach the server at the URL it mentions. It could be a misconfigured port number, or even (as it was in my case), localhost being misconfigured. I suppose it could be the server is not running, or any of a dozen other problems.

Check if you can reach the server manually. If you can't, fix that.

Upvotes: 8

user1460043
user1460043

Reputation: 2481

I just had the same issue in a different setup (Linux, QUnit, Firefox), though. The problem disappeared after I killed all karma processes and did a fresh karma start.

Upvotes: 11

Related Questions