在 Github 操作中设置 Capybara / Selenium 测试时遇到问题

Troubles setting up Capybara / Selenium tests in Github Actions

我正在为 运行 一个 rails webapp 的整个测试套件设置一个 Github Actions 工作流程,并且在使用 Capybara 和 Selenium 的测试中遇到了一些麻烦(遗憾的是我几乎没有使用这些工具的经验!)。

这是来自 Github 操作的错误消息:

22) Participant authentication Participant signs out
      Failure/Error: visit '/'

      Selenium::WebDriver::Error::UnknownError:
        Reached error page: about:neterror?e=dnsNotFound&u=http%3A//project.example.com%3A31337/&c=UTF-8&d=We%20can%E2%80%99t%20connect%20to%20the%20server%20at%20project.example.com.
      # WebDriverError@chrome://marionette/content/error.js:181:5
      # UnknownError@chrome://marionette/content/error.js:488:5
      # checkReadyState@chrome://marionette/content/navigate.js:63:24
      # onNavigation@chrome://marionette/content/navigate.js:317:39
      # emit@resource://gre/modules/EventEmitter.jsm:160:20
      # receiveMessage@chrome://marionette/content/actors/MarionetteEventsParent.jsm:40:25
      # ./spec/features/public/registration_and_authentication_spec.rb:131:in `block (2 levels) in <main>'

这是 worklflow yml 文件:

name: Continuous Integration
on:
  push:
    branches: [ setup-github-actions-2 ]
  pull_request:
    branches: [ setup-github-actions-2 ]
jobs:
  build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:11
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - name: Set Timezone
        run: |
          sudo timedatectl set-timezone Europe/Paris

      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Setup chrome driver (for Selenium)
        uses: nanasess/setup-chromedriver@master
        # with:
          # Optional: do not specify to match Chrome's version
          # chromedriver-version: '88.0.4324.96'
      - run: |
          export DISPLAY=:99
          chromedriver --url-base=/wd/hub &
          sudo Xvfb -ac :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & # optional

      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: 2.7.2
          bundler-cache: true # runs 'bundle install' and caches installed gems automatically

      - name: Install node modules
        uses: borales/actions-yarn@v2.0.0
        with:
          cmd: install

      - name: Install dependent libraries
        run: sudo apt-get install libpq-dev

      - name: Bundle install
        run: |
          gem install bundler
          bundle install --jobs 4 --retry 3

      - name: Setup Database
        run: |
          cp config/database.yml.example config/database.yml
          bundle exec rake db:create
          bundle exec rake db:schema:load
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on helpers
        if: always() # make sure to not stop the workflow if one test fails
        run: bundle exec rspec spec/helpers
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on jobs
        if: always()
        run: bundle exec rspec spec/jobs
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on lib
        if: always()
        run: bundle exec rspec spec/lib
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on mailers
        if: always()
        run: bundle exec rspec spec/mailers
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on models
        if: always()
        run: bundle exec rspec spec/models
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on controllers
        if: always()
        run: bundle exec rspec spec/controllers
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run RSpec on requests
        if: always()
        run: bundle exec rspec spec/requests
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      # 28 examples, 25 failures => Selenium error
      - name: Run RSpec on features
        if: always()
        run: bundle exec rspec spec/features
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

      - name: Run Jest test suite
        if: always()
        run: bundle exec yarn test
        env:
          RAILS_ENV: test
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres

请注意,测试在本地运行良好。

关于我在这里遗漏了什么的任何线索?

从错误消息中,您可以看到浏览器正被发送到 http://project.example.com/...,但它不存在,即使它不存在,也不会出现在您的测试应用所在的位置 运行ning .假设您 运行 在本地使用相同的测试配置,您可能 project.example.com 指向 localhost/127.0.0.1(检查您的 /etc/hosts 或本地 DNS 配置)会让你的测试在那里工作。您需要更新您的测试配置,以便 运行 针对您正在测试的应用 运行 打开的任何 IP 进行测试。

为了完整起见,以下是我在特定配置下通过测试所缺少的内容:

steps:
    - name: Add hosts to /etc/hosts
      run: |
        sudo echo "172.16.18.16 example.test" | sudo tee -a /etc/hosts
        sudo echo "172.16.18.16 example2.test" | sudo tee -a /etc/hosts

参考:https://github.community/t/add-host-to-etc-hosts/17364