Google-Cloud-Platform

Karma 單元測試無法在使用 Puppeteer 的 Google Cloud Build 中執行

  • July 13, 2019

我正在嘗試使用 puppeteer 執行 ChromeHeadless 來執行我的單元測試。但是我無法啟動 ChromeHeadless 並收到以下錯誤。

03 07 2019 03:19:25.066:INFO [karma-server]: Karma v4.1.0 server started at http://0.0.0.0:9876/
03 07 2019 03:19:25.071:INFO [launcher]: Launching browsers ChromeHeadless with concurrency unlimited
03 07 2019 03:19:25.080:INFO [launcher]: Starting browser ChromeHeadless
03 07 2019 03:19:25.573:ERROR [launcher]: Cannot start ChromeHeadless
/workspace/node_modules/puppeteer/.local-chromium/linux-672088/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

03 07 2019 03:19:25.574:ERROR [launcher]: ChromeHeadless stdout: 
03 07 2019 03:19:25.574:ERROR [launcher]: ChromeHeadless stderr: /workspace/node_modules/puppeteer/.local-chromium/linux-672088/chrome-linux/chrome: error while loading shared libraries: libX11-xcb.so.1: cannot open shared object file: No such file or directory

在我的 Karma.conf.js 我有::

   browsers : ['ChromeHeadlessCustom'],
   customLaunchers: {
     ChromeHeadlessCustom: {
       base: 'ChromeHeadless',
       flags: ['--no-sandbox', '--disable-setuid-sandbox']
     }
   },

cloudbuild.yaml ::

 - name: 'gcr.io/cloud-builders/npm:node-10.10.0'
   id: 'Install Puppeteer'
   args: ['install',  'puppeteer']

 - name: 'gcr.io/cloud-builders/npm:node-10.10.0'
   id: 'Test'
   args: ['run', 'test']

選定的 docker 容器,沒有執行 chrome 所需的包;你應該改變,更新一個;

cloud build.yaml 來執行 headless-chrome 測試(另外,你應該將 puppeeter 放在你的 package.json 中的依賴項中):

- name: 'buildkite/puppeteer'
 args: ['npm','install']
- name: 'buildkite/puppeteer'
 args: ['npm', 'run', 'test', ]

引用自:https://serverfault.com/questions/973873