設定
Gemfile
gem 'selenium-webdriver'
spec/rails_helper.rb
require 'selenium-webdriver'
# それぞれのUserAgentを持ったドライバを定義しておく
Capybara.register_driver :piyo_driver do |app|
useragent = "piyo"
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
chrome_options: {
args: %W(headless disable-gpu window-size=1680,1050 user-agent=#{useragent})
}
)
Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)
end
spec/features/piyo_spec.rb
require 'rails_helper'
# System Spec ができるようにまだしてない... :cry:
describe 'サンプルテスト' do
it 'piyoというUserAgentでアクセスする', driver: :piyo_driver do
# piyoというUserAgentが設定された状態でのテストができる
end
end
参考
- Configuring User Agents with Capybara + Selenium Webdriver « Plataformatec Blog
- GitHubGist - Configure User Agent for selenium webdriver in Capybara
- RSpec の feature spec でヘッドレス Chrome を使う - Speee DEVELOPER BLOG
- ruby - How to use headless chrome with capybara and selenium - Stack Overflow
- capybara/README.md at master · teamcapybara/capybara