Open Chrome with Rails Feature Specs Using Docker
In this article
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
- Ensure your Rails app has
rspec-rails,capybara, andselenium-webdrivergems in thetestgroup of the Gemfile. Runbundle installto install them. - Create a
docker-compose.ymlfile in your project root if one doesn’t exist. This will define services for your Rails app and Chrome. - Add a service for Chrome using the official Selenium Docker image. Use
selenium/standalone-chromewhich bundles Chrome and ChromeDriver: - In your Rails
spec/rails_helper.rborspec/spec_helper.rb, configure Capybara to use Selenium with a remote Chrome driver. Add this configuration: - Update your Dockerfile to install necessary dependencies like Node.js and yarn, which Rails often requires for asset compilation during tests:
- Build the Docker image and start the services using
docker-compose up --build. This launches both Rails and Chrome containers. - 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. - To view Chrome in non-headless mode for debugging, modify the Chrome args in the driver configuration by removing
headlessand optionally exposing VNC port 5900 in the Docker Compose file. Use a VNC client to view the browser session. - For persistent testing environments, consider caching bundle installs and node modules using Docker volumes or multi-stage builds to speed up test runs.
- Ensure database migrations are run in the Rails container before running specs. Use
docker-compose run rails rails db:test:prepareor equivalent depending on your Rails version.
services:
rails:
build: .
ports:
- "3000:3000"
volumes:
- .:/app
depends_on:
- chrome
chrome:
image: selenium/standalone-chrome:latest
shm_size: 2gb
ports:
- "4444:4444"
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
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 . .
Common Mistakes
- Forgetting to set
shm_size: 2gbin the Chrome service, which can cause Chrome to crash during tests due to insufficient shared memory. - Using
localhostinstead 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-debugimage to enable VNC access for visual debugging of browser behavior during tests. - Tag your feature specs with
:jsand use RSpec filtering to run only those requiring Chrome. - Set
autoclose=falsein 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, usingselenium/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 setshm_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, removeheadless from Chrome args and use VNC to view the browser session.
How do I speed up test startup time?
Use Docker volumes to cachebundle install and yarn install, or implement multi-stage builds.