硒如何访问Github Actions上的capybara服务器?



我正在用Ruby on Rails创建一个个人项目。

我试图创建一个CI与Github行动+ Capybara +硒服务。为什么是硒服务?为了使它更接近项目的docker-compose.

首先,这是我的工作流文件:
name: CI
on: push
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14.4
ports:
- 5432:5432
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
selenium:
image: selenium/standalone-chrome:3.141.59
ports:
- 4444:4444
steps:
-
name: Check out repository code
uses: actions/checkout@v3
-
name: 'Create web docker image'
run: docker build -t web . --target new_release
-
name: 'Create database'
run: docker run --env-file .docker/env_files/.env.ci --network="host" web bin/rails db:create db:migrate
-
name: 'Run unit tests'
run: docker run --env-file .docker/env_files/.env.ci --network="host" web bin/rails test
-
name: 'Run system tests'
run: docker run --env-file .docker/env_files/.env.ci --network="host" web bin/rails test:system

在最后一步发生错误:

Selenium::WebDriver::Error::UnknownError: unknown Error: net::ERR_CONNECTION_REFUSED

我的解释是selenium服务无法连接到capybara服务器。那么,让我们来谈谈项目的水豚配置:

# frozen_string_literal: true
require "test_helper"
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
Capybara.register_driver(:chrome_remote) do |app|
Capybara::Selenium::Driver.new(
app,
browser: :remote,
url: ENV.fetch("SELENIUM_URL"),
capabilities: Selenium::WebDriver::Remote::Capabilities.chrome
)
end
Capybara.run_server = true
Capybara.server = :puma, { Silent: true }
Capybara.server_host = ENV.fetch("CAPYBARA_SERVER_HOST")
Capybara.server_port = ENV.fetch("CAPYBARA_SERVER_PORT")
Capybara.default_driver = :chrome_remote
Capybara.javascript_driver = :chrome_remote
Capybara.save_path = Rails.root.join("tmp/capybara")
driven_by :chrome_remote, using: :chrome, screen_size: [1400, 1400]
include FactoryBot::Syntax::Methods
end

最后,在ci上使用的env文件:

CAPYBARA_SERVER_HOST=localhost
CAPYBARA_SERVER_PORT=4000
POSTGRES_PASSWORD=postgres
POSTGRES_HOST=localhost
RAILS_ENV=test
SELENIUM_URL=http://localhost:4444/wd/hub

我尝试不使用——network="host",使用服务的名称,但使用该配置我无法通过"创建数据库"的一步。我想知道为什么不工作,我可以看到capybara使用我的环境变量创建服务器:

Capybara starting Puma...
* Version 5.6.2 , codename: Birdie's Version
* Min threads: 0, max threads: 4
* Listening on http://127.0.0.1:4000
* Listening on http://[::1]:4000

但是我迷路了,不确定什么连接被拒绝了。在当地环境中,测试完全通过。你觉得呢?

我决定使用docker-compose而不是使用services + docker。成功的文件:

version: "3.7"
services:
db:
image: postgres:14.4
ports:
- 5432:5432
env_file:
- ./.docker/env_files/.env
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
volumes:
- ./.docker/volumes/postgres_data:/var/lib/postgresql/data
web:
image: test
env_file:
- ./.docker/env_files/.env
build:
context: .
depends_on:
db:
condition: service_healthy
links:
- db
ports:
- 3000:3000
volumes:
- .:/opt/test:cached
stdin_open: true
tty: true
livereload:
image: test
depends_on:
- web
ports:
- 35729:35729
command: ["bundle", "exec", "guard", "-i"]
env_file:
- ./.docker/env_files/.env
volumes:
- .:/opt/test:cached
tailwindcsswatcher:
image: test
depends_on:
- web
ports:
- 3035:3035
command: ["bin/rails", "tailwindcss:watch"]
env_file:
- ./.docker/env_files/.env
volumes:
- .:/opt/test:cached
tty: true
selenium:
image: selenium/standalone-chrome:3.141.59
ports:
- 4444:4444
volumes:
postgres_data:

现在是ci。使用docker-compose:

name: CI
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
-
name: Check out repository code
uses: actions/checkout@v3
-
name: 'Create docker-compose file'
run: cp .docker/docker-compose.dev.yml docker-compose.yml
-
name: 'Create env file'
run: cp .docker/env_files/.env.dev .docker/env_files/.env
-
name: Build web container
run: docker-compose build web selenium
-
name: 'Create database'
run: docker-compose run --rm web bin/rails db:create db:migrate
-
name: 'Start container'
run: docker-compose up -d web selenium
-
name: 'Run unit tests'
run: docker-compose exec -T web bin/rails test
-
name: 'Run system tests'
run: docker-compose exec -T web bin/rails test:system

最新更新