将ssl证书添加到selenium-webdriver [英] Add ssl certificate to selenium-webdriver
问题描述
我将硒用于chromeDriver进行端到端测试。要测试的网站需要ssl证书。手动打开浏览器时,会弹出一个窗口,供我选择已安装的证书。不同的测试访问不同的URL,并且还需要不同的证书。但是,如果我以无头模式运行测试,则不会弹出窗口。因此,我需要一种以编程方式设置要用于当前测试的证书(例如,设置 .pem
文件)的方法。
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.
如何实现?
我尝试设置 browserMob 代理,然后将其配置为硒代理-但是,这似乎无能为力...有更好的方法吗?我究竟做错了什么?这是我尝试的方法:
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(...))
推荐答案
因此,显然,对于BrowserMob,这是不可能的。因此,我编写了一个代理扩展 SeleniumSslProxy
,可以将其插入Selenium并添加基于证书的身份验证以创建HTTPS连接。
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.
它是这样工作的:
- 使用BrowserMob拦截Selenium HTTP请求
- 设置给定证书(.pfx文件)和密码的
SSLContext
- 使用 okhttp 将请求转发到目标URL
- 转换okhttp
Response
到净额FullHttpResponse
以便可以由Selenium
- intercept Selenium HTTP requests with BrowserMob
- setup an
SSLContext
given a certificate (.pfx file) and password - use okhttp to forward the request to the target URL
- convert the okhttp
Response
to a nettyFullHttpResponse
so it can be handled by Selenium
您可以在 github 上找到代码。这是一个如何在Selenium端到端测试(也可以在无头模式下使用)中使用的示例:
You 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):
@Before
public void setup() {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
File clientSslCertificate = new File(
classLoader.getResource("certificates/some-certificate.pfx").getFile());
String certificatePassword = "superSecret";
this.proxy = new SeleniumSslProxy(clientSslCertificate, certificatePassword);
this.proxy.start();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setProxy(proxy);
this.webDriver = new ChromeDriver(chromeOptions);
}
@Test
public void pageTitleIsFoo() {
// given
String url = "http://myurl.lol";
// NOTE: do not use https in the URL here. It will be converted to https by the proxy.
// when
this.webDriver.get(url);
this.webDriver.manage().timeouts().implicitlyWait(5, SECONDS);
// then
WebElement title = this.webDriver.findElement(By.className("title"));
assertEquals("Foo", title.getText());
}
@After
public void teardown() {
this.webDriver.quit();
this.proxy.stop();
}
请注意,我仅使用chromeDriver,从未使用其他驱动程序对其进行测试。对 SeleniumSslProxy
进行细微调整可能需要与其他驱动程序一起使用。
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.
这篇关于将ssl证书添加到selenium-webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!