User Agents

Web browsers and everyday web tech, explained

Set Chrome Headless Screen Size in Rails System Tests

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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_test or 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 webdrivers gem to manage ChromeDriver

Step-by-Step

  1. Open your Rails test environment configuration file. Navigate to config/environments/test.rb in your Rails app. This file controls settings specific to the test environment, including browser behavior.
  2. Ensure Capybara is using Selenium with Chrome in headless mode. Confirm that your test.rb includes or does not override the default system test driver. Rails typically sets this in test/application_system_test_case.rb. If not already present, ensure the driver is registered. You can verify or set it in test.rb or via the test case.
  3. 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-size flag to specify width and height. Example:
  4.   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
    
  5. Set the default driver for system tests. In test/application_system_test_case.rb, ensure the test case uses the correct driver:
  6.   class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
        driven_by :selenium_chrome_headless
      end
    
    If you named the driver differently, update driven_by accordingly.
  7. 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.innerWidth and window.innerHeight:
  8.   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
    
  9. 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,667 for iPhone-sized screens.
  10. 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-node or google-chrome-stable) to guarantee Chrome is available.
  11. Restart Spring server if tests don’t reflect changes. After modifying driver settings, stop Spring with bin/spring stop to ensure new configurations are loaded when running tests.

Common Mistakes

  • Using --window-size without enabling headless mode correctly—ensure --headless=new is 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-gpu and --no-sandbox in CI environments, leading to crashes.

Tips

  • Use --window-size=1920,1080 for desktop testing and --window-size=375,667 for 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=VizDisplayCompositor if encountering rendering glitches in headless mode.
  • Use the webdrivers gem to auto-manage ChromeDriver versions and avoid compatibility issues.
  • Log Chrome arguments during test startup for debugging: wrap driver creation in a puts or Rails logger statement.

Troubleshooting

If the screen size does not apply:

  • Check Chrome version compatibility. Run google-chrome --version and ensure ChromeDriver supports it. Mismatched versions cause silent failures.
  • Verify headless mode is active. Remove --headless=new temporarily 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'].inspect to verify arguments are passed.
  • Run tests with debugging output. Set SELENIUM_LOG_LEVEL=debug or use selenium-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.