3
3

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 5 years have passed since last update.

Rails+Rspec+Headless Chromeの設定

3
Last updated at Posted at 2019-07-19

試した環境:

  • rails 5.1.6.2
  • rspec-rails 3.8.2
  • capybara 3.26.0
  • selenium-webdriver 3.142.3
  • webdrivers 4.1.1
  • Google Chrome 75
Gemfile
group :test do
  gem 'rspec-rails'
  gem 'capybara'
  gem 'webdrivers'
end
spec_helper.rb
Capybara.register_driver :headless_chrome do |app|
  chrome_options = Selenium::WebDriver::Chrome::Options.new
  chrome_options.args << '--headless'

  driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: chrome_options)
  driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(2000, 3000)
  driver
end

Capybara.javascript_driver = :headless_chrome
Capybara.default_max_wait_time = 5
Capybara.server = :puma, { Silent: true }

2019年5月以降、Rspecのfeature specがまともに動かないことで悩まされてきました。Chrome 74では謎のネットワークエラーが発生し、Chrome 75ではブラウザーがヘッドレスにならない、というものです。上記の設定により無事動くようになりました。

参考にしたのは、Rails 5.2のsystem testのソースコードです。

Rails 5.1.7と5.2以降 1 では、feature specではなく system spec を使うほうがよいと思いますが、なかなかRailsをバージョンアップできない環境もありますので。

Rails 5.0 でも動く

Rails 5.0のアプリケーションでも試してみたところうまくいきました。

  • rails 5.0.7.2
  • rspec-rails 3.5.2
  • capybara 2.16.0
  • selenium-webdriver 3.14.0
  • webdrivers 4.1.1
  • Google Chrome 75

Cannot call non W3C standard command while in W3C mode というエラーが出る場合は、上記の chrome_options にオプションを加えます。

spec_helper.rb
  chrome_options.add_option(:w3c, false)
  1. これより前では、system testでCapybara 3が使えない。

3
3
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
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?