現在『Everyday Rails RspecによるRailsテスト入門』でRspecを学習している者です。
タイトルの通り、system specを動かしたいのですが、様々な手法を試みたものの動かすことができず質問させていただきます。
selenium-webdriver
というgemを使用して、dockerの本番環境以外でchromeを使用できるようにして、system specが実行できるようにしようとしています。
しかしながら、
NameError:
uninitialized constant Selenium
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome(
上記のようなエラーが続き解決することができません。
参考記事
https://qiita.com/at-946/items/e96eaf3f91a39d180eb3
環境
Rails 7.0.2.3
Ruby ruby 3.1.2
Docker
発生しているエラー
root@b09d1b2d36f2:/everydayrails-rspec-jp-2022# rspec spec/system/tasks_spec.rb
Tasks
ユーザーがタスクの状態を切り替える (FAILED - 1)
Failures:
1) Tasks ユーザーがタスクの状態を切り替える
Got 0 failures and 2 other errors:
1.1) Failure/Error:
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions' => {
'args' => [
'no-sandbox',
'headless',
'disable-gpu',
'window-size=1680,1050'
]
}
)
NameError:
uninitialized constant Selenium
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome(
^^^^^^^^^^
# ./spec/support/capybara.rb:20:in `block in <main>'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/session.rb:105:in `driver'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/session.rb:91:in `initialize'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara.rb:421:in `new'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara.rb:421:in `block in session_pool'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara.rb:317:in `current_session'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/dsl.rb:46:in `page'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/dsl.rb:52:in `visit'
# ./spec/system/tasks_spec.rb:11:in `block (2 levels) in <top (required)>'
1.2) Failure/Error:
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions' => {
'args' => [
'no-sandbox',
'headless',
'disable-gpu',
'window-size=1680,1050'
]
}
)
NameError:
uninitialized constant Selenium
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome(
^^^^^^^^^^
# ./spec/support/capybara.rb:20:in `block in <main>'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/session.rb:105:in `driver'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/session.rb:91:in `initialize'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara.rb:421:in `new'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara.rb:421:in `block in session_pool'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara.rb:317:in `current_session'
# /usr/local/bundle/gems/capybara-3.38.0/lib/capybara/dsl.rb:46:in `page'
Finished in 0.76328 seconds (files took 8.3 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/system/tasks_spec.rb:4 # Tasks ユーザーがタスクの状態を切り替える
Gemfile
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "7.0.2.3"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
gem 'mysql2', '~> 0.5'
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem 'importmap-rails'
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use Sass to process CSS
gem "sassc-rails"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
gem "image_processing", "~> 1.2"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ], require: false
gem 'rspec-rails' # add in chapter 2
gem 'factory_bot_rails' # add in chapter 4
end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
gem 'faker', require: false # for sample data in development
end
group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
# TODO: waiting for release https://github.com/teamcapybara/capybara/pull/2520
gem "capybara", '~> 3.28'
gem 'selenium-webdriver', '~> 4.6', '>= 4.6.1'
# gem "webdrivers"
gem 'launchy' # add in chapter 6
gem 'vcr' # add in chapter 10
gem 'webmock' # add in chapter 10
end
gem 'devise'
gem 'net-imap'
gem 'net-pop'
gem 'net-smtp'
gem 'activestorage-validator'
gem 'geocoder'
gem 'pry-rails'
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'
# 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 Devise::Test::ControllerHelpers, type: :controller
end
docker-compose.yml
version: "3"
services:
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: password
volumes:
- mysql-data:/var/lib/mysql
- /tmp/dockerdir:/etc/mysql/conf.d/
ports:
- 4306:3306
web:
build:
context: .
dockerfile: Dockerfile
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
volumes:
- .:/everydayrails-rspec-jp-2022
- ./vendor/bundle:/everydayrails-rspec-jp-2022/vendor/bundle
environment:
TZ: Asia/Tokyo
RAILS_ENV: development
SELENIUM_REMOTE_URL: http://chrome:4444/wd/hub
ports:
- "3000:3000"
depends_on:
- db
- chrome
chrome:
image: selenium/standalone-chrome:latest
ports:
- 4444:4444
volumes:
mysql-data:
capybara.rb
RSpec.configure do |config|
config.before(:each, type: :system) do
driven_by :remote_chrome
Capybara.server_host = IPSocket.getaddress(Socket.gethostname)
Capybara.server_port = 3000
Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}"
end
config.before(:each, type: :system, js: true) do
driven_by :remote_chrome
Capybara.server_host = IPSocket.getaddress(Socket.gethostname)
Capybara.server_port = 3000
Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}"
end
end
# Chrome
Capybara.register_driver :remote_chrome do |app|
url = 'http://chrome:4444/wd/hub'
caps = ::Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions' => {
'args' => [
'no-sandbox',
'headless',
'disable-gpu',
'window-size=1680,1050'
]
}
)
Capybara::Selenium::Driver.new(app, browser: :remote, url: url, desired_capabilities: caps)
end
実行specファイル
require 'rails_helper'
RSpec.describe "Tasks", type: :system do
scenario "ユーザーがタスクの状態を切り替える", js: true do
user = FactoryBot.create(:user)
project = FactoryBot.create(:project,
name: "Rspec Tutorial",
owner: user)
task = project.tasks.create(name: "finish rspce tutorial")
visit root_path
click_link "Sign in"
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Log in"
click_link "Rspec Tutorial"
check "finish rspce tutorial"
expect(page).to have_css "label#task_#{task.id}.completed"
expect(task.reload).to be_completed
uncheck "finish rspce tutorial"
expect(page).to_not have_css "label#task_#{task.id}.completed"
expect(task.reload).to_not be_completed
end
end
すみませんどなたかご教示いただけますと幸いです。。