目指していること
Railsで作ったアプリにDockerを導入し + CircleCIを連携させ、テストを自動化させたい。
困っていること
まず、Docker上でrspecのテストを走らせた際に、エラーが起きる。(ローカルではエラーは起きない)
エラー文は下記。
Selenium::WebDriver::Error::WebDriverError: Unable to find chromedriver. Please download the server from https://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH. More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.
試したこと
Docker上ではchromeがないが故に発生しているエラーだと判断し、Dockerイメージとしてdocker-compose.ymlに以下を導入した。
その他の必要そうな記述に関しても、ググりながら記述しました。知識が浅いので、間違いがあるかもしれません。。
image: selenium/standalone-chrome-debug
コード
Gemfile
1group :development, :test do 2 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 3 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 4 gem 'factory_bot_rails' 5 gem 'rspec-rails', '<5.0.0' 6end 7 8group :test do 9 # Adds support for Capybara system testing and selenium driver 10 gem 'capybara', '>= 2.15' 11 gem 'selenium-webdriver' 12 gem 'webdrivers' 13 gem 'minitest' 14 gem 'minitest-reporters' 15 gem 'guard' 16 gem 'guard-minitest' 17end 18...(略)
docker-compose.yml ↓
version: '3.9' services: app: build: context: . ports: - "3000:3000" command: bash -c "rm -f tmp/pids/server.pid && bundle exec puma -C config/puma.rb" volumes: - .:/myapp - log-data:/myapp/log environment: - RAILS_ENV=development - DB_HOST=db - "SELENIUM_DRIVER_URL=http://selenium_chrome:4444/wd/hub" depends_on: - db selenium_chrome: image: selenium/standalone-chrome-debug logging: driver: none db: image: mysql:5.7 volumes: - db-data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: MYSQL_ALLOW_EMPTY_PASSWORD: "yes" nginx: build: context: containers volumes: - public-data:/myapp/public - tmp-data:/myapp/tmp ports: - 80:80 depends_on: - app volumes: public-data: tmp-data: log-data: db-data:
Dockerfile
1FROM ruby:2.6.2 2RUN apt-get update -qq && apt-get install -y nodejs 3WORKDIR /myapp 4COPY Gemfile /myapp/Gemfile 5COPY Gemfile.lock /myapp/Gemfile.lock 6RUN gem install bundler 7RUN bundle install 8 9# Add a script to be executed every time the container starts. 10COPY entrypoint.sh /usr/bin/ 11RUN chmod +x /usr/bin/entrypoint.sh 12ENTRYPOINT ["entrypoint.sh"] 13EXPOSE 3000 14 15RUN mkdir -p tmp/sockets 16RUN mkdir tmp/pids 17CMD bundle exec puma
spec_helper.rb↓
require 'capybara/rspec' RSpec.configure do |config| config.expect_with :rspec do |expectations| config.before(:each, type: :system) do driven_by :selenium_chrome_headless end expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end config.shared_context_metadata_behavior = :apply_to_host_groups end
rails_helper.rb
require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' begin ActiveRecord::Migration.maintain_test_schema! rescue ActiveRecord::PendingMigrationError => e puts e.to_s.strip exit 1 end RSpec.configure do |config| config.fixture_path = "#{::Rails.root}/spec/fixtures" config.use_transactional_fixtures = true config.infer_spec_type_from_file_location! config.filter_rails_from_backtrace! require 'capybara/rspec' end
capybara.rb↓
require 'capybara/rspec' require 'selenium-webdriver' Capybara.register_driver :chrome_headless do |app| options = ::Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1400,1400') Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end Capybara.javascript_driver = :chrome_headless RSpec.configure do |config| config.before(:each, type: :system) do driven_by :rack_test end config.before(:each, type: :system, js: true) do if ENV["SELENIUM_DRIVER_URL"].present? driven_by :selenium, using: :chrome, options: { browser: :remote, url: ENV.fetch("SELENIUM_DRIVER_URL"), desired_capabilities: :chrome } else driven_by :selenium_chrome_headless end end end
.rspec↓
--require spec_helper --format documentation --color
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/07/13 14:35