LoginSignup
47
28

More than 5 years have passed since last update.

RailsにCapybara + Seleniumを導入してChrome Headlessモードで実行する手順

Last updated at Posted at 2018-02-05

Rails + Capybara + Selenium でフロントエンド側のテストをChrome Headlessモードで実行する手順を残しておきます。

手順

  • Gemfileに以下を追加
gem 'capybara'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
  • bundle install実行
  • spec_helperに以下を追加
require 'selenium-webdriver'
require 'capybara/rspec'

# Capybara自体の設定、ここではどのドライバーを使うかを設定しています
Capybara.configure do |capybara_config|
  capybara_config.default_driver = :selenium_chrome
  capybara_config.default_max_wait_time = 10 # 一つのテストに10秒以上かかったらタイムアウトするように設定しています
end
# Capybaraに設定したドライバーの設定をします
Capybara.register_driver :selenium_chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument('headless') # ヘッドレスモードをonにするオプション
  options.add_argument('--disable-gpu') # 暫定的に必要なフラグとのこと
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :selenium_chrome
  • これでheadlessモードで実行できます
  • ※Chrome 59以上でないとheadlessモードは実行できません。。。

chromedriverの更新方法

  • 下記ファイルを削除します 
    • .chromedriver-helper
  • その後run chromedriver-update実行で更新できます。
  • ※ Gemfile でchromedriver-helper を入れている場合は
    • bundle exec chromedriver-update

追記

  • 2月9日
    • コードに簡単なコメントを追加しました

参照

https://github.com/teamcapybara/capybara#selenium
https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
https://developers.google.com/web/updates/2017/04/headless-chrome?hl=ja

47
28
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
47
28