12

Is it possible to test client side SSL certificates with Selenium and any browser? E.g. Can you create a web driver and give dummy certificates for it? Or use a prepared Firefox profile?

2 Answers 2

17

Creating Selenium Firefox test profile for SSL client-side certificates

You need to prepare Selenium's WebDriver Firefox profile which has client certificates imported in.

First you launch WebDriver with the following configuration in your test code:

# Pre-seeded Firefox profile directory
profile_directory = os.path.join(os.path.dirname(__file__), "..", "..", "certs", "firefox-client-ssl-profile")
self.assertTrue(os.path.exists(profile_directory))

profile = FirefoxProfile(profile_directory)

# Make sure the client side certificate selection does not interrupt the test
# XXX: What happens in other language versions?
profile.set_preference("security.default_personal_cert", "Select Automatically")
self.driver = WebDriver(firefox_profile=profile)

self.selenium_helper = SeleniumHelper(self, self.driver)
self.selenium_helper.driver = self.driver

Start unit tests and drive them to the point Zope test server is up. Stop tests with "import pdb ; pdb.set_trace()"

You should now have a Selenium's "WebDriver" Firefox instance on your screen.

Import your client side certificate. Preferences > Advanced > Encryption > View certificates. Import "client.p12" from your client-side certificate supply.

Visit in the URL triggering the client-side certificate dialog in Webdriver's Firefox::

    https://yourservevr/triggers-client-side-certificate-ssl-handshake

This should prompt you to accept the client side certificate against the test server. Accept everything manually.

Visit in menu Help > Troubleshooting Information > Application Basics > Show in Finder. This opens the temporary directory holding Webdriver's active profile files.

Copy Firefox profile files cert8.db and key3.db to your unit test package WebDriver's Firefox profile's seed folder. This is the folder where Selenium picks seeds for Firefox web driver when the test starts firefox-client-ssl-profile.

Interrupt the tests. Restart the tests. Run until the pause again. In Webdriver's Firefox see in the settings that it now contains the certificates you did approve on the last run in Preferences > Advanced > Encryption > View certificates.

More info

1
  • 3
    From Firefox version 58 it will be cert9.db and key4.db. With Visual Studio make sure they will be marked as "Copy always" in the properties to ensure that these files are copied when building.
    – 321X
    Nov 27, 2017 at 11:49
0

I dont know if this helps, but you can change some of the preferences in the profile. In Java, you could do this.

ProfilesIni allProfiles = new ProfilesIni();
    FirefoxProfile profile = allProfiles.getProfile("default"); //change profile name. there is a note somewhere on how to change it
    profile.setPreference(uaKey, uaValue);
    profile.setAcceptUntrustedCertificates(acceptUntrustedSsl);

I am not sure this is what you need.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.