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

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

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

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

Ruby on Rails 6

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

RSpec

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

Docker

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

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Q&A

0回答

449閲覧

Docker環境下でRspecのsystemSpecをSeleniumを使用して動かしたい

nittamatama

総合スコア0

Capybara

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

Ruby on Rails 6

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

RSpec

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

Docker

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

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

0グッド

0クリップ

投稿2022/12/30 13:29

環境

ruby 3.0.3
Rails 6.1.7
Docker
Rspec

実現したいこと

docker環境下でRspecのsystemSpecを、ドライバーでseleniumを使用して動かしたい

発生している問題・エラーメッセージ

  • 現在docker環境内でchrome-serverという名称でseleniumのchromeイメージからchromeのブラウザ環境を構築し、Rspecのsystemスペックを実行しようとしております。

テスト自体は実行されるのですが、正常にテストが実行されない状況です。
Selenium Grid で実行状況を見てみると、ブラウザが正常に動いていない?ような状態かと思われます。
イメージ説明

エラーメッセージを見ると、実際に存在しているはずの要素が存在しないと出ております。

bash

1 % docker-compose run --env TEST_BROWSER=chrome --rm web rspec spec/system/events_spec.rb 2WARN[0000] Found orphan containers ([awesome_events-webdriver_chrome-1]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. 3[+] Running 2/0 4 ⠿ Container awesome_events-db-1 Running 0.0s 5 ⠿ Container awesome_events-chrome-server-1 Running 0.0s 6Running via Spring preloader in process 32 7 8Events 9 /event/:id ページを表示 (FAILED - 1) 10 11Failures: 12 13 1) Events /event/:id ページを表示 14 Failure/Error: expect(page).to have_selector "h5", text: event.name 15 expected to find css "h5" but there were no matches 16 17 [Screenshot Image]: /awesome_events/tmp/screenshots/failures_r_spec_example_groups_events__event__id_ページを表示_610.png 18 19 20 # ./spec/system/events_spec.rb:8:in `block (2 levels) in <top (required)>' 21 22Finished in 6.34 seconds (files took 9.48 seconds to load) 231 example, 1 failure 24 25Failed examples: 26 27rspec ./spec/system/events_spec.rb:4 # Events /event/:id ページを表示 28

いまいちSeleniumやCapybaraのことなどを理解しないまま進めてしまっていたため全く検討つかずの状態となっております。。。
こちらを正常にテスト実行できるようなアドバイスをご教示いただけますと幸いです。

該当のソースコード

docker-compose.yml

yml

1version: "3" 2services: 3 db: 4 image: mysql:8.0 5 environment: 6 MYSQL_ROOT_PASSWORD: password 7 volumes: 8 - mysql-data:/var/lib/mysql 9 - /tmp/dockerdir:/etc/mysql/conf.d/ 10 ports: 11 - 4306:3306 12 chrome-server: 13 image: selenium/standalone-chrome:96.0 14 ports: 15 - "7900:7900" 16 web: 17 build: 18 context: . 19 dockerfile: Dockerfile 20 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 21 volumes: 22 - .:/awesome_events 23 - ./vendor/bundle:/awesome_events/vendor/bundle 24 environment: 25 TZ: Asia/Tokyo 26 RAILS_ENV: development 27 ports: 28 - "3000:3000" 29 depends_on: 30 - db 31 links: 32 - chrome-server 33volumes: 34 mysql-data:

dockerfile

1FROM ruby:3.0 2 3RUN curl https://deb.nodesource.com/setup_14.x | bash 4 5 RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \ 6 && echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list 7 8RUN apt-get update -qq && apt-get install -y build-essential nodejs yarn mariadb-client 9RUN apt-get install -y vim 10 11ENV APP_PATH /awesome_events 12 13RUN mkdir $APP_PATH 14 15WORKDIR $APP_PATH 16 17COPY Gemfile $APP_PATH/Gemfile 18COPY Gemfile.lock $APP_PATH/Gemfile.lock 19RUN bundle install 20 21COPY . $APP_PATH 22 23COPY entrypoint.sh /usr/bin/ 24RUN chmod +x /usr/bin/entrypoint.sh 25ENTRYPOINT ["entrypoint.sh"] 26EXPOSE 3000 27 28CMD ["rails", "server", "-b", "0.0.0.0"]

spec/support/capybara.rb

ruby

1RSpec.configure do |config| 2 config.before(:each, type: :system) do 3 driven_by :rack_test 4 end 5 6 config.before(:each, type: :system, js: true) do 7 driven_by :selenium, using: ENV.fetch("TEST_BROWSER"), screen_size: [1400, 1400], options: { 8 browser: :remote, 9 url: "http://#{ENV.fetch("TEST_BROWSER")}-server:4444" 10 } 11 Capybara.server_host = "0.0.0.0" 12 Capybara.app_host = "http://#{ENV.fetch("HOSTNAME")}:#{Capybara.server_port}" 13 end 14end

rails_helper.rb

ruby

1# This file is copied to spec/ when you run 'rails generate rspec:install' 2require 'spec_helper' 3ENV['RAILS_ENV'] ||= 'test' 4require_relative '../config/environment' 5# Prevent database truncation if the environment is production 6abort("The Rails environment is running in production mode!") if Rails.env.production? 7require 'rspec/rails' 8# Add additional requires below this line. Rails is not loaded until this point! 9 10# Requires supporting ruby files with custom matchers and macros, etc, in 11# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are 12# run as spec files by default. This means that files in spec/support that end 13# in _spec.rb will both be required and run as specs, causing the specs to be 14# run twice. It is recommended that you do not name files matching this glob to 15# end with _spec.rb. You can configure this pattern with the --pattern 16# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. 17# 18# The following line is provided for convenience purposes. It has the downside 19# of increasing the boot-up time by auto-requiring all files in the support 20# directory. Alternatively, in the individual `*_spec.rb` files, manually 21# require only the support files necessary. 22# 23Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } 24 25# Checks for pending migrations and applies them before tests are run. 26# If you are not using ActiveRecord, you can remove these lines. 27begin 28 ActiveRecord::Migration.maintain_test_schema! 29rescue ActiveRecord::PendingMigrationError => e 30 abort e.to_s.strip 31end 32RSpec.configure do |config| 33 # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures 34 config.fixture_path = "#{::Rails.root}/spec/fixtures" 35 36 # If you're not using ActiveRecord, or you'd prefer not to run each of your 37 # examples within a transaction, remove the following line or assign false 38 # instead of true. 39 config.use_transactional_fixtures = true 40 41 # You can uncomment this line to turn off ActiveRecord support entirely. 42 # config.use_active_record = false 43 44 # RSpec Rails can automatically mix in different behaviours to your tests 45 # based on their file location, for example enabling you to call `get` and 46 # `post` in specs under `spec/controllers`. 47 # 48 # You can disable this behaviour by removing the line below, and instead 49 # explicitly tag your specs with their type, e.g.: 50 # 51 # RSpec.describe UsersController, type: :controller do 52 # # ... 53 # end 54 # 55 # The different available types are documented in the features, such as in 56 # https://relishapp.com/rspec/rspec-rails/docs 57 config.infer_spec_type_from_file_location! 58 59 # Filter lines from Rails gems in backtraces. 60 config.filter_rails_from_backtrace! 61 # arbitrary gems may also be filtered via: 62 # config.filter_gems_from_backtrace("gem name") 63end

spec/system/events_spec.rb

ruby

1require 'rails_helper' 2 3RSpec.describe "Events", type: :system, js: true do 4 scenario "/event/:id ページを表示" do 5 event = FactoryBot.create(:event) 6 visit events_url(event) 7 8 expect(page).to have_selector "h5", text: event.name 9 end 10 11end 12

Gemfile

Gemfile

1source 'https://rubygems.org' 2git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 4ruby '3.0.3' 5 6# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' 7gem 'rails', '~> 6.1.7' 8# Use mysql as the database for Active Record 9gem 'mysql2', '~> 0.5' 10# Use Puma as the app server 11gem 'puma', '~> 5.0' 12# Use SCSS for stylesheets 13gem 'sass-rails', '>= 6' 14# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker 15gem 'webpacker', '~> 5.0' 16# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks 17gem 'turbolinks', '~> 5' 18# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 19gem 'jbuilder', '~> 2.7' 20# Use Active Model has_secure_password 21# gem 'bcrypt', '~> 3.1.7' 22 23# Use Active Storage variant 24# gem 'image_processing', '~> 1.2' 25 26# Reduces boot times through caching; required in config/boot.rb 27gem 'bootsnap', '>= 1.4.4', require: false 28 29group :development, :test do 30 # Call 'byebug' anywhere in the code to stop execution and get a debugger console 31 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 32 gem 'factory_bot_rails' 33 gem 'rspec-rails' 34end 35 36group :development do 37 # Access an interactive console on exception pages or by calling 'console' anywhere in the code. 38 gem 'web-console', '>= 4.1.0' 39 # Display performance information such as SQL time and flame graphs for each request in your browser. 40 # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md 41 gem 'rack-mini-profiler', '~> 2.0' 42 gem 'listen', '~> 3.3' 43 # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 44 gem 'spring' 45end 46 47group :test do 48 # Adds support for Capybara system testing and selenium driver 49 gem 'capybara', '>= 3.26' 50 gem 'selenium-webdriver', '>= 4.0.0.rc1' 51 # Easy installation and use of web drivers to run system tests with browsers 52 gem 'webdrivers' 53 gem 'launchy' 54end 55 56# Windows does not include zoneinfo files, so bundle the tzinfo-data gem 57gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 58gem 'hamlit-rails' 59gem 'omniauth', '~> 2.1' 60gem 'omniauth-github', '~> 2.0', '>= 2.0.1' 61gem 'omniauth-rails_csrf_protection', '~> 1.0', '>= 1.0.1' 62gem 'rails-i18n', '~> 7.0', '>= 7.0.6' 63

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

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

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

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

ただいまの回答率
85.47%

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

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

質問する

関連した質問