0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails in Docker × Rspec with Capybara × selenium_driver

Last updated at Posted at 2021-05-13

#####追記
2021/05/14
この記事だけでは、system_specで、jqueryなどのチェックができるようにはなりませんでした。自分の場合は、下の記事の設定を導入し、やっとテストできるようになりました。

概要

これらの記事を参考にCapybaraのテスト実行環境を :rack_test → :seluniumへ変更致しました。(とても参考になりました。ありがとうございました!)

この記事では変更点だけを羅列しますが、注意点としましては、gem 'rspec-rails', '>= 4.0.2'こいつだけバージョンの指定が必要である、という事です。

これは2個目の記事に参考にさせていただきました!

動作環境とバージョンについて

ruby 2.6.6
Rails 6.1.3
rspec-rails 5.0.1
selenium-webdriver 3.142.7
capybara 3.35.3

結論

Gemfile
+  gem 'rspec-rails', '>= 4.0.2'
+  gem 'capybara'
+  gem 'selenium-webdriver'
docker-compose.yml
version: '3'
services:
	db:
	  image: mysql:8.0
	  environment:
	    MYSQL_ROOT_PASSWORD: password
	  ports:
	    - '3306:3306'
	  command: --default-authentication-plugin=mysql_native_password
	  volumes:
	    - mysql-data:/var/lib/mysql
	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
+     - chrome
	  stdin_open: true
	  tty: true
+ chrome:
+	  image: selenium/standalone-chrome:latest
+	  ports:
+	    - 4444:4444
	  stdin_open: true
	  tty: true
	chrome:
	  image: selenium/standalone-chrome:latest
	  ports:
	   - 4444:4444
volumes:
  mysql-data:
    driver: local
rails_helper.rb
-# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
+Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
 :
+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
 RSpec.configure do |config|
+  config.before(:each, type: :system) do
+    driven_by :rack_test
+  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
   config.include FactoryBot::Syntax::Methods
   # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
   config.fixture_path = "#{::Rails.root}/spec/fixtures"
.rspec
---require spec_helper
+--require rails_helper
 --format documentation

最後まで目を通していただきありがとうございました!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?