質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Capybara

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

Q&A

1回答

2114閲覧

Docker上でrspecを実行するとWebDriverErrorが発生する

moonlight4_6_17

総合スコア12

Capybara

Capybaraは、 Rubyで開発されているWebアプリケーションテストフレームワークです。Webブラウザ不要でブラウザ上のユーザー操作及びJavaScriptの挙動を自動化することができます。

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Docker

Dockerは、Docker社が開発したオープンソースのコンテナー管理ソフトウェアの1つです

0グッド

0クリップ

投稿2021/07/12 07:06

目指していること

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

rspecを実行するRubyのイメージの中でchromiumをインストールする必要があると思います。Docker Composeにそれを追加するのではなく、RubyのDockerfileの中でchromiumをインストールしてみてはいかがでしょうか?

投稿2021/07/13 03:00

inductor

総合スコア428

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

moonlight4_6_17

2021/07/13 14:35

inductorさん ありがとうございます! おっしゃる通り、Dockerfileにインストールする記述加えてみることにします!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問