Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SSL with Capybara and Selenium
# Hack for Capybara to use SSL connections using selenium.
### Usage:
# Require this from rails_helper.rb
### Steps to generate a SSL certificate on a Linux box:
# 0. Starting from 'Rails.root'
# 1. Generate private key. Type in some password.
# $ openssl genrsa -des3 -out private.key 4096
# 2. Generate certificate sign request
# $ openssl req -new -key private.key -out server.csr
# 3. Store decrypted private key in test/. Type in your password from before.
# Then, get rid of the original private key file.
# $ openssl rsa -in private.key -out test/private.key
# $ shred private.key && rm private.key
# 4. Self-sign your new server certificate
# $ openssl x509 -req -days 3650 -in server.csr -signkey test/private.key -out test/certificate.pem
##### - DONE!
# Monkey patched responsive? for SSL. Taken from:
module Capybara
class Server
def responsive?
return false if @server_thread && @server_thread.join(0)
http =, @port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = http.get('/__identify__')
if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
return res.body == @app.object_id.to_s
rescue SystemCallError
return false
# Helper class to start a WEBrick server with SSL
class SslServer
TEST_CERTIFICATE = Rails.root.join('test', 'certificate.pem').to_s
TEST_PRIVATE_KEY = Rails.root.join('test', 'private.key').to_s
require 'rack/handler/webrick'
require 'webrick/https'
def initialize
@certificate = TEST_CERTIFICATE
def run(app, port)
options = {
Port: port,
AccessLog: [],
Logger: WEBrick::Log::new(nil, 0),
SSLEnable: true,
SSLCertificate: @certificate,
SSLPrivateKey: @key
} app, options
# Configure Capybara for SSL.
RSpec.configure do |config|
Capybara.register_driver :selenium_ssl do |app|
profile =
profile.secure_ssl = false
profile.assume_untrusted_certificate_issuer = false app, browser: :firefox, profile: profile
Capybara.javascript_driver = :selenium_ssl
Capybara.app_host = "https://#{Capybara.server_host}:#{Capybara.server_port}"
Capybara.server do |app, port|
server = app, port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.