如何在 Jenkins env 变量中为 Headless Chrome 配置 CHROME_BIN 路径

Posted

技术标签:

【中文标题】如何在 Jenkins env 变量中为 Headless Chrome 配置 CHROME_BIN 路径【英文标题】:How to configure CHROME_BIN path in Jenkins env variable for Headless Chrome 【发布时间】:2019-08-23 10:52:22 【问题描述】:

我正在开发基本的 Angular 项目,我可以在我的 Windows 上使用无头 chrome 运行 karma 和 Jasmin 的测试。但是Jenkins说No binary for ChromeHeadless browser on your platform,所以问题是如何在Jenkins配置中配置ChromeHeadless。

错误日志

您的平台上没有适用于 ChromeHeadless 浏览器的二进制文件。请设置 CHROME_BIN" 环境变量。npm ERR!测试失败。

package.json

 "devDependencies": 
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.1.0",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "^2.9.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage": "^1.1.2",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-sonarqube-unit-reporter": "0.0.18",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "^2.9.2"
  

karma.conf.js

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) 

  config.set(
    basePath: '',
    // captureTimeout: 60000, // it was already there
    // browserDisconnectTimeout: 10000,
    // browserDisconnectTolerance: 1,
    // browserNoActivityTimeout: 60000, //by default 10000
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-coverage'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      // require('@angular/cli/plugins/karma'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-sonarqube-unit-reporter'),
      // require('karma-phantomjs-launcher')

    ],
    client: 
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    ,
    coverageIstanbulReporter: 
      // dir: require('path').join(__dirname, '../coverage'),
      reports: ['html', 'lcovonly'],
      useBrowserName: false,
      fixWebpackSourcePaths: true
    ,
    reporters: ['progress', 'kjhtml', 'sonarqubeUnit', 'coverage'],
    sonarQubeUnitReporter: 
      outputFile: '../../target/angular_reports/ut_report.xml',
      useBrowserName: false
    ,
    // angularCli: 
    //   environment: 'dev'
    // ,
    coverageReporter: 
      // specify a common output directory
      dir: '../../target/angular_reports/coverage',
      reporters: [
        // reporters not supporting the `file` property
        
          type: 'html',
          subdir: 'report-html'
        ,
        
          type: 'lcov',
          subdir: 'report-lcov'
        ,
        // reporters supporting the `file` property, use `subdir` to directly
        // output them in the `dir` directory
        
          type: 'cobertura',
          subdir: '.',
          file: 'cobertura.txt'
        ,
        
          type: 'lcovonly',
          subdir: '.',
          file: 'report-lcovonly.txt'
        ,
        
          type: 'teamcity',
          subdir: '.',
          file: 'teamcity.txt'
        ,
        
          type: 'text',
          subdir: '.',
          file: 'text.txt'
        ,
        
          type: 'text-summary',
          subdir: '.',
          file: 'text-summary.txt'
        ,
      ]
    ,
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    // browsers: ['Chrome'],
    // browsers: ['ChromeHeadless'],
    // browsers: ['Chrome'],
    browsers: ['MyHeadlessChrome'],
    // browsers: ['Chrome', 'ChromeHeadless', 'MyHeadlessChrome'],
    customLaunchers: 
      MyHeadlessChrome: 
        base: 'ChromeHeadless',
        flags: ['--disable-translate', '--disable-extensions', '--remote-debugging-port=9223']
      
    ,
    // singleRun: false
    singleRun: true
  );
;

【问题讨论】:

检查这个问题:***.com/questions/45881228/… How to add the chrome binary to run e.g. Karma tests on headless chrome on a CI server的可能重复 我已经检查了该解决方案它不再起作用了,你试过了吗?朋友@jahller 【参考方案1】:

您可以将 CHROME_BIN 环境变量设置为 chrome 浏览器可执行文件的位置

【讨论】:

以上是关于如何在 Jenkins env 变量中为 Headless Chrome 配置 CHROME_BIN 路径的主要内容,如果未能解决你的问题,请参考以下文章

使用 env 在 bash 中为一个程序调用设置环境变量

在 Docker 中为 AWS Elastic Beanstalk 构建期间访问 .env 变量

在 terraform 文件而不是 env 变量中为 Terraform 提供凭据

Jenkins-pipeline查看内置的env种类及取值

Groovy Init 中的 Jenkins 环境变量

在詹金斯作业中使用 git repo 名称作为 ENV 变量