【问题标题】:Troubles setting up Capybara / Selenium tests in Github Actions在 Github Actions 中设置 Capybara / Selenium 测试的问题
【发布时间】:2021-03-30 21:40:22
【问题描述】:

我正在设置一个 Github Actions 工作流来运行 Rails Web 应用程序的整个测试套件,但在使用 Capybara 和 Selenium 的测试时遇到了一些问题(遗憾的是,我几乎没有使用这些工具的经验!)。

这是来自 Github Actions 的错误消息:

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>'

这是工作流 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

需要注意的是,测试在本地运行良好。

关于我在这里缺少什么的任何线索?

【问题讨论】:

    标签: ruby-on-rails selenium capybara github-actions


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      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

      【讨论】:

        猜你喜欢
        • 2021-06-25
        • 1970-01-01
        • 2021-10-03
        • 2020-12-22
        • 2022-01-17
        • 2011-12-17
        • 2021-06-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多