由于 Chrome 与 Chromedriver 版本不匹配,Angular e2e 测试在 GitHub 操作中失败

Angular e2e tests failing in GitHub actions because of Chrome vs Chromedriver version mismatch

我的 open source Angular projectlintbuildteste2e 一切都进行了成功的 GitHub 操作。然而,不久之后,我看到了这个:

[00:20:01] I/launcher - Running 1 instances of WebDriver
[00:20:01] I/direct - Using ChromeDriver directly...
[00:20:07] E/launcher - session not created: This version of ChromeDriver only supports Chrome version 85
  (Driver info: chromedriver=85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}),platform=Linux 5.3.0-1034-azure x86_64)
[00:20:07] E/launcher - SessionNotCreatedError: session not created: This version of ChromeDriver only supports Chrome version 85
  (Driver info: chromedriver=85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}),platform=Linux 5.3.0-1034-azure x86_64)
    at Object.checkLegacyResponse (/home/runner/work/sample-angular-oauth2-oidc-with-auth-guards/sample-angular-oauth2-oidc-with-auth-guards/node_modules/selenium-webdriver/lib/error.js:546:15)
    at parseHttpResponse (/home/runner/work/sample-angular-oauth2-oidc-with-auth-guards/sample-angular-oauth2-oidc-with-auth-guards/node_modules/selenium-webdriver/lib/http.js:509:13)
    at /home/runner/work/sample-angular-oauth2-oidc-with-auth-guards/sample-angular-oauth2-oidc-with-auth-guards/node_modules/selenium-webdriver/lib/http.js:441:30
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

在某种程度上这是有道理的,因为 GitHub 图像包含 Chrome 84,并且版本 85[正在下载 chromedriver 中的 =55=]。但它也 not 有意义,因为 Angular 使用 webdriver-manager 我认为应该 manage 正确版本的 chromedriver?为什么会安装错误的版本?还是我把事情搞反了?

我的情况应该可以通过分叉存储库并设置相同的 GitHub 操作来重现,这是工作流程的相关部分:

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    # Based on: https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
    - name: Cache node modules
      uses: actions/cache@v2
      env:
        cache-name: cache-node-modules
      with:
        # npm cache files are stored in `~/.npm` on Linux/macOS
        path: ~/.npm
        key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
        restore-keys: |
          ${{ runner.os }}-build-${{ env.cache-name }}-
          ${{ runner.os }}-build-
          ${{ runner.os }}-

    - name: Install dependencies
      run: npm ci

    - name: Linting
      run: npm run lint

    - name: Build
      run: npm run build -- --prod

    - name: Tests
      run: npm run e2e -- --configuration=ci

angular.json目标也很简单:

"e2e": {
  "builder": "@angular-devkit/build-angular:protractor",
  "options": {
    "protractorConfig": "e2e/protractor.conf.js",
    "devServerTarget": "sample-auth-guards:serve"
  },
  "configurations": {
    "production": {
      "devServerTarget": "sample-auth-guards:serve:production"
    },
    "ci": {
      "devServerTarget": "sample-auth-guards:serve:production",
      "protractorConfig": "e2e/protractor-ci.conf.js"
    }
  }
}

我尝试更新所有 NPM devdependencies,但这没有帮助。

我也试过在工作流程中设置 webdriverUpdate=false(假设来自 GitHub 的图像声称有 Chrome 84 关联的 chromedriver 版本),但这会导致:

[16:43:43] I/launcher - Running 1 instances of WebDriver
[16:43:44] I/direct - Using ChromeDriver directly...
[16:43:44] E/direct - Error code: 135
[16:43:44] E/direct - Error message: Could not find update-config.json. Run 'webdriver-manager update' to download binaries.
[16:43:44] E/direct - Error: Could not find update-config.json. Run 'webdriver-manager update' to download binaries.

许多“解决方案”(例如 )建议将 chromedriver 更新为 specific chrome 版本。但这不是我想要的,因为每当 GitHub 更新他们的图像时,我都会再次遇到同样的问题。

同样重要的是要注意:我很想有一个 CI-specific 解决方案。开发人员(即我)通常会在他们的机器上安装最新的 Chrome,所以 运行 npm run e2e 应该仍然给我 chromedriver 的权利 我的机器。

底线:如何使 Angular 项目下载 chromedriver 的 right 版本? 也就是:机器上安装的属于Chrome版本的那个?

问题出在量角器上,而不是 github 动作。好像这个问题经常发生。基本上量角器在 chrome 稳定版本发布之前将 chrome 驱动程序更新到最新版本,因此差异会导致此设置中断。正如您已经提到的,当前量角器正在下载 85 版,但 chrome 版本是 84.

来源: https://github.com/angular/protractor/issues/5460

分叉您的代码并找到两个解决方案

解决方案 1: 将 google chrome 更新为最新的内部工作流程

  - run: sudo apt-get install google-chrome-stable

这将安装 google chrome 当前最新的稳定版本,即 85。似乎已经发布,所以这允许您 运行 一切正常。为了将来的参考,这只是因为 chrome 最终发布了 85 作为稳定版本,否则,我们将 运行 陷入同样的​​问题,我们将不得不像在解决方案 2.

管道表明它有效: https://github.com/meroware/sample-angular-oauth2-oidc-with-auth-guards/actions/runs/230800136

如果您想在不必担心版本奇偶校验的情况下进行实际修复,则可以执行以下操作。

解决方案 2: 运行 内部 docker 以确保我们的构建是干净的

管道表明它有效: https://github.com/meroware/sample-angular-oauth2-oidc-with-auth-guards/runs/1046824367

工作流代码Url:https://github.com/meroware/sample-angular-oauth2-oidc-with-auth-guards/blob/master/.github/workflows/ci-workflow.yml

这是更改的简要片段

     container:
        image: ubuntu:trusty
     steps:
     - uses: actions/checkout@v2
     - name: Use Node.js
       uses: actions/setup-node@v1
       with:
         node-version: '14.x'
     # Based on: https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#example-using-the-cache-action
     - name: Cache node modules
       uses: actions/cache@v2
       env:
         cache-name: cache-node-modules
       with:
         # npm cache files are stored in `~/.npm` on Linux/macOS
         path: ~/.npm
         key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
         restore-keys: |
           ${{ runner.os }}-build-${{ env.cache-name }}-
           ${{ runner.os }}-build-
           ${{ runner.os }}-
     - name: Setup chrome driver environment
       run: |
         apt-get update # Remove if running outside container
         apt-get clean # Remove if running outside container
         apt-get install -y wget # Remove if running outside container
         wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - # Remove if running outside container
         echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list # Remove if running outside container
         apt-get -y update # Remove if running outside container
         apt-get install -y google-chrome-stable # If not running in docker then run this line with sudo
         VERSION=`google-chrome --version | egrep -o '[0-9]+.[0-9]+' | head -1` # Get chrome version that we just installed
         npm i webdriver-manager@latest -D # Install webdriver manager locally
         npm i chromedriver --chromedriver_version=$VERSION -D # Install chrome driver to the version that we got from google chrome installation above

我将其他答案重新混合到我的最终解决方案中。我想单独分享它,因为对于其他人来说,它可能也像这几行一样简单:

- name: Update Chrome # See 
  run: |
    wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
    sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
    sudo apt-get update
    sudo apt-get --only-upgrade install google-chrome-stable

我使用的 GitHub Actions 中的图片不知为何也需要 Google 的 apt 源(即使它已经预装了 Google Chrome) ,之后更新 + 安装技巧开始正常工作。