I use selenium for end-to-end test with chromeDriver. The websites to test require an ssl certificate. When I manually open the browser, there is a popup that lets me select an installed certificate. Different tests access different URLs and also need different certificates. However, if I run the tests in headless mode, there is no popup. So I need a way to programatically set a certificate (eg. set a .pem
file) to be used for the current test.
How can I achieve this? I tried setting up a browserMob proxy which I then configured as a proxy in selenium - however, this does not seem to do anything... Are there better approaches? What am I doing wrong? Here's what I tried:
PemFileCertificateSource pemFileCertificateSource = new PemFileCertificateSource(
new File("myCertificate.pem"),
new File("myPrivateKey.pem"),
"myPrivateKeyPassword");
ImpersonatingMitmManager mitmManager = ImpersonatingMitmManager.builder()
.rootCertificateSource(pemFileCertificateSource)
.build();
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
browserMobProxy.setTrustAllServers(true);
browserMobProxy.setMitmManager(mitmManager);
browserMobProxy.start(8080);
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setProxy(ClientUtil.createSeleniumProxy(browserMobProxy));
WebDriver webDriver = new ChromeDriver(chromeOptions);
// use the webdriver for tests, e.g. assertEquals("foo", webDriver.findElement(...))
So apparantly this is not possible with BrowserMob out of the box. I therefore wrote a proxy extension
SeleniumSslProxy
that can be plugged into Selenium and adds certificate based authentication to create a HTTPS connection.This is how it works:
SSLContext
given a certificate (.pfx file) and passwordResponse
to a nettyFullHttpResponse
so it can be handled by SeleniumYou can find the code on github. Here's an example how it can be used in Selenium end-to-end tests (also works in headless mode):
Note that I only used chromeDriver and never tested it with other drivers. Minor adjustments to the
SeleniumSslProxy
might be necessary to be used with other drivers.