User Agents

Web browsers and everyday web tech, explained

Open Chrome with Rails Feature Specs Using Docker

· 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

Running Rails feature specs with Chrome in a Docker environment allows developers to test browser interactions in a consistent, isolated setup. This approach ensures that JavaScript-heavy features work correctly across different environments by launching Chrome via headless mode or with a visible browser when debugging. It integrates tools like Capybara, Selenium, and Docker Compose to automate browser tests within a containerized Rails application. This guide walks through setting up Chrome for Rails feature specs inside Docker, enabling reliable and repeatable testing workflows.

What You'll Need

  • Rails application with RSpec and Capybara configured for feature specs
  • Docker and Docker Compose installed on your system
  • ChromeDriver compatible with the Chrome version used in the container
  • Basic understanding of Docker networking and container linking

Step-by-Step

  1. Ensure your Rails app has rspec-rails, capybara, and selenium-webdriver gems in the test group of the Gemfile. Run bundle install to install them.
  2. Create a docker-compose.yml file in your project root if one doesn’t exist. This will define services for your Rails app and Chrome.
  3. Add a service for Chrome using the official Selenium Docker image. Use selenium/standalone-chrome which bundles Chrome and ChromeDriver:
  4. services:
      rails:
        build: .
        ports:
          - "3000:3000"
        volumes:
          - .:/app
        depends_on:
          - chrome
      chrome:
        image: selenium/standalone-chrome:latest
        shm_size: 2gb
        ports:
          - "4444:4444"
    
  5. In your Rails spec/rails_helper.rb or spec/spec_helper.rb, configure Capybara to use Selenium with a remote Chrome driver. Add this configuration:
  6. Capybara.register_driver :selenium_chrome_headless_remote do |app|
        options = Selenium::WebDriver::Options.chrome(
          args: %w[headless no-sandbox disable-gpu window-size=1280,800]
        )
    
        Capybara::Selenium::Driver.new(
          app,
          browser: :remote,
          url: "http://chrome:4444",
          desired_capabilities: :chrome,
          options: options
        )
      end
    
      Capybara.default_driver = :selenium_chrome_headless_remote
      Capybara.javascript_driver = :selenium_chrome_headless_remote
  7. Update your Dockerfile to install necessary dependencies like Node.js and yarn, which Rails often requires for asset compilation during tests:
  8. FROM ruby:3.1
    
    RUN apt-get update \u0026\u0026 apt-get install -y nodejs yarn \u0026\u0026 rm -rf /var/lib/apt/lists/*
    
    WORKDIR /app
    COPY Gemfile Gemfile.lock ./
    RUN bundle install
    
    COPY . .
    
  9. Build the Docker image and start the services using docker-compose up --build. This launches both Rails and Chrome containers.
  10. Run your feature specs with JavaScript using docker-compose run rails rspec spec/features/your_feature_spec.rb. Capybara will connect to Chrome via Selenium on port 4444.
  11. To view Chrome in non-headless mode for debugging, modify the Chrome args in the driver configuration by removing headless and optionally exposing VNC port 5900 in the Docker Compose file. Use a VNC client to view the browser session.
  12. For persistent testing environments, consider caching bundle installs and node modules using Docker volumes or multi-stage builds to speed up test runs.
  13. Ensure database migrations are run in the Rails container before running specs. Use docker-compose run rails rails db:test:prepare or equivalent depending on your Rails version.

Common Mistakes

  • Forgetting to set shm_size: 2gb in the Chrome service, which can cause Chrome to crash during tests due to insufficient shared memory.
  • Using localhost instead of the service name (chrome) when configuring the Selenium URL in Capybara.
  • Not installing Node.js or yarn in the Docker image, leading to asset compilation errors during test runs.
  • Running specs without ensuring the test database is up to date, resulting in failed expectations due to missing data.
  • Attempting to use ChromeDriver manually instead of relying on the Selenium standalone image, which handles version compatibility.

Tips

  • Use selenium/standalone-chrome-debug image to enable VNC access for visual debugging of browser behavior during tests.
  • Tag your feature specs with :js and use RSpec filtering to run only those requiring Chrome.
  • Set autoclose=false in Chrome args temporarily to inspect the browser state after test failure.
  • Version pin your Selenium image (e.g., selenium/standalone-chrome:4.15) to avoid unexpected breaking changes.
  • Mount your local code as a volume in Docker to reflect changes without rebuilding the image every time.

Troubleshooting

If Chrome fails to start or the connection times out, first verify that the chrome service is running via docker-compose ps. Check logs with docker-compose logs chrome for startup errors. If Chrome crashes immediately, confirm shm_size is set. If Capybara raises a connection error, ensure the Selenium URL uses the service name and correct port (4444). If assets fail to compile, verify Node.js and yarn are installed in the Rails container. For JavaScript not executing, ensure your spec is tagged with :js and uses js: true in the test metadata. If using Docker Desktop, ensure sufficient memory is allocated to the Docker engine, especially when running headless Chrome.

Frequently Asked Questions

Can I use Chrome instead of Firefox in Docker?

Yes, using selenium/standalone-chrome is standard and well-supported for Chrome in Docker.

Why does Chrome crash during tests?

Most often due to insufficient shared memory. Always set shm_size: 2gb in the Docker Compose service.

How do I debug browser behavior in Docker?

Use the -debug variant of the Selenium image and connect via VNC on port 5900.

Do I need to install ChromeDriver manually?

No, the Selenium standalone image includes a compatible ChromeDriver automatically.

Can I run tests without headless mode?

Yes, remove headless from Chrome args and use VNC to view the browser session.

How do I speed up test startup time?

Use Docker volumes to cache bundle install and yarn install, or implement multi-stage builds.