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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

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

Ruby on Rails

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

selenium

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

Q&A

1回答

2312閲覧

Rspac 結合テスト chromedriverに接続できない

ricopin

総合スコア2

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

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

Ruby on Rails

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

selenium

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

0グッド

0クリップ

投稿2021/11/10 22:13

編集2021/11/12 14:51

前提・実現したいこと

ruby on rails にて予約管理アプリを制作しています。

Rspecでユーザー登録に関する結合テストを実行したところ、chromedriverに接続できずテストが通りません。

Selenium::WebDriver::Error::WebDriverError: . . unable to connect to ...chromedriver

というエラーが出てしまい、対処法を探しているのですがなかなか解決に至りません。

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

% bundle exec rspec spec/system/users_spec.rb Users ユーザー新規登録ができるとき 正しい情報を入力すればユーザー新規登録ができてトップページに移動する (FAILED - 1) #←今回のエラーが起きているテスト ユーザー新規登録ができないとき 誤った情報ではユーザー新規登録ができずに新規登録ページへ戻ってくる #←テストコード未記入のため正常に通る Failures: 1) Users ユーザー新規登録ができるとき 正しい情報を入力すればユーザー新規登録ができてトップページに移動する Got 0 failures and 2 other errors: 1.1) Failure/Error: visit new_user_registration_path Selenium::WebDriver::Error::WebDriverError: unable to connect to /Users/.rbenv/versions/2.6.5/bin/chromedriver 127.0.0.1:9515 # ./spec/system/users_spec.rb:10:in `block (3 levels) in <top (required)>' 1.2) Failure/Error: raise Error::WebDriverError, cannot_connect_error_text Selenium::WebDriver::Error::WebDriverError: unable to connect to /Users/.rbenv/versions/2.6.5/bin/chromedriver 127.0.0.1:9515

該当のソースコード

./spec/system/users_spec.rb

ruby

1require 'rails_helper' 2 3RSpec.describe "Users", type: :system do 4 before do 5 @user = FactoryBot.build(:user) 6 end 7 context 'ユーザー新規登録ができるとき' do 8 it '正しい情報を入力すればユーザー新規登録ができてトップページに移動する' do 9 # 新規登録ページへ移動する 10 visit new_user_registration_path 11 # ユーザー情報を入力する 12 fill_in 'last_name', with: @user.last_name 13 fill_in 'first_name', with: @user.first_name 14 fill_in 'Email', with: @user.email 15 fill_in 'Password', with: @user.password 16 fill_in 'Password confirmation', with: @user.password_confirmation 17 # サインアップボタンを押すとユーザーモデルのカウントが1上がることを確認する 18 expect{ 19 find('input[name="commit"]').click 20 }.to change { User.count }.by(1) 21 # トップページへ遷移したことを確認する 22 expect(current_path).to eq(root_path) 23 # マイページへ遷移するボタンが表示されていないことを確認する 24 expect(page).to have_no_content('あなたのページ') 25 end 26 end 27 context 'ユーザー新規登録ができないとき' do 28 it '誤った情報ではユーザー新規登録ができずに新規登録ページへ戻ってくる' do 29 # 未記入 30 31 end 32 end 33 34end

Gemfire

source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } ruby '2.6.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.0' # Use mysql as the database for Active Record gem 'mysql2', '>= 0.4.4' # Use Puma as the app server gem 'puma', '~> 3.11' # Use SCSS for stylesheets gem 'sass-rails', '~> 5' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 4.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.7' # Use Redis adapter to run Action Cable in production # gem 'redis', '~> 4.0' # Use Active Model has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Active Storage variant # gem 'image_processing', '~> 1.2' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'rspec-rails', '~> 4.0.0' gem 'factory_bot_rails' gem 'faker' end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' end group :test do # Adds support for Capybara system testing and selenium driver gem 'capybara', '>= 2.15' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'devise' gem 'rails-i18n' gem 'devise-i18n' gem 'simple_calendar', '~> 2.0' gem 'active_hash' gem 'pry-rails' gem 'jquery-rails'

./spec/rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'rspec/rails' require 'devise' # Add additional requires below this line. Rails is not loaded until this point! # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end # in _spec.rb will both be required and run as specs, causing the specs to be # run twice. It is recommended that you do not name files matching this glob to # end with _spec.rb. You can configure this pattern with the --pattern # option on the command line or in ~/.rspec, .rspec or `.rspec-local`. # # The following line is provided for convenience purposes. It has the downside # of increasing the boot-up time by auto-requiring all files in the support # directory. Alternatively, in the individual `*_spec.rb` files, manually # require only the support files necessary. # # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.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 RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. config.use_transactional_fixtures = true # You can uncomment this line to turn off ActiveRecord support entirely. # config.use_active_record = false # RSpec Rails can automatically mix in different behaviours to your tests # based on their file location, for example enabling you to call `get` and # `post` in specs under `spec/controllers`. # # You can disable this behaviour by removing the line below, and instead # explicitly tag your specs with their type, e.g.: # # RSpec.describe UsersController, type: :controller do # # ... # end # # The different available types are documented in the features, such as in # https://relishapp.com/rspec/rspec-rails/docs config.infer_spec_type_from_file_location! # Filter lines from Rails gems in backtraces. config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") #config.include FactoryBot::Syntax::Methods config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::IntegrationHelpers, type: :request end

