在Github Actions中设置Capybara/Selenium测试时出现问题



我正在设置Github Actions工作流,以运行rails网络应用程序的整个测试套件,并且在使用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

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

有没有线索表明我在这里遗漏了什么?

从错误消息中,您可以看到浏览器正在发送到不存在的http://project.example.com/...,即使它不存在,也不会是您的测试应用程序运行的地方。假设您在本地运行相同的测试配置,那么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

最新更新