質問するログイン新規登録

Q&A

0回答

1231閲覧

Rspec feature specにおいてjavascriptを使った操作をテストする

komati88

総合スコア6

Ruby on Rails 6

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

RSpec

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

Docker

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

0グッド

0クリップ

投稿2021/06/11 05:32

0

0

実現したいこと

feturespecにおいてajaxを用いたいいね機能をテストしたい

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

Failures: 1) Likes like_button Failure/Error: visit new_user_session_path 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. # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/service.rb:136:in `binary_path' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/service.rb:94:in `initialize' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/service.rb:41:in `new' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/service.rb:41:in `chrome' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:299:in `service_url' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/chrome/driver.rb:40:in `initialize' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:46:in `new' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver/common/driver.rb:46:in `for' # /usr/local/bundle/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver.rb:88:in `for' # /usr/local/bundle/gems/capybara-3.35.3/lib/capybara/selenium/driver.rb:83:in `browser' # /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/support/login_macros.rb:3:in `log_in' # ./spec/features/like_spec.rb:8:in `block (2 levels) in <top (required)>' Finished in 1.63 seconds (files took 11.02 seconds to load) 1 example, 1 failure Failed examples: rspec ./spec/features/like_spec.rb:11 # Likes like_button ERROR: 1

該当コード

require 'rails_helper' RSpec.feature 'Likes', type: :feature do let(:user) { FactoryBot.create(:user)} before do create(:micropost) log_in(user) end scenario 'like_button', js: true do visit microposts_path click_link '詳細' find('.far').click expect(page).to have_css '.fas' end end

log_in(user)はspec/support/login_macros.rbに書いてあります

module LoginMacros def log_in(user) visit new_user_session_path fill_in 'user_email', with: user.email fill_in 'user_password', with: user.password click_button I18n.t('.devise.sessions.new.sign_in') end

試したこと

以下のエラーが出ており、Docker環境内にchromeが入っていないと考え,
docker-compose.rmlやcapybara.rbにselenium_chromeに関する記述をしました。
すると最初のエラーが発生しました。

Webdrivers::BrowserNotFound: Failed to find Chrome binary.

補足情報

開発環境

Dockerfile

FROM ruby:2.7.1 RUN apt-get update -qq && apt-get install -y nodejs postgresql-client RUN apt-get update && apt-get install -y curl apt-transport-https wget && \ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \ echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \ apt-get update && apt-get install -y yarn # Node.jsをインストール RUN curl -sL https://deb.nodesource.com/setup_7.x | bash - && \ apt-get install nodejs RUN mkdir /myapp WORKDIR /myapp COPY Gemfile /myapp/Gemfile COPY Gemfile.lock /myapp/Gemfile.lock RUN bundle install COPY . /myapp # 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 # Start the main process. CMD ["rails", "server", "-b", "0.0.0.0"]

docker-compose.yml

version: '3' services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data #######追加######################### environment: POSTGRES_HOST_AUTH_METHOD: 'trust' ################################### 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 environment: - "SELENIUM_DRIVER_URL=http://selenium_chrome:4444/wd/hub" selenium_chrome: image: selenium/standalone-chrome-debug logging: driver: none

spec/support/capybara.rb

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 = :selenium_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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.29%

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

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

質問する

関連した質問