Reputation: 2872
I'm using RSpec, Capybara and Selenium to test my local Rails application. Capybara by itself is working fine. My problem is that I use a forced SSL connection in most of my application.
The kind of workaround I have now is to configure Capybara for javascript testing like this:
Capybara.register_driver :selenium_profile do |app|
profile = Selenium::WebDriver::Firefox::Profile.new
profile.secure_ssl = false
profile.assume_untrusted_certificate_issuer = true
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
Capybara.javascript_driver = :selenium_profile
Capybara.run_server = false
Capybara.server_port = 3001
Capybara.app_host = "https://localhost:%d" % Capybara.server_port
That only works if I start up a server independently on port 3001 with a valid local SSL certificate. In practice, this is annoying and is just generally a manual dependency that I don't like.
Does anyone know a cleaner workaround for this? Either:
1) How to configure Capybara's internal server to find and use my local SSL certificate?, or
2) How to disable forced SSL for javascript testing, or
3) How to automatically start that local server running before any javascript tests?
Any help would be greatly appreciated.
Upvotes: 3
Views: 2064
Reputation: 1844
You can tell Capybara how to start a server, by passing a block accepting an app and a port to Capybara.server. The default just calls Rake::Handler::WEBrick.run:
# This is the default setting
Capybara.server = {|app, port| Capybara.run_default_server(app, port)}
def run_default_server(app, port)
require 'rack/handler/webrick'
Rack::Handler::WEBrick.run(app, :Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))
end
As long as whatever you pass to server accepts an app and a port, you can define whatever server starting code you like.
Rack::Handler::WEBrick passes most of its options straight through to WEBrick::HTTPServer, so you can pass the options for SSL config (SSLEnable, SSLCertificate and SSLPrivateKey, taken from the docs) and start your server something like this:
def run_ssl_server(app, port)
require 'rack/handler/webrick'
require 'webrick/https'
certificate = OpenSSL::X509::Certificate.new File.read '/myapp/some_directory/certificate.pem'
key = OpenSSL::PKey::RSA.new File.read '/myapp/some_directory/key.pem'
opts = {
:Port => port,
:AccessLog => [],
:Logger => WEBrick::Log::new(nil, 0),
:SSLEnable => true,
:SSLCertificate => certificate,
:SSLPrivateKey => key
}
Rack::Handler::WEBrick.run(app, opts)
end
# Elsewhere in your test/spec helper
Capybara.server = {|app, port| run_ssl_server(app, port)}
Upvotes: 4