Skip to content

Instantly share code, notes, and snippets.

@mcrmfc
Created May 25, 2016 15:56
Show Gist options
  • Save mcrmfc/838ca6b1ad768b1d53e7e7e59b3932be to your computer and use it in GitHub Desktop.
Save mcrmfc/838ca6b1ad768b1d53e7e7e59b3932be to your computer and use it in GitHub Desktop.
Firefox Profile with Client Cert
require 'selenium/webdriver'
# How to run this test.
# Manually create a new firefox profile using the profile manager " /Applications/Firefox.app/Contents/MacOS/firefox-bin -P"
# Import your p12 BUT DO NOT SET A MASTER PASSWORD
# Move cert8.db key3.db from that profile to the same directory as this script e.g.
# cp ~/Library/Application\ Support/Firefox/Profiles/4k2ob3bb.webdrivertest/*.db .
# NOTE: DO NOT USE YOUR OWN CERT IN CI!!!!
# In order to use this in CI get whoever provisions your CI box to provision an app cert in this format
# In theory it should be possible to do this on the command line using Mozilla nsutil - but it's very fussy about
# whay platform it runs on and is a pain in the preverbials.
# Run this and pass the url you want to visit as an argument
# e.g. ruby profile-cert-example.rb https://mycertprotectedhost.co.uk
def update_firefox_profile_with_certificates(profile, certificate_path, certificate_prefix = '')
profile_path = profile.layout_on_disk
puts "Your profile is located at #{profile_path}"
# Create links to the certificate files in the profile directory
%w(cert8.db key3.db).each do |cert_file|
source_file = "#{certificate_prefix}#{cert_file}"
source_path = "#{certificate_path}" + File::SEPARATOR + source_file
dest_path = profile_path + File::SEPARATOR + cert_file
if(! File.exist?(source_path))
raise "Firefox cert db file #{source_path} does not exist."
end
FileUtils.cp(source_path, dest_path)
end
# Force the certificates to get pulled into the profile
profile = Selenium::WebDriver::Firefox::Profile.new(profile_path)
# Avoid Firefox certificate alerts
profile["security.default_personal_cert"] = 'Select Automatically'
return profile
end
profile = Selenium::WebDriver::Firefox::Profile.new
profile = update_firefox_profile_with_certificates(profile, '.')
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.navigate.to ARGV[0]
puts driver.title
driver.quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment