Azure DevOps构建管道执行Angular测试时找不到Chromium(rev.1108766)求助
解决Azure DevOps构建管道中Angular测试找不到Chromium的问题
出现"Could not find Chromium (rev. 1108766)"错误,主要是配置语法错误、Chromium下载/缓存异常,以及无头Chrome启动参数缺失导致的,以下是具体修复方案:
一、修正Karma配置文件的语法错误
你的kamas.conf(应为karma.conf.js)里存在无效代码:
cacheDirectory: join(__dirname, '.cache', 'puppeteer'),<sup>your text</sup>
这行代码直接写在module.exports的函数内部,属于语法错误,会干扰配置加载,必须删除。
二、配置无头Chrome的启动参数
Azure DevOps构建环境属于受限环境,默认的ChromeHeadless无法直接运行,需要添加--no-sandbox等参数规避权限问题。修改后的karma.conf.js如下:
// Karma configuration file, see link for more information // https://karma-runner.github.io/1.0/config/configuration-file.html const puppeteer = require('puppeteer'); const { join } = require('path'); // 绑定puppeteer自带的Chrome路径 process.env.CHROME_BIN = 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'), require('karma-junit-reporter'), require('@angular-devkit/build-angular/plugins/karma') ], client: { jasmine: {}, clearContext: false }, jasmineHtmlReporter: { suppressAll: true }, coverageReporter: { dir: join(__dirname, './coverage'), subdir: '.', reporters: [ { type: 'html' }, { type: 'text-summary' }, { type: 'lcovonly' }, { type: 'cobertura' } ], check: { global: { statements: 80, branches: 80, functions: 80, lines: 80 } } }, reporters: ['progress', 'kjhtml', 'junit'], junitReporter: { outputDir: '', outputFile: undefined, suite: '', useBrowserName: true, nameFormatter: undefined, classNameFormatter: undefined, properties: {}, xmlVersion: null }, port: 9876, colors: true, logLevel: config.LOG_INFO, autoWatch: true, // 自定义无头Chrome启动配置,适配构建环境 customLaunchers: { ChromeHeadlessCustom: { base: 'ChromeHeadless', flags: [ '--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage', '--disable-web-security' ] } }, browsers: ['ChromeHeadlessCustom'], singleRun: true, restartOnFileChange: false }); };
三、确保Puppeteer正确下载Chromium
Azure DevOps构建代理是临时环境,默认缓存目录不持久化,需通过以下方式确保Chromium下载成功:
- 调整Puppeteer缓存配置
保留你的puppeteer.conf.js,但将缓存目录指向构建管道工作区,避免每次构建重新下载:
const { join } = require('path'); /** * @type {import("puppeteer").Configuration} */ module.exports = { cacheDirectory: join(process.env.PIPELINE_WORKSPACE || __dirname, '.cache', 'puppeteer'), };
- 修改Azure DevOps管道步骤
在执行npm install时,添加参数确保权限足够,并强制下载Chromium:
- 设置环境变量:
PUPPETEER_SKIP_DOWNLOAD=false - 执行安装命令:
npm install --unsafe-perm - 测试命令改为:
npm test -- --browsers=ChromeHeadlessCustom
- 可选:配置Pipeline Cache缓存Chromium
在Azure DevOps管道中添加缓存任务,缓存.cache/puppeteer目录,减少重复下载时间:
- task: Cache@2 inputs: key: 'puppeteer | "$(Agent.OS)" | package-lock.json' restoreKeys: | puppeteer | "$(Agent.OS)" path: $(System.DefaultWorkingDirectory)/.cache/puppeteer
四、验证修复
重新运行Azure DevOps构建管道,测试命令将使用自定义的无头Chrome配置,且Chromium会被正确加载。
内容的提问来源于stack exchange,提问作者dead pool
相关产品推荐
相关产品推荐

