Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evg2108/db889add9c406fb6421f146a86ab8305 to your computer and use it in GitHub Desktop.
Save evg2108/db889add9c406fb6421f146a86ab8305 to your computer and use it in GitHub Desktop.

need phantomjs >= 2.1

require 'capybara/poltergeist'
require 'phantomjs'

Capybara.register_driver :poltergeist do |app|
  path_to_certs = '/home/evg2108/RubymineProjects/megafon_sbms/certificates/'
  path_to_ca_certs = path_to_certs
  
  cert = OpenSSL::PKCS12.new(File.open("#{path_to_certs}/my_certificate.pfx"), 'password')

  f = File.open("#{path_to_certs}/key.key", 'w')
  f.write(cert.key.to_s)
  f.close
  key = f.path


  f = File.open("#{path_to_certs}/certificate.cer", 'w')
  f.write(cert.certificate.to_s)
  f.close
  certificate = f.path

  f = File.open("#{path_to_certs}/ca.crt", 'w')
  f.write(cert.ca_certs.map(&:to_s).join)
  f.close
  ca_file = f.path
  Capybara::Poltergeist::Driver.new(app, {
    js_errors: false,
    timeout: 100,
    phantomjs: Phantomjs.path,
    phantomjs_options: %W(--load-images=no --ignore-ssl-errors=yes --ssl-protocol=any --ssl-client-certificate-file=#{certificate} --ssl-client-key-file=#{key} --ssl-certificates-path=#{path_to_ca_certs})})
end

Capybara.configure do |config|
  config.run_server = false
  config.default_wait_time = 60
end

Capybara.default_driver = :poltergeist

@agent = Capybara::Session.new :poltergeist
@agent.visit('https://site_with_client_certificate_auth.org')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment