Docker + Rails(RSpec)でアプリを作成しています。
docker + system_spec の 記事をいくつか見ていた中で
Rails on DockerでRSpecのSystem testをSelenium Dockerを使ってやってみた。 - Qiita
がわかりやすそうなので、参考にしながらテストを書いていますが、
save_and_open_page
を使うjs: true
オプションを付加する
時にテストがパスしません(上記2つを使わないとテストはパスします)。
Ruby rspec VCR でVCR::Errors::UnhandledHTTPRequestError が... - Qiita
といった記事があったので内容を見てみましたが、関係ないような気がします(m_ m)
もしわかる方がいらっしゃればご教授いただきますようよろしくお願いいたします(m _m)
動作環境
Ruby: 2.5.7
Rails: 5.1.7
rspec-rails: 4.0.1
capybara: 3.32.2
Docker for Mac: 2.2.0.5
エラー内容(2)
- 下記のコードでは
Launchy::CommandNotFoundError:
が発生する
ruby
1require 'rails_helper' 2 3RSpec.describe "sample test", type: :system do 4 it 'xxx' do 5 save_and_open_page 6 visit root_path 7 end 8end 9
bash
1Failure/Error: save_and_open_page 2Launchy::CommandNotFoundError: 3 Unable to find a browser command. 4 If this is unexpected, Please rerun with environment variable LAUNCHY_DEBUG=true or the '-d' commandline option 5 and file a bug at https://github.com/copiousfreetime/launchy/issues/new
js: true
オプションを付加すると下記のエラーが発生する。save_and_open_page
をコメントアウトしてもFailure/Error:
の対象が変わるだけ(長い)
ruby
1require 'rails_helper' 2 3RSpec.describe "sample test", type: :system, js: true do 4 it 'xxx' do 5 save_and_open_page 6 visit root_path 7 end 8end 9
bash
1sample test 2 xxx (FAILED - 1) 3 4Failures: 5 6 1) sample test xxx 7 Got 0 failures and 2 other errors: 8 9 1.1) Failure/Error: save_and_open_page 10 11 VCR::Errors::UnhandledHTTPRequestError: 12 13 14 ================================================================================ 15 An HTTP request has been made that VCR does not know how to handle: 16 POST http://chrome:4444/wd/hub/session 17 18 There is currently no cassette in use. There are a few ways 19 you can configure VCR to handle this request: 20 21 * If you're surprised VCR is raising this error 22 and want insight about how VCR attempted to handle the request, 23 you can use the debug_logger configuration option to log more details [1]. 24 * If you want VCR to record this request and play it back during future test 25 runs, you should wrap your test (or this portion of your test) in a 26 `VCR.use_cassette` block [2]. 27 * If you only want VCR to handle requests made while a cassette is in use, 28 configure `allow_http_connections_when_no_cassette = true`. VCR will 29 ignore this request since it is made when there is no cassette [3]. 30 * If you want VCR to ignore this request (and others like it), you can 31 set an `ignore_request` callback [4]. 32 33 [1] https://www.relishapp.com/vcr/vcr/v/5-1-0/docs/configuration/debug-logging 34 [2] https://www.relishapp.com/vcr/vcr/v/5-1-0/docs/getting-started 35 [3] https://www.relishapp.com/vcr/vcr/v/5-1-0/docs/configuration/allow-http-connections-when-no-cassette 36 [4] https://www.relishapp.com/vcr/vcr/v/5-1-0/docs/configuration/ignore-request 37 ================================================================================ 38: 39省略(収まりきらない) 40: 41 42 43 1.2) Failure/Error: raise VCR::Errors::UnhandledHTTPRequestError.new(vcr_request) 44 45 VCR::Errors::UnhandledHTTPRequestError: 46 47 48 ================================================================================ 49 An HTTP request has been made that VCR does not know how to handle: 50 POST http://chrome:4444/wd/hub/session 51 52 There is currently no cassette in use. There are a few ways 53 you can configure VCR to handle this request: 54 55 * If you're surprised VCR is raising this error 56 and want insight about how VCR attempted to handle the request, 57: 58省略(収まりきらない) 59: 60
期待する動作
- save_and_open_page を実行、スクリーンショットが保存されるようになること
- js: trueオプションを付加してもテストがパスするようになること
- ブラウザ操作を視認で確認できるようになること
試したこと
- Rails on DockerでRSpecのSystem testをSelenium Dockerを使ってやってみた。 - Qiita
- DockerでRSpecのブラウザ操作を見る | Futurismo
を参考にファイルを作成後
docker-compse run -e LAUNCH_BROWSER=true web rspec spec/system/sample_spec.rb
を実行しました
該当しそうなコード6つ
- Gemfile(1 / 6)
gemfile
1source 'https://rubygems.org' 2 3git_source(:github) do |repo_name| 4 repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") 5 "https://github.com/#{repo_name}.git" 6end 7: 8: 9 10group :development, :test do 11 gem 'awesome_print' 12 gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] 13 gem 'factory_bot_rails' 14 gem 'rspec-rails' 15 gem 'rails-flog', require: 'flog' 16 17end 18 19group :development do 20 gem 'bullet' 21 gem 'pry-byebug' 22 gem 'pry-rails' 23 gem 'letter_opener_web' 24 gem 'listen', '>= 3.0.5', '< 3.2' 25 gem 'rubocop', require: false 26 gem 'rubocop-performance' 27 gem 'rubocop-rails', require: false 28 gem 'rubocop-rspec', require: false 29 gem 'solargraph' 30 gem 'spring' 31 gem 'spring-commands-rspec' 32 gem 'spring-watcher-listen', '~> 2.0.0' 33 gem 'web-console', '>= 3.3.0' 34end 35 36group :test do 37 gem 'capybara' 38 gem 'launchy' 39 gem 'shoulda-matchers', 40 git: 'https://github.com/thoughtbot/shoulda-matchers.git', 41 branch: 'rails-5' 42 gem 'simplecov' 43 gem 'vcr' 44 gem 'webdrivers' 45 gem 'webmock' 46 gem 'database_cleaner' 47 gem 'selenium-webdriver' 48 # gem 'chromedriver-helper' 49end 50 51gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] 52
- docker-compose.yml(2 / 6)
docker
1# docker-compose.yml 2version: '3.7' 3 4services: 5 db: 6 image: mysql:5.7 7 environment: 8 MYSQL_ROOT_PASSWORD: password 9 MYSQL_DATABASE: root 10 ports: 11 - "4306:3306" 12 volumes: 13 - db-data:/var/lib/mysql 14 web: 15 init: true 16 tty: true 17 stdin_open: true 18 build: . 19 command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" 20 # 21 volumes: 22 - .:/webapp 23 ports: 24 - "3000:3000" 25 # 26 links: 27 - db 28 - chrome 29 chrome: 30 # image: selenium/standalone-chrome:latest 31 # 視認で確認する 32 image: selenium/standalone-chrome-debug:latest 33 ports: 34 - 4444:4444 35 # 視認で確認する 36 - 5901:5900 37 38volumes: 39 db-data:
- rails_helper.rb(3 / 6)
ruby
1require 'spec_helper' 2ENV['RAILS_ENV'] ||= 'test' 3require File.expand_path('../config/environment', __dir__) 4abort('The Rails environment is running in production mode!') if Rails.env.production? 5 6require 'simplecov' 7require 'capybara/rspec' 8require 'selenium-webdriver' 9require 'factory_bot' 10require 'shoulda-matchers' 11require 'vcr' 12require 'spec_helper' 13require 'rspec/rails' 14 15Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } 16 17begin 18 ActiveRecord::Migration.maintain_test_schema! 19rescue ActiveRecord::PendingMigrationError => e 20 puts e.to_s.strip 21 exit 1 22end 23RSpec.configure do |config| 24 config.fixture_path = "#{::Rails.root}/spec/fixtures" 25 config.use_transactional_fixtures = false 26 config.infer_spec_type_from_file_location! 27 config.filter_rails_from_backtrace! 28 29 config.before(:suite) do 30 DatabaseCleaner.strategy = :transaction 31 DatabaseCleaner.clean_with(:truncation) 32 end 33 34 config.include Devise::Test::IntegrationHelpers, type: :system 35end
- spec_helper.rb(4 / 6)
ruby
1require 'simplecov' 2SimpleCov.start 'rails' 3 4RSpec.configure do |config| 5 config.filter_run focus: true 6 config.run_all_when_everything_filtered = true 7 config.expect_with :rspec do |expectations| 8 expectations.include_chain_clauses_in_custom_matcher_descriptions = true 9 end 10 11 config.mock_with :rspec do |mocks| 12 mocks.verify_partial_doubles = true 13 end 14 15 config.shared_context_metadata_behavior = :apply_to_host_groups 16end 17
- capybara.rb(5 / 6)
ruby
1require 'capybara/rspec' 2require 'selenium-webdriver' 3require 'vcr' 4 5Capybara.register_driver :remote_chrome do |app| 6 url = "http://chrome:4444/wd/hub" 7 caps = ::Selenium::WebDriver::Remote::Capabilities.chrome( 8 "goog:chromeOptions" => { 9 "args" => [ 10 "no-sandbox", 11 # "headless", 12 "disable-gpu", 13 "window-size=1680,1050" 14 ] 15 } 16 ) 17 Capybara::Selenium::Driver.new(app, browser: :remote, url: url, desired_capabilities: caps) 18end 19 20RSpec.configure do |config| 21 config.before(:each, type: :system) do 22 driven_by :rack_test 23 end 24 25 config.before(:each, type: :system, js: true) do 26 driven_by :remote_chrome 27 Capybara.server_host = IPSocket.getaddress(Socket.gethostname) 28 Capybara.server_port = 3000 29 Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}" 30 end 31 # ~~~ 32end 33
- スペックファイル(6 / 6)
ruby
1# spec/system/sample_spec.rb 2require 'rails_helper' 3 4RSpec.describe "sample test", type: :system, js: true do 5 it 'xxx' do 6 save_and_open_page 7 visit root_path 8 end 9end 10
あなたの回答
tips
プレビュー