karma-chrome-launcher: ChromeHeadless can't run as root with --no-sandbox anymore

After updating to Chrome 62 and Chromedriver 2.33, I get the following error when running karma tests with ChromeHeadless:

  | 25 10 2017 08:47:40.197:ERROR [launcher]: Cannot start ChromeHeadless
  | [1025/084740.006078:ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.

This is in Docker running Ubuntu 16

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 44
  • Comments: 25

Commits related to this issue

Most upvoted comments

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

If you’re running Puppeteer directly from Node code, you can provide this flag as follows: const browser = await puppeteer.launch({headless: true, args:['--no-sandbox']});

Sorry for the rookie question: and where should I put this text?

browsers: [‘ChromeHeadlessNoSandbox’], customLaunchers: { ChromeHeadlessNoSandbox: { base: ‘ChromeHeadless’, flags: [‘–no-sandbox’] } },

Where is this karma config?

I keep getting smth like this in bitbucket pipeline.

02 05 2018 19:40:16.221:WARN [karma]: No captured browser, open http://localhost:9876/
02 05 2018 19:40:16.230:INFO [karma]: Karma v2.0.2 server started at http://0.0.0.0:9876/
02 05 2018 19:40:16.230:INFO [launcher]: Launching browser Chrome with unlimited concurrency
02 05 2018 19:40:16.236:INFO [launcher]: Starting browser Chrome
02 05 2018 19:40:16.656:ERROR [launcher]: Cannot start Chrome
	[79:79:0502/194016.598825:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.656:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:16.656:ERROR [launcher]: Chrome stderr: [79:79:0502/194016.598825:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.676:INFO [launcher]: Trying to start Chrome again (1/2).
02 05 2018 19:40:16.883:ERROR [launcher]: Cannot start Chrome
	[90:90:0502/194016.759919:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.883:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:16.883:ERROR [launcher]: Chrome stderr: [90:90:0502/194016.759919:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:16.919:INFO [launcher]: Trying to start Chrome again (2/2).
02 05 2018 19:40:17.145:ERROR [launcher]: Cannot start Chrome
	[101:101:0502/194017.008599:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:17.145:ERROR [launcher]: Chrome stdout: 
02 05 2018 19:40:17.145:ERROR [launcher]: Chrome stderr: [101:101:0502/194017.008599:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
02 05 2018 19:40:17.152:ERROR [launcher]: Chrome failed 2 times (cannot start). Giving up.
02 05 2018 19:40:23.683:WARN [karma]: No captured browser, open http://localhost:9876/

When specifying the browser as ChromeHeadless this is automatically added, yes.

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Don’t forget to add --headless ChromeHeadlessNoSandbox to your test command. For me, that meant changing

ng test ${library} --browsers ChromeHeadless --watch=${watch} to

ng test ${library} --browsers ChromeHeadlessNoSandbox --watch=${watch}

It’s probably obvious to you, but it took me a minute to figure that out. I hope that helps.

i solved after see this videos https://youtu.be/tc379GTTgys

100% problem has been fixed 😃 …

@DragonRus By default, most projects have Karma configuration stored in a file named “karma.conf.js.” However, that is just the default; the configuration file could be named anything and passed to Karma like this: karma start some-karma-config.js.

If your project doesn’t have a configuration file, you can generate it by running karma init from a command line. See the Karma documentation.

i solved after see this videos https://youtu.be/tc379GTTgys

100% problem has been fixed 😃 …

THANKS BRO FOR THIS VIDEO MY PROBLEM SOLVED; <<<print(“thanks for this”)

^^ I had something like that in Jenkins. Turned out I had to set the --browsers flag in the command as it was still defaulting to plain headless Chrome without the --no-sandbox flag. Here’s one example of what I used: yarn test --single-run --no-progress --browsers Chrome_without_security

Where Chrome_without_security was set up as this:

module.exports = function (config) {
  process.env.CHROME_BIN = require('puppeteer').executablePath()

  config.set({

    // You may use 'ChromeCanary', 'Chromium' or any other supported browser
    browsers: ['Chrome_without_security'], 
    // you can define custom flags
    customLaunchers: {
      Chrome_without_security: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};

Puppeteer is there to handle any missing Chrome package, of course.

boboldehampsink, sorry, but where should this workaround be written? Is it about docker?

Worked for me too. Thanks guys!

Keywords: CodeBuild, Karma, Chrome

@Zicrou or any can give some advice about How it works for you?

i will show mine config for compare

karma.config

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html
const process = require('process');
process.env.CHROME_BIN = require('puppeteer').executablePath();

module.exports = function(config) {
    config.set({
        basePath: '',
        frameworks: ['jasmine', '@angular-devkit/build-angular'],
        plugins: [
            require('karma-jasmine'),
            require('karma-chrome-launcher'),
            require('karma-jasmine-html-reporter'),
            require('karma-coverage-istanbul-reporter'),
            require('@angular-devkit/build-angular/plugins/karma')
        ],
        client: {
            clearContext: false // leave Jasmine Spec Runner output visible in browser
        },
        coverageIstanbulReporter: {
            dir: require('path').join(__dirname, './coverage/angCICD'),
            reports: ['html', 'lcovonly', 'text-summary'],
            fixWebpackSourcePaths: true
        },
        reporters: ['progress', 'kjhtml'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox']
            }

        },
        singleRun: false,
        restartOnFileChange: true,
        concurrency: Infinity
    });
};

protractor.config

// @ts-check
// Protractor configuration file, see link for more information
// https://github.com/angular/protractor/blob/master/lib/config.ts
const puppeteer = require('puppeteer');
const { SpecReporter } = require('jasmine-spec-reporter');

/**
 * @type { import("protractor").Config }
 */
exports.config = {
    allScriptsTimeout: 11000,
    specs: [
        './src/**/*.e2e-spec.ts'
    ],
    capabilities: {
        browserName: 'chrome',
        chromeOptions: {
            args: ['--headless', '--no-sandbox'],
            binary: puppeteer.executablePath()
        }
    },
    directConnect: true,
    baseUrl: 'http://localhost:4200/',
    framework: 'jasmine',
    jasmineNodeOpts: {
        showColors: true,
        defaultTimeoutInterval: 30000,
        print: function() {}
    },
    onPrepare() {
        require('ts-node').register({
            project: require('path').join(__dirname, './tsconfig.json')
        });
        jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
    }
};

gitlab-ci.yml

variables:
  IMG_BUILD: node:latest
  IMG_TEST: trion/ng-cli-karma
  IMG_TESTING: alekzonder/puppeteer:latest
  IMG_TESTING_FINAL: node:8.9.1
  IMG_GITLAB_CI: juristr/angular-ci-build:1.0.0

deploy_test:
  stage: test
  image: ${IMG_BUILD}
  environment: Production
  artifacts:
    paths:
      - node_modules/
  only:
    - master
  script:
    - rm ./package-lock.json
    - apt-get update
    - apt-get install gconf-service libasound2 libatk1.0–0 libc6 libcairo2 libcups2 libdbus-1–3 libexpat1 libfontconfig1 libgcc1 libgconf-2–4 libgdk-pixbuf2.0–0 libglib2.0–0 libgtk-3–0 libnspr4 libpango-1.0–0 libpangocairo-1.0–0 libstdc++6 libx11–6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget -y
    - npm install
    - npm run test-ci

definition for npm run test-ci is:

"test-ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessNoSandbox",

and that is the error give me gitlab jobs in console

21 08 2019 17:30:16.241:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2019 17:30:16.357:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
21 08 2019 17:30:16.358:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency unlimited
21 08 2019 17:30:16.361:INFO [launcher]: Starting browser ChromeHeadless
21 08 2019 17:30:17.276:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.276:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.277:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.307:INFO [launcher]: Trying to start ChromeHeadless again (1/2).
21 08 2019 17:30:17.358:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.359:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.359:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.382:INFO [launcher]: Trying to start ChromeHeadless again (2/2).
21 08 2019 17:30:17.436:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.437:ERROR [launcher]: ChromeHeadless stdout: 
21 08 2019 17:30:17.437:ERROR [launcher]: ChromeHeadless stderr: /app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

21 08 2019 17:30:17.469:ERROR [launcher]: ChromeHeadless failed 2 times (cannot start). Giving up.
21 08 2019 17:30:22.314:WARN [karma]: No captured browser, open http://localhost:9876/

I have this version of puppeteer --> “puppeteer”: “^1.19.0”,

i am missing something?

thx

^^ I had something like that in Jenkins. Turned out I had to set the --browsers flag in the command as it was still defaulting to plain headless Chrome without the --no-sandbox flag. Here’s one example of what I used: yarn test --single-run --no-progress --browsers Chrome_without_security

Where Chrome_without_security was set up as this:

module.exports = function (config) {
  process.env.CHROME_BIN = require('puppeteer').executablePath()

  config.set({

    // You may use 'ChromeCanary', 'Chromium' or any other supported browser
    browsers: ['Chrome_without_security'], 
    // you can define custom flags
    customLaunchers: {
      Chrome_without_security: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};

Puppeteer is there to handle any missing Chrome package, of course.

I was not aware of the ability to define custom launchers. I would consider it very helpful if this was added to the README.

For me, my karma.config.js was missing this:

 customLaunchers: {
      chromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Workaround:

    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },

Don’t forget to add --headless ChromeHeadlessNoSandbox to your test command. For me, that meant changing

ng test ${library} --browsers ChromeHeadless --watch=${watch} to

ng test ${library} --browsers ChromeHeadlessNoSandbox --watch=${watch}

It’s probably obvious to you, but it took me a minute to figure that out. I hope that helps.

about this solution i have this setup

karma.config

const process = require('process');
process.env.CHROME_BIN = require('puppeteer').executablePath();

.
.
.
.

 browsers: ['ChromeHeadlessNoSandbox'],
        customLaunchers: {
            ChromeHeadlessNoSandbox: {
                base: 'ChromeHeadless',
                flags: ['--no-sandbox', '--headless']
            }

        },

in package.json apply npm run test-ci

"test-ci": "ng test --no-watch --no-progress --browsers=ChromeHeadlessNoSandbox",

gitlab-ci results

21 08 2019 20:01:29.751:WARN [karma]: No captured browser, open http://localhost:9876/
21 08 2019 20:01:29.822:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
21 08 2019 20:01:29.822:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency unlimited
21 08 2019 20:01:29.849:INFO [launcher]: Starting browser ChromeHeadless
21 08 2019 20:01:30.732:ERROR [launcher]: Cannot start ChromeHeadless
	/app/node_modules/puppeteer/.local-chromium/linux-674921/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

In karma config