Set Chrome Headless Screen Size in Rails System Tests
In this article
Setting the Chrome headless screen size in Rails system tests ensures your application renders correctly under test conditions, especially when JavaScript, responsive layouts, or viewport-dependent features are involved. By default, Chrome in headless mode uses a small viewport (often 800x600), which may not reflect real user behavior or trigger responsive breakpoints accurately. This guide walks you through configuring a custom screen size in Rails system tests using Capybara and Selenium, so your tests run with a realistic browser window size.
What You'll Need
- Rails application with system tests enabled (via
rails generate system_testor manually) - Capybara gem (included by default in Rails 5.1+)
- Selenium WebDriver gem (typically
selenium-webdriver) - Chrome browser installed on the system (or ChromeDriver if running in CI)
- Chromedriver-helper or
webdriversgem to manage ChromeDriver
Step-by-Step
- Open your Rails test environment configuration file. Navigate to
config/environments/test.rbin your Rails app. This file controls settings specific to the test environment, including browser behavior. - Ensure Capybara is using Selenium with Chrome in headless mode. Confirm that your
test.rbincludes or does not override the default system test driver. Rails typically sets this intest/application_system_test_case.rb. If not already present, ensure the driver is registered. You can verify or set it intest.rbor via the test case. - Modify Chrome options to set window size in headless mode. In
config/environments/test.rb, add or update the Capybara driver registration with custom Chrome arguments. Use the--window-sizeflag to specify width and height. Example: - Set the default driver for system tests. In
test/application_system_test_case.rb, ensure the test case uses the correct driver: - Verify the configuration works. Run a sample system test that checks viewport size or responsive element visibility. For example, use JavaScript within the test to log
window.innerWidthandwindow.innerHeight: - Adjust size as needed for different breakpoints. If testing mobile responsiveness, you can register multiple drivers (e.g.,
selenium_chrome_headless_mobile) with different window sizes like--window-size=375,667for iPhone-sized screens. - Update CI configuration if applicable. In continuous integration (CI) environments like GitHub Actions, ensure Chrome and ChromeDriver are installed. Use setup commands or actions (e.g.,
actions/setup-nodeorgoogle-chrome-stable) to guarantee Chrome is available. - Restart Spring server if tests don’t reflect changes. After modifying driver settings, stop Spring with
bin/spring stopto ensure new configurations are loaded when running tests.
Capybara.register_driver :selenium_chrome_headless do |app|
chrome_opts = [
'--headless=new',
'--disable-gpu',
'--no-sandbox',
'--disable-dev-shm-usage',
'--window-size=1920,1080'
]
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
'goog:chromeOptions' => { args: chrome_opts }
)
Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: caps)
end
class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
driven_by :selenium_chrome_headless
end
If you named the driver differently, update driven_by accordingly.
it 'has correct viewport size' do
visit root_path
width = page.evaluate_script('window.innerWidth')
height = page.evaluate_script('window.innerHeight')
assert_operator width, :>=, 1900
assert_operator height, :>=, 1000
end
Common Mistakes
- Using
--window-sizewithout enabling headless mode correctly—ensure--headless=newis included. - Forgetting to restart Spring or reload the Rails server after config changes, leading to stale driver settings.
- Setting window size in non-headless Chrome but forgetting to apply it in headless mode, where default viewport is smaller.
- Using outdated ChromeDriver versions incompatible with Chrome browser version, causing silent failures.
- Not specifying
--disable-gpuand--no-sandboxin CI environments, leading to crashes.
Tips
- Use
--window-size=1920,1080for desktop testing and--window-size=375,667for mobile to match common device resolutions. - Register multiple named drivers (e.g.,
:selenium_chrome_headless_desktop,:selenium_chrome_headless_mobile) to switch between screen sizes per test. - Add
--disable-features=VizDisplayCompositorif encountering rendering glitches in headless mode. - Use the
webdriversgem to auto-manage ChromeDriver versions and avoid compatibility issues. - Log Chrome arguments during test startup for debugging: wrap driver creation in a
putsor Rails logger statement.
Troubleshooting
If the screen size does not apply:
- Check Chrome version compatibility. Run
google-chrome --versionand ensure ChromeDriver supports it. Mismatched versions cause silent failures. - Verify headless mode is active. Remove
--headless=newtemporarily to see if the browser opens visibly—this helps confirm if Chrome is launching at all. - Inspect Capybara’s current driver. In a test, add
puts page.driver.browser.capabilities['goog:chromeOptions']['args'].inspectto verify arguments are passed. - Run tests with debugging output. Set
SELENIUM_LOG_LEVEL=debugor useselenium-webdriver’s logging to trace driver initialization. - Test in non-headless mode first. Confirm window size works in regular Chrome, then apply the same flags in headless mode.
- Check for overridden drivers. Ensure no other part of the app (e.g., initializers, test helpers) redefines the Capybara driver after your configuration.
Frequently Asked Questions
Why does Chrome headless have a small screen by default?
Headless Chrome defaults to a minimal viewport (800x600) to reduce resource usage. Without explicit sizing, tests may not trigger responsive layouts.
Can I set screen size dynamically per test?
Yes. Define multiple drivers and use driven_by :custom_driver at the test level to switch sizes on demand.
Does --window-size work in headless mode?
Yes, but only with --headless=new (Chrome 112+). Older headless modes ignored window size; ensure you're using updated Chrome.
What if my CI fails with Chrome not found?
Install Chrome in your CI environment. For GitHub Actions, use actions/setup-node with Chrome, or explicitly install google-chrome-stable.
How do I test mobile views?
Register a driver with --window-size=375,667 and device-specific flags like --user-agent='Mozilla/5.0...Mobile' to simulate mobile browsing.
Is --no-sandbox safe in CI?
Yes, in controlled CI environments. It’s required when running Chrome as root (common in containers), but not recommended for production or user-facing systems.