23

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?

Community
  • 1
  • 1
Alice
  • 1,381
  • 3
  • 11
  • 18
  • Thanks! I wish my logs would mention ```Please set env variable ``` **CHROME_BIN**, like yours did (BTW, on Windows try "`C:\Program Files\Google\Chrome\Application\chrome.exe`", without quotes) – Top-Master Oct 25 '21 at 10:37

4 Answers4

33

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).

Daniel Bidulock
  • 2,344
  • 1
  • 26
  • 27
  • I find your answer more readable then Ludwig's : I like the fact that I can copy-paste the export command (and that it just works afterwards). You could maybe go one step further and explain how to automatically reload that export variable using a .bashrc for instance. – nha Nov 08 '14 at 22:14
  • 1
    If you are a newbie to karma (which is probably why you are reading this) it's helpful to know that karma with Chromium requires a graphics display. It conveniently hides the 'cannot open display' error message which would be trying to tell you that your DISPLAY environment variable is not set to use some running X Windows server. – nmgeek Mar 31 '16 at 17:02
10
  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.
Ludwig Magnusson
  • 13,964
  • 10
  • 38
  • 53
2

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

Gh111
  • 1,362
  • 18
  • 19
  • 1
    I'm about a year late, but I just came across this question this morning and this is exactly what I needed as I'm running Windows Subsystem for Linux ;) Thanks! – blueyodeler Jul 15 '20 at 12:56
1

When I set up a project with the yeoman generator angular today, I couldn't get chrome or phantomjs to work with karma either.

It turns out this is an issue specific to that project and the fix is simply adding the missing plugins to the package.json in your project root.

turtlemonvh
  • 9,149
  • 6
  • 47
  • 53