内容
- 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/
./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