Thomas Cannon

HTTPS, Subdomained System Tests in Ruby on Rails

Getting puma to use self-signed certificates in Test

Install the localhost gem, which allows Puma to use self-signed certificates in your test environment:

group(:test) do
  gem 'localhost'

It’s not documented anywhere in Capybara, but Capybara’s built-in Puma handler allows you to issue a custom bind

Capybara.run_server = true
Capybara.server = :puma, { Host: "ssl://#{Capybara.server_host}"}

I found that you do need to explicitly pass the Capybara.run_server = true argument in your system tests

You also need to have Selenium Webdriver explicitly accept insecure hosts:

For example:

Capybara.register_driver :chrome do |app|
  options =
  options.accept_insecure_certs = true, browser: :chrome, options: options)

With that, Capybara runs with self-signed certificates in development, and the WebDriver will accept them.

Running subdomained tests

Because of how macOS does not handle wildcard DNS resolution for localhost by default, if you need to run your tests using subdomains, you’ll need to use a loopback DNS service like
For example:

Capybara.app_host = ""

Depending on how your existing tests are setup, you might also need to setup a custom initializer block in config/environments/test.rb to override the tld_length

Rails.application.configure do

config.hosts << “”

if ENV[“PREP_SYSTEM_TESTS”].present? config.action_dispatch.tld_length = 5 # = 5 top-level host details to throw out Rails.application.routes.default_url_options[:host] = “” # Necessary for routes to play nicely # … end end