“ChromeHeadless 在 60000 ms 内没有捕获,杀死。” 仅发生在 Gitlab 托管的 CI/CD 管道中

nev*_*eik 14 selenium docker karma-jasmine gitlab-ci gitlab-ci-runner

在 Gitlab 上运行 CI/CD 管道时,我的 Karma 测试超时并出现错误:

? ?wdm?: Compiled successfully.
05 08 2019 22:25:31.483:INFO [karma-server]: Karma v4.2.0 server started at http://0.0.0.0:9222/
05 08 2019 22:25:31.485:INFO [launcher]: Launching browsers ChromeHeadlessNoSandbox with concurrency 1
05 08 2019 22:25:31.488:INFO [launcher]: Starting browser ChromeHeadless
05 08 2019 22:26:31.506:WARN [launcher]: ChromeHeadless have not captured in 60000 ms, killing.
05 08 2019 22:26:31.529:INFO [launcher]: Trying to start ChromeHeadless again (1/2).
05 08 2019 22:27:31.580:WARN [launcher]: ChromeHeadless have not captured in 60000 ms, killing.
05 08 2019 22:27:31.600:INFO [launcher]: Trying to start ChromeHeadless again (2/2).
05 08 2019 22:28:31.659:WARN [launcher]: ChromeHeadless have not captured in 60000 ms, killing.
05 08 2019 22:28:31.689:ERROR [launcher]: ChromeHeadless failed 2 times (timeout). Giving up.



npm ERR! Test failed.  See above for more details.
Run Code Online (Sandbox Code Playgroud)

在本地运行测试时不会出现此问题,在本地使用与 Gitlab Runner相同的Docker 镜像运行测试时也不会出现此问题。

我觉得我已经尝试了所有可能的配置karma.conf.js。我在谷歌上无情地搜索了这个问题,并尝试了从代理服务器到环境变量,再到标志的每一个建议......但可惜,没有运气。我尝试了多个 Docker 映像,因为这最初在本地 Gitlab Runner 上失败,但我发现 Docker 映像selenium/standalone-chrome:latest在本地 Gitlab Runner 中运行良好。

这是我的karma.conf.js文件:

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

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',

    // frameworks to use
    frameworks: [ 'jasmine' ],

    // list of files / patterns to load in the browser
    files: [
      'src/**/*.spec.js'
    ],

    // list of files / patterns to exclude
    exclude: [],

    // preprocess matching files before serving them to the browser
    preprocessors: {
      'src/**/*.spec.js': [ 'webpack' ]
    },

    webpack: {
      // webpack configuration
      mode: 'development',
      module: {
        rules: [
          {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
              presets: ['env']
            }
          }
        ]
      },
      stats: {
        colors: true
      }
    },


    // test results reporter to use
    reporters: [ 'spec' ],

    // web server port
    port: 9222,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,

    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,

    // plugins for karma
    plugins: [
      'karma-chrome-launcher',
      'karma-webpack',
      'karma-jasmine',
      'karma-spec-reporter'
    ],

    // start these browsers
    browsers: ['ChromeHeadlessNoSandbox'],
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: [
          '--headless',
          '--no-sandbox',
          '--disable-gpu'
        ]
      }
    },
    captureTimeout: 60000,
    browserDisconnectTolerance: 5,
    browserDisconnectTimeout : 30000,
    browserNoActivityTimeout : 30000,

    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: 1
  })
}
Run Code Online (Sandbox Code Playgroud)

这是我的 .gitlab-ci.yml 文件:

.prereq_scripts: &prereq_scripts |
  sudo apt -y update && sudo curl -sL https://deb.nodesource.com/setup_10.x | sudo bash && sudo apt -y install nodejs

image: 'selenium/standalone-chrome:latest'

stages:
- test

test:
  stage: test
  script:
  - *prereq_scripts
  - npm install
  - npm test
Run Code Online (Sandbox Code Playgroud)

我希望测试能够在所有三个实例(本地 npm、本地 Gitlab Runner 和远程 Gitlab CI/CD 管道)中成功运行。目前它只在前两个中成功运行。

Tob*_*ter 2

在您的karma.conf.js文件中,您需要在 module.exports 函数内声明 CHROME_BIN 变量:

module.exports = function(config) {

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

    config.set({
    ...
Run Code Online (Sandbox Code Playgroud)