3

I want to start Firefox using Xvfb to run Karma in a headless server.

Karma needs a launcher plugin to start and stop browsers automatically. I've found a karma-xvfb-chrome-launcher, but no one for Firefox.

After installing Xvfb (Ubuntu), I know I can start Firefox using the following command:

$ xvfb-run firefox <app-under-test-url> 

What I don't know is how to make Karma start Firefox this way.

Is there a way to provide a custom start/stop browser script to Karma work with?

Is possible to do it via Karma.conf.js?

How can I do that?

Thanks!

Fernando Costa
  • 669
  • 1
  • 9
  • 31

2 Answers2

8

You can use xvfb-run to start Karma, and any browser it launches will run in a new Xvfb instance.

In a Makefile of mine I have this command which is run when I want to run my Karma-based tests:

xvfb-run karma start --single-run

xvfb-run operates by starting an Xvfb instance, grabbing its display number and setting the DISPLAY environment variable, then it runs the command you passed as argument. This means that everything that is executing through xvfb-run (including any new processes started from the initial one) is using the DISPLAY value that xvfb-run has set and consequently will appear on the Xvfb instance that xvfb-run started.

Louis
  • 146,715
  • 28
  • 274
  • 320
0

Considering Louis's answer...

As I'm using Angular 2 with typscript, I've made it work adding the following lines to my package.json file:

"test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
"test-once": "tsc && karma start karma.conf.js --single-run",
"test-xvfb": "xvfb-run npm run test-once",

Now, when I run tests this way:

$ npm run test-xvfb

No browser window is open at all!

Community
  • 1
  • 1
Fernando Costa
  • 669
  • 1
  • 9
  • 31