使用 Selenium 的 Docker-compose 测试失败/Selenium::WebDriver::Error::UnknownError:未知错误:net::ERR_CONNECTION_REFUSED
2021-09-14
1008
我尝试使用 Docker-compose 运行测试,但在使用 Selenium 时遇到了问题。 我以前没有使用过 docker,所以我很难使用它。
Dockerfile
# syntax=docker/dockerfile:1
FROM ruby:2.7.2
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 3000
# Configure the main process to run when running the image
CMD ["rails", "server", "-b", "0.0.0.0"]
docker-compose.yml
version: "3.9"
services:
db:
image: postgres
volumes:
- ./tmp/db:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/myapp
ports:
- "3000:3000"
depends_on:
- db
selenium-hub:
image: selenium/hub:3.141.59-20210913
container_name: selenium-hub
ports:
- "4444:4444"
expose:
- 4444
chrome:
image: selenium/node-chrome:3.141.59-20210913
links:
- selenium-hub
container_name: chrome
deploy:
replicas: 1
ports:
- "5900:5900"
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
- JAVA_OPTS=-Dwebdriver.chrome.whitelistedIps=
- NODE_MAX_INSTANCES=3
- NODE_MAX_SESSION=3
firefox:
image: selenium/node-firefox:3.141.59-20210913
container_name: firefox
deploy:
replicas: 1
volumes:
- /dev/shm:/dev/shm
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
rails_helper.rb
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "spec_helper"
require "rspec/rails"
# Add additional requires below this line. Rails is not loaded until this point!
require "capybara/rspec"
# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
# Seting capybara for google chrome browser
selenium_host = "http://selenium-hub:4444/wd/hub"
unless ENV['SELENIUM_HOST'].nil?
selenium_host = "http://#{ ENV["SELENIUM_HOST"] }:4444/wd/hub"
end
Capybara.register_driver :selenium_chrome do |app|
# Capybara::Selenium::Driver.new(app, browser: :chrome)
options = Selenium::WebDriver::Chrome::Options.new(args: %w[
headless no-sandbox disable-gpu window-size=1920x1080
])
Capybara::Selenium::Driver.new(
app,
browser: :remote,
desired_capabilities: :chrome,
options: options,
url: selenium_host
)
end
Capybara.server = :puma, { Silent: true }
Capybara.javascript_driver = :selenium_chrome
Capybara.save_path = "#{ Rails.root }/tmp/screenshots/"
Capybara.asset_host = 'http://localhost:3000'
Capybara.run_server = true
Capybara.configure do |config|
config.match = :prefer_exact
config.ignore_hidden_elements = true
config.visible_text_only = true
# accept clicking of associated label for checkboxes/radio buttons (css psuedo elements)
config.automatic_label_click = true
end
Capybara.always_include_port = true
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
config.filter_rails_from_backtrace!
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end
# This block must be here, do not combine with the other `before(:each)` block.
# This makes it so Capybara can see the database.
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
构建并运行容器后,当我尝试运行测试命令时:
docker-compose run web bundle exec rspec
我在使用 selenium chrome 驱动程序进行测试时遇到错误:
Failures:
1) Create new job offer when all fields are correct
Failure/Error: visit '/users/sign_in'
Selenium::WebDriver::Error::UnknownError:
unknown error: net::ERR_CONNECTION_REFUSED
(Session info: chrome=93.0.4577.63)
# #0 0x564856de3583 <unknown>
# #1 0x564856b57a38 <unknown>
# #2 0x564856b51e8d <unknown>
# #3 0x564856b43ec6 <unknown>
# #4 0x564856b44de3 <unknown>
# #5 0x564856b44192 <unknown>
# #6 0x564856b43774 <unknown>
# #7 0x564856b42543 <unknown>
# #8 0x564856b42893 <unknown>
# #9 0x564856b593fa <unknown>
# #10 0x564856bbdfaf <unknown>
# #11 0x564856baa752 <unknown>
# #12 0x564856bbd74c <unknown>
# #13 0x564856baa643 <unknown>
# #14 0x564856b80b94 <unknown>
# #15 0x564856b81b85 <unknown>
# #16 0x564856e13c6e <unknown>
# #17 0x564856e29975 <unknown>
# #18 0x564856e14bc5 <unknown>
# #19 0x564856e2afb5 <unknown>
# #20 0x564856e0984b <unknown>
# #21 0x564856e46248 <unknown>
# #22 0x564856e463c8 <unknown>
# #23 0x564856e6073d <unknown>
# #24 0x7f92b5c68609 start_thread
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/response.rb:72:in `assert_ok'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:88:in `new'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:88:in `create_response'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/default.rb:114:in `request'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:64:in `call'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/bridge.rb:167:in `execute'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/w3c/bridge.rb:567:in `execute'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/w3c/bridge.rb:59:in `get'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/navigation.rb:32:in `to'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:104:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/session.rb:278:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `call'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `visit'
# ./spec/features/create_job_spec.rb:7:in `block (2 levels) in <top (required)>'
2) The expiring date when deadline is expired yet, date is not displayed
Failure/Error: visit '/users/sign_in'
Selenium::WebDriver::Error::UnknownError:
unknown error: net::ERR_CONNECTION_REFUSED
(Session info: chrome=93.0.4577.63)
# #0 0x564856de3583 <unknown>
# #1 0x564856b57a38 <unknown>
# #2 0x564856b51e8d <unknown>
# #3 0x564856b43ec6 <unknown>
# #4 0x564856b44de3 <unknown>
# #5 0x564856b44192 <unknown>
# #6 0x564856b43774 <unknown>
# #7 0x564856b42543 <unknown>
# #8 0x564856b42893 <unknown>
# #9 0x564856b593fa <unknown>
# #10 0x564856bbdfaf <unknown>
# #11 0x564856baa752 <unknown>
# #12 0x564856bbd74c <unknown>
# #13 0x564856baa643 <unknown>
# #14 0x564856b80b94 <unknown>
# #15 0x564856b81b85 <unknown>
# #16 0x564856e13c6e <unknown>
# #17 0x564856e29975 <unknown>
# #18 0x564856e14bc5 <unknown>
# #19 0x564856e2afb5 <unknown>
# #20 0x564856e0984b <unknown>
# #21 0x564856e46248 <unknown>
# #22 0x564856e463c8 <unknown>
# #23 0x564856e6073d <unknown>
# #24 0x7f92b5c68609 start_thread
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/response.rb:72:in `assert_ok'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:88:in `new'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:88:in `create_response'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/default.rb:114:in `request'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:64:in `call'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/bridge.rb:167:in `execute'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/w3c/bridge.rb:567:in `execute'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/w3c/bridge.rb:59:in `get'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/navigation.rb:32:in `to'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:104:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/session.rb:278:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `call'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `visit'
# ./spec/features/display_expiring_date_spec.rb:22:in `block (2 levels) in <top (required)>'
3) The expiring date when deadline is not expired yet, date is displayed
Failure/Error: visit '/users/sign_in'
Selenium::WebDriver::Error::UnknownError:
unknown error: net::ERR_CONNECTION_REFUSED
(Session info: chrome=93.0.4577.63)
# #0 0x564856de3583 <unknown>
# #1 0x564856b57a38 <unknown>
# #2 0x564856b51e8d <unknown>
# #3 0x564856b43ec6 <unknown>
# #4 0x564856b44de3 <unknown>
# #5 0x564856b44192 <unknown>
# #6 0x564856b43774 <unknown>
# #7 0x564856b42543 <unknown>
# #8 0x564856b42893 <unknown>
# #9 0x564856b593fa <unknown>
# #10 0x564856bbdfaf <unknown>
# #11 0x564856baa752 <unknown>
# #12 0x564856bbd74c <unknown>
# #13 0x564856baa643 <unknown>
# #14 0x564856b80b94 <unknown>
# #15 0x564856b81b85 <unknown>
# #16 0x564856e13c6e <unknown>
# #17 0x564856e29975 <unknown>
# #18 0x564856e14bc5 <unknown>
# #19 0x564856e2afb5 <unknown>
# #20 0x564856e0984b <unknown>
# #21 0x564856e46248 <unknown>
# #22 0x564856e463c8 <unknown>
# #23 0x564856e6073d <unknown>
# #24 0x7f92b5c68609 start_thread
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/response.rb:72:in `assert_ok'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/response.rb:34:in `initialize'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:88:in `new'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:88:in `create_response'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/default.rb:114:in `request'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/http/common.rb:64:in `call'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/bridge.rb:167:in `execute'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/w3c/bridge.rb:567:in `execute'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/remote/w3c/bridge.rb:59:in `get'
# /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/navigation.rb:32:in `to'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:104:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/session.rb:278:in `visit'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `call'
# /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/dsl.rb:53:in `visit'
# ./spec/features/display_expiring_date_spec.rb:7:in `block (2 levels) in <top (required)>'
Finished in 3.82 seconds (files took 1.13 seconds to load)
39 examples, 3 failures
Failed examples:
rspec ./spec/features/create_job_spec.rb:6 # Create new job offer when all fields are correct
rspec ./spec/features/display_expiring_date_spec.rb:21 # The expiring date when deadline is expired yet, date is not displayed
rspec ./spec/features/display_expiring_date_spec.rb:6 # The expiring date when deadline is not expired yet, date is displayed
Randomized with seed 39822
ERROR: 1
非常欢迎任何帮助。
2个回答
该错误意味着 chrome 驱动程序崩溃并且不再可用。由于它是 docker 的问题,主要原因是 chrome 无法启动,因为它是一个 GUI 程序,而您正尝试从没有 GUI 的 dokcer 运行它
您应该使用任何显示服务器,例如 xvfb
阅读更多内容:
https://praveendavidmathew.medium.com/running-webdriverio-in-wsl2-windows-91d3a0dc7746
PDHide
2021-09-15
如果您运行的是 linux/macos,请尝试执行以下操作:
-
按如下方式更新
docker-compose
文件:
volumes:
- "/tmp/.X11-unix:/tmp/.X11-unix:rw"
environment:
- DISPLAY=:0
- QT_X11_NO_MITSHM=1
-
然后确保在 webdriver 中没有标记
--headless
标志 - 打开终端并运行:
xhost +
docker-compose up
使用
Selenium::WebDriver::Firefox
测试 :)
sjdonado
2022-02-16