DZone
DevOps Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > DevOps Zone > How to Run Karma Tests in a Docker Container

How to Run Karma Tests in a Docker Container

How do you launch a browser in a Docker container that has no graphic acceleration? PhantomJS can be a viable solution to this problem.

Abdelghani Tassi user avatar by
Abdelghani Tassi
·
Dec. 31, 16 · DevOps Zone · Tutorial
Like (3)
Save
Tweet
14.08K Views

Join the DZone community and get the full member experience.

Join For Free

Last week, I started developing a CI platform for my company using Karma and Jasmine as testing frameworks.

Our software solutions are built in different Docker containers and Karma runs tests in a browser. How do you launch a browser in a Docker container that has no graphic acceleration?

Here's the answer.

If your JavaScript code does not contain any ES5 or ES6 features, you can use PhantomJS as a testing browser.

PhantomJS runs as a background task with no graphical interface.

// karma.conf.js
module.exports = function(config) {
  config.set({
    browsers: ['PhantomJS', 'PhantomJS_custom'],

    // you can define custom flags
    customLaunchers: {
      'PhantomJS_custom': {
        base: 'PhantomJS',
        options: {
          windowName: 'my-window',
          settings: {
            webSecurityEnabled: false
          },
        },
        flags: ['--load-images=true'],
        debug: true
      }
    },

    phantomjsLauncher: {
      // Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
      exitOnResourceError: true
    }
  })
}

If you are using ES5 or ES6 (which is very likely), then you should use a real browser like Chrome or Firefox. These browsers need graphical acceleration, which is not available in a Docker container.

The trick is to launch the X virtual frame buffer (Xvfb), which emulates an X11 display so that the browser can execute its GUI code

Here's an example in a Centos Docker container:

yum -y install Xvfb libXfont Xorg chromium #install X* packages and chromium
/usr/bin/Xvfb :99 & #run Xvfb in background
export DISPLAY=:99.0 #export the display environment var

Your Karma config file should look like this:

module.exports = function(config) {
  config.set({
    browsers: ['Chrome'],
    customLaunchers: {
      Chrome_without_sandbox: {
        base: 'Chrome',
        flags: ['--no-sandbox'] // with sandbox it fails under Docker
      }
    },
    ...
  });
};
Docker (software)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Python Class Attribute: Class Attribute vs. Instance Attribute
  • NextJS Vs React: Key Differences, Advantages and Limitations
  • Revoking Access to JWTs With a Blacklist/Deny List
  • MACH Architecture Explained

Comments

DevOps Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo