wahei628 / Outspire

0 stars 0 forks source link

システムスペック #97

Open wahei628 opened 2 months ago

wahei628 commented 1 month ago

システムスペックについてお聞きしたいです。

現状以下のエラーが発生しています。

$ docker compose exec web bash
root@df53ec0d80ea:/outspire# bundle exec rspec spec/system/

User registration
  allows a user to sign up with valid details (FAILED - 1)
  shows validation errors when signing up with invalid details (FAILED - 2)

Failures:

  1) User registration allows a user to sign up with valid details
     Got 0 failures and 2 other errors:

     1.1) Failure/Error: visit new_user_path

          Selenium::WebDriver::Error::WebDriverError:
            unable to connect to /root/.cache/selenium/chromedriver/linux64/126.0.6478.126/chromedriver 127.0.0.1:9515
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:132:in `connect_until_stable'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:58:in `block in start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/socket_lock.rb:42:in `locked'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:55:in `start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service.rb:94:in `launch'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:328:in `service_url'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/local_driver.rb:28:in `initialize_local_driver'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/chrome/driver.rb:34:in `initialize'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `new'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `for'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver.rb:89:in `for'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:75:in `browser'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:95:in `visit'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/session.rb:281:in `visit'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/dsl.rb:52:in `call'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/dsl.rb:52:in `visit'
          # ./spec/system/users_spec.rb:5:in `block (2 levels) in <top (required)>'

     1.2) Failure/Error: raise Error::WebDriverError, cannot_connect_error_text

          Selenium::WebDriver::Error::WebDriverError:
            unable to connect to /root/.cache/selenium/chromedriver/linux64/126.0.6478.126/chromedriver 127.0.0.1:9515
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:132:in `connect_until_stable'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:58:in `block in start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/socket_lock.rb:42:in `locked'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:55:in `start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service.rb:94:in `launch'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:328:in `service_url'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/local_driver.rb:28:in `initialize_local_driver'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/chrome/driver.rb:34:in `initialize'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `new'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `for'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver.rb:89:in `for'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:75:in `browser'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:152:in `save_screenshot'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/session.rb:748:in `block in save_screenshot'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/session.rb:748:in `save_screenshot'

  2) User registration shows validation errors when signing up with invalid details
     Got 0 failures and 2 other errors:

     2.1) Failure/Error: visit new_user_path

          Selenium::WebDriver::Error::WebDriverError:
            unable to connect to /root/.cache/selenium/chromedriver/linux64/126.0.6478.126/chromedriver 127.0.0.1:9515
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:132:in `connect_until_stable'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:58:in `block in start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/socket_lock.rb:42:in `locked'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:55:in `start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service.rb:94:in `launch'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:328:in `service_url'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/local_driver.rb:28:in `initialize_local_driver'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/chrome/driver.rb:34:in `initialize'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `new'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `for'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver.rb:89:in `for'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:75:in `browser'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:95:in `visit'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/session.rb:281:in `visit'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/dsl.rb:52:in `call'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/dsl.rb:52:in `visit'
          # ./spec/system/users_spec.rb:17:in `block (2 levels) in <top (required)>'

     2.2) Failure/Error: raise Error::WebDriverError, cannot_connect_error_text

          Selenium::WebDriver::Error::WebDriverError:
            unable to connect to /root/.cache/selenium/chromedriver/linux64/126.0.6478.126/chromedriver 127.0.0.1:9515
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:132:in `connect_until_stable'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:58:in `block in start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/socket_lock.rb:42:in `locked'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service_manager.rb:55:in `start'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/service.rb:94:in `launch'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:328:in `service_url'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/local_driver.rb:28:in `initialize_local_driver'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/chrome/driver.rb:34:in `initialize'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `new'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver/common/driver.rb:47:in `for'
          # /usr/local/bundle/gems/selenium-webdriver-4.22.0/lib/selenium/webdriver.rb:89:in `for'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:75:in `browser'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/selenium/driver.rb:152:in `save_screenshot'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/session.rb:748:in `block in save_screenshot'
          # /usr/local/bundle/gems/capybara-3.40.0/lib/capybara/session.rb:748:in `save_screenshot'

Finished in 1 minute 37.26 seconds (files took 5.59 seconds to load)
2 examples, 2 failures

Failed examples:

rspec ./spec/system/users_spec.rb:4 # User registration allows a user to sign up with valid details
rspec ./spec/system/users_spec.rb:16 # User registration shows validation errors when signing up with invalid details

いろいろなサイトを参考にし修正しました。 ただ以下のようなエラーが出たりし最終的に解決に辿り着きませんでした。

Capybara::DriverNotFoundError:
            no driver called :remote_chrome was found, available drivers: :rack_test, :selenium, :selenium_headless, :selenium_chrome, :selenium_chrome_headless

以下が現状のファイルです。

rails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# 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 'capybara/rspec'
# 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.
#
# Rails.root.glob('spec/support/**/*.rb').sort.each { |f| require f }
# ↓追加
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].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
  abort e.to_s.strip
end
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_paths = [
    Rails.root.join('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://rspec.info/features/6-0/rspec-rails
  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.before(:each, type: :system) do
    driven_by :selenium_chrome
  end

  config.before(:each, type: :system, js: true) do
    driven_by :selenium_chrome
  end
#  ここまで
  # factory_bot使用のための設定
  config.include FactoryBot::Syntax::Methods
end

Gemfile

source 'https://rubygems.org'

ruby '3.2.2'

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem 'rails', '~> 7.1.3', '>= 7.1.3.2'

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem 'sprockets-rails'

# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'

# Use the Puma web server [https://github.com/puma/puma]
gem 'puma', '>= 5.0'

# Bundle and transpile JavaScript [https://github.com/rails/jsbundling-rails]
gem 'jsbundling-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'

# Bundle and process CSS [https://github.com/rails/cssbundling-rails]
gem 'cssbundling-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.1"

# 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[windows jruby]

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

gem 'sorcery'

gem 'config'

gem 'high_voltage'

gem 'dotenv-rails'

gem 'google_places'

gem 'httparty'

gem 'meta-tags'

gem 'carrierwave', '~> 3.0'

gem 'mini_magick'

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem 'debug', platforms: %i[mri windows]

  gem 'pry-byebug'

  gem 'rspec-rails'

  gem 'factory_bot_rails'

  gem 'faker'
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem 'web-console'

  gem 'letter_opener_web', '~> 2.0'

  gem 'rails_live_reload'

  gem 'rubocop', require: false
  gem 'rubocop-performance', require: false
  gem 'rubocop-rails', require: false
  gem 'rubocop-rspec'

  gem 'spring-commands-rspec'
# ↓追加
  group :test do
  gem 'capybara'
  gem 'selenium-webdriver'
  gem 'launchy', '~> 2.4.3'
end

  # 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"
end

compose.yml

services:
  db:
    image: postgres:latest
    platform: linux/amd64
    environment:
      TZ: Asia/Tokyo
      POSTGRES_PASSWORD: password
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - 5432:5432
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    tty: true
    stdin_open: true
    volumes:
      - .:/outspire
      - bundle_data:/usr/local/bundle:cached
      - node_modules:/app/node_modules
    environment:
      TZ: Asia/Tokyo
      # ↓追加
      SELENIUM_DRIVER_URL: http://selenium_chrome:4444/wd/hub
    ports:
      - "3000:3000"
    depends_on:
      - db
      # ↓追加
      - chrome
  chrome:
    image: selenium/standalone-chrome-debug
    ports:
      - 4444:4444
      # ここまで
volumes:
  postgres_data:
  bundle_data:
  node_modules:

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 = 4444
    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 = 4444
    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

users_spec.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 = 4444
    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 = 4444
    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
Tsuchiya2 commented 1 month ago

もしかしたらchromeコンテナが上手く使えていないかもしれませんね。 Rails基礎のcompose.ymlや設定周りなどを参考に取り組んでみると良いかもしれません。

wahei628 commented 1 month ago

参考にしてみます。 ありがとうございます。