Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Karma to work with Chrome

I have a command-line-only Ubuntu 11.04 (GNU/Linux 2.6.35.4-rscloud x86_64) and I'm working through the Angular Phonecat tutorial which uses the Karma for testing. The Karma browser config page says Chrome launcher is "shipped with Karma by default."

But it's not working for me. This is the karma.conf.js. Below is the output.

 $ ./scripts/test.sh

Starting Karma Server (http://karma-runner.github.io)
-------------------------------------------------------------------
INFO [karma]: Karma v0.10.4 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
ERROR [launcher]: Cannot start Chrome
        Can not find the binary google-chrome
        Please set env variable CHROME_BIN

This answer points out that I need chromium.

So I tried to install Chrome on my Ubuntu with this guide with "download 64 bit version using command line". Everything went well until sudo apt-get -f install, which ended with a lot of "failed to fetch" from ubuntu IP addresses, such as 91.189.91.15 or 91.189.92.. Even with /usr/bin/googlesomething* that the "Can not find the binary google-chrome" goes away, I still got the "Cannot start Chrome" on the individual test level. Then while trying to fix errors, I deleted /usr/bin/googlesomething.

Currently, I have these node_modules:

angular-phonecat/node_modules$ ls
karma                      karma-html2js-preprocessor  karma-requirejs
karma-chrome-launcher      karma-jasmine               karma-script-launcher
karma-coffee-preprocessor  karma-junit-reporter
karma-firefox-launcher     karma-phantomjs-launcher

Q 1: Since my machine has only command-line access, no GUI, is the other Karma launcher, PhantomJS, a better choice than Chrome/Chromium?

Q 2: If I should still use Chrome/Chromium, should I get Chrome or Chromium?

Q 3: Does anyone know what exactly do I need to get google-chrome or chromium for Karma to work in the Angular app?

like image 761
Alice Avatar asked Oct 30 '13 01:10

Alice


People also ask

How do I run a karma test in Chrome?

To launch Chrome from karma, we need to use karma-chrome-launcher. Run the command npm install karma-chrome-launcher --save to install to the application. Add the karma-chrome-launcher plugin to the plugins list in your karma.

How do I run a karma test on my browser?

You need to run it with singleRun = false in karma. conf. js and then click the button on the top corner that says "DEBUG". Then you should see the output and it won't disappear or close.

How do I run a karma test without a browser?

Correct - Karma requires a browser to run. BUT - you can run Chrome in Headless mode, which means although you do need the browser installed, it will not open it's UI, and you can therefore run the tests purely through an SSH session for example.

Why headless Chrome?

The Headless mode is a feature which allows the execution of a full version of the Chrome Browser. It provides the ability to control Chrome via external programs. The headless mode can run on servers without the need for dedicated display or graphics.


3 Answers

I'll expand on Ludwig's third point for answer seekers and for my own reference in the future...

The first thing you need to do is find where chromium-browser is installed. Run:

which chromium-browser 

This will return the path to the executable, which will look something like this:

/usr/bin/chromium-browser 

Then simply set the path:

export CHROME_BIN=/usr/bin/chromium-browser 

Now Karma can find the browser it needs to execute your tests (assuming you've decided to use a graphical interface).

like image 153
Daniel Bidulock Avatar answered Oct 03 '22 16:10

Daniel Bidulock


  1. If you only have a command-line interface, PhantomJS is the only choice for you.
  2. In linux (at least 12.04 and forwards) chromium is the alternative. But you can't install it if you don't have a graphical ui.
  3. The clue is here: "Please set env variable CHROME_BIN". Karma tries to execute a file called google-chrome and it does not exist. The name of the executable for chrome varies from OS to OS. Therefore you need to set an enviroment variable called CHROME_BIN which has the value of the name of your chrome executable. On my system (Linux desktop 13.10) this is chromium-browser.
like image 37
Ludwig Magnusson Avatar answered Oct 03 '22 16:10

Ludwig Magnusson


My solution is to create an alias in linux to windws's chrome

alias launchchrome="\"/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe\""

and after that export that alias as linux env variable

export CHROME_BIN=launchchrome

That's it, now if you run ng test your windows chrome will be used! In my particular case this solution worked

like image 41
Gh111 Avatar answered Oct 03 '22 16:10

Gh111