試したこと1 capybaraを最新のものに更新→エラーに変化なし

gem 'capybara', '>= 3.36.0'

webで情報を調べていたところ、capybaraのバージョンを最新(2.15.0 ⇒ 3.25.0)にしたら解決したとありました。
(2019年の記事)
https://qiita.com/omokawa_yasu/items/6f52ed20c88eef735dc0
Capibaraのバージョンを調べたところ、Version 3.36.0(Release date: 2021-10-24)が最新のようです。

https://github.com/teamcapybara/capybara/blob/master/History.md

ジェムファイルを書き換え、bundle installしましたが、エラーに変化はありませんでした。

capybaraのバージョン(2.15.0)を使用している他のアプリケーションの結合テストを実行したところ問題なくテストが通ったので、Capibaraのバージョンには関係ないと判断しました。

試したこと2  rails_helper.rbに新たに記述したものを削除→エラーに変化なし

今までに作成したアプリケーションと違うところは何か?と考え、今回のアプリケーションで初めて記述した
rails_helper.rbの

config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::IntegrationHelpers, type: :request

に注目しました。

コントローラーの単体テストの際に、authenticate_user!を通すためにsign_inメソッドを使用するためにこの記述をしました。
https://github.com/heartcombo/devise/wiki/How-To:-Test-controllers-with-Rails-(and-RSpec)#controller-specsを参考にしています。
Githubも少し古いものだったので、この記述が悪さをしているのではと考え、消して(コメントアウトして)実行してみましたが、、、エラー状態は変わりませんでした…。

試そうとしているが躊躇していること  'chromedriver-helper'をインストール

今回の件でたくさんのWeb上の記事を拝見しているのですが、ダントツで多いのが

gem 'chromedriver-helper'

をインストールするというもの。
そもそもエラー内容が

unable to connect to ...chromedriver

というようにchromedriverに接続できないとあるので、'chromedriver-helper'を入れるというのが特効薬のように感じるのですが…よくよく調べていると、この'chromedriver-helper'というgem、2019年にサポートを終了しているとのことでした。
代わりに用意されたのが、今回のアプリに既に入っている

gem 'webdrivers'

きちんと最新のものを利用しているのになぜ…という感じですが、'chromedriver-helper'をインストールして解決した、という記事を見ると試してみたくもなりつつ、サポート終了しているものを安易にインストールするのもなあと躊躇しています。

何か不要な記述をしているのではと色々と考えているのですが、なかなか解決に至らず質問させていただきました。私のエラーの解釈が違うなど、些細なことでも構いませんので、ご教授いただきたいです。どうぞよろしくお願いします。

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

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

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

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

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

guest

回答1

0

chromedriver-helperはサポート終了したので、webdriversが良さそうです。

こちらの記事が参考できそうです。
http://vdeep.net/rubyonrails-rspec-factorybot-capybara

投稿2021/11/16 06:31

heroyct

総合スコア434

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

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

ricopin

2021/11/27 11:04 編集

ご回答いただきありがとうございます! お返事が大変遅くなり申し訳ありませんでした…。 やはり、webdriversの方がいいのですね…!chromedriver-helperへのモヤモヤが断ち切れました!ありがとうございます! ご共有いただいた記事をもとに修正を行ってみました。 記事に沿ってgemfileを変更。 【「selenium-webdriver」は「Webdrivers」で代替できるので削除する】 とのことでしたので ``` group :development, :test do gem 'rspec-rails' gem 'factory_bot_rails' end group :test do gem 'capybara' # gem 'selenium-webdriver' ←(コメントアウト) gem 'webdrivers' end ``` のように変更し、テスト実行しました。 今度のエラーは ``` LoadError: cannot load such file -- selenium/webdriver System test integration requires Rails >= 5.1 and has a hard dependency on a webserver and `capybara`, please add capybara to your Gemfile and configure a webserver (e.g. `Capybara.server = :webrick`) before attempting to use system specs. ``` とのことで、やはり gem 'selenium-webdriver' が必要と判断し、もう一度記述(コメントアウトをはず)しました。 結果、エラー内容はふりだしに戻った状態です。 せっかくの参考記事のご共有をいただいたにも関わらず、私の勉強不足故、解決に至らず申し訳ございません… webdriversについてもっと調べてみようと思います!ありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問