LoginSignup
3
0

More than 5 years have passed since last update.

Rails5.1 SystemTest with Chromium Headless mode

Last updated at Posted at 2017-12-31

内容

  • Rails5.1でSystemTestが実装されていたそうな
  • とりあえず使ってみるか
  • Docker環境なのでheadless,disable-gpu,no-sandboxで使う
    • なぜno-sandboxが必要なの?後で調べる

準備

Debian9.2ならaptで簡単導入でした
※ Chromiumがインストールされるのでそこだけ注意

sudo apt install chromedriver
sudo apt install fonts-ipafont-gothic fonts-ipafont-mincho

TestHelperの記述

test/application_system_test_case
require "test_helper"

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: {
      args: %w(no-sandbox headless disable-gpu window-size=1280x800)
    }
  )

  driven_by :selenium, using: :chrome,
    options: { desired_capabilities: capabilities }

end

その他

chromiumでheadlessが動くか確認したときのコマンド

# コアダンプ
$ chromium --screenshot http://google.com/
$ chromium --headles --screenshot http://google.com/
$ chromium --headles --disable-gpu --screenshot http://google.com/

# 結局これでおk
$ chromium --no-sandbox --headless --disable-gpu --screenshot http://google.com/

出力
screenshot.png

./bin/rails runner testrun_selenium.rbで色々試したときのスクリプト

testrun_selenium.rb
puts Rails.env

capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
  chromeOptions: {
    # binary: "/usr/bin/chromium", # 必要なし
    args: %w(no-sandbox headless disable-gpu window-size=1280x800)
  }
)

driver = Selenium::WebDriver.for :chrome,
  desired_capabilities: capabilities

driver.navigate.to "http://google.com"

element = driver.find_element(name: 'q')
element.send_keys "Hello WebDriver!"
element.submit

puts driver.title

driver.quit

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