Chrome和Chromedriver版本不匹配,导致Angular e2e测试在GitHub操作中失败

问题描述

我的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 ,并且似乎正在下载chromedriver的版本 85 。但是, 也没有意义,因为Angular使用webdriver-manager,我认为应该管理正确的chromedriver版本?为什么安装了错误的版本?还是我的事情倒退了?

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

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依赖性,但这无济于事。

我还尝试在工作流程中设置webdriverUpdate=false(假定自GitHub上的图像声称具有Chrome 84 关联的chromedriver版本),但这会导致: / p>

[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.

许多“解决方案”(例如this related SO thread)建议将chromedriver更新为特定 chrome版本。但这不是我想要的,因为每当GitHub更新其映像时,我都会再次遇到相同的问题。

还要注意的重要一点:我很想拥有针对CI的解决方案。开发人员(即我)通常会在其计算机上安装最新的Chrome,因此运行npm run e2e仍应为我 my 计算机提供合适的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

工作流程代码网址: 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