java 将个人 SSL 证书与 Webdriver (Selenium 2.0) 结合使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5578023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Personal SSL certificates with Webdriver (Selenium 2.0)
提问by Beccy
I am testing a website which requires personal SSL certificates in order to do certain things, such as sign-in.
我正在测试一个需要个人 SSL 证书才能执行某些操作(例如登录)的网站。
I have a Webdriver (Selenium 2.0) test that I have set up with a proxy:
我有一个使用代理设置的 Webdriver (Selenium 2.0) 测试:
Proxy localhostProxy = new Proxy();
localhostProxy.setProxyType(Proxy.ProxyType.MANUAL);
localhostProxy.setHttpProxy("www-proxyname:port");
FirefoxProfile profile = new FirefoxProfile();
profile.setProxyPreferences(localhostProxy);
driver = new FirefoxDriver(profile);
And this will access the homepage fine. The test then clicks the sign in button, enters in the correct credentials and clicks on submit. At this point the browser then goes into a loading state, and I'm assuming it's because the SSL certificate is missing from my side and therefore cannot connect to the sign in service.
这将很好地访问主页。然后测试单击登录按钮,输入正确的凭据并单击提交。此时浏览器会进入加载状态,我假设这是因为我这边缺少 SSL 证书,因此无法连接到登录服务。
I searched for different proxy solutions, and found this:
我搜索了不同的代理解决方案,发现了这个:
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
So I added it into my code, but it doesn't seem to do what I want. I think I'm looking for a way to tell WebDriver that my ssl certificate is in x directory, please use it when accessing this site. Does anyone know how to do this?
所以我将它添加到我的代码中,但它似乎没有做我想要的。我想我正在寻找一种方法来告诉 WebDriver 我的 ssl 证书在 x 目录中,请在访问此站点时使用它。有谁知道如何做到这一点?
My Test code is:
我的测试代码是:
@Test
public void userSignsInAndVerifiesDrawerViews(){
driver.get("www.url.com");
waitFor(5000);
driver.findElement(By.xpath("//a[contains(text(), 'Sign in')]")).click();
waitFor(3000);
String username = "seleniumtest";
String password = "seleniumtest1";
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.xpath("//signin")).click();
waitFor(30000);
String signInLinkText = driver.findElement(By.xpath("//xpath")).getText();
assertEquals(signInLinkText, username);
}
Thanks, Beccy
谢谢,贝西
采纳答案by Derek Ekins
Webdriver has no built in mechanism for adding a personal cert.
Webdriver 没有用于添加个人证书的内置机制。
If you are using firefox the only way that I have found to do this is to create a firefox profile and add the certificate to it. You can then either reuse the profile when you run your tests OR, and this is my prefered option, take the cert8.db and key3.db files and add them to the profile that webdriver creates at runtime.
如果您使用的是 Firefox,我发现这样做的唯一方法是创建一个 Firefox 配置文件并将证书添加到其中。然后,您可以在运行测试时重用配置文件,或者,这是我的首选选项,获取 cert8.db 和 key3.db 文件并将它们添加到 webdriver 在运行时创建的配置文件中。
I am not sure how yo do this in java, but in ruby I override the layout_on_disk method of FirefoxProfile to add the extra files I required. Java has the same classso you should be able to do this same thing.
我不确定如何在 Java 中执行此操作,但是在 ruby 中我覆盖了 FirefoxProfile 的 layout_on_disk 方法以添加我需要的额外文件。Java 有相同的类,所以你应该能够做同样的事情。
回答by Rubén Escartín
No need to overwrite the method layout_on_disk() as suggested.
You can simply load as profile a folder containing the files cert8.db and key3.db.
无需按照建议覆盖方法 layout_on_disk()。
您可以简单地将包含文件 cert8.db 和 key3.db 的文件夹加载为配置文件。
Selenium will complete the profile for you.
Selenium 将为您完成配置文件。
Then you can add the preferences you need to the firefox profile.
The resulting code looks like this:
然后您可以将您需要的首选项添加到 firefox 配置文件中。
生成的代码如下所示:
FirefoxProfile firefoxProfile = new FirefoxProfile(
new File("/folder/location"));
FirefoxOptions options = new FirefoxOptions();
options.setProfile(firefoxProfile);
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444/wd/hub"),
options.toCapabilities());
Tested with selenium 3.5.3.
用硒 3.5.3 测试。
回答by djangofan
Webdriver can do this, although Derek is right and it isn't built in.
Webdriver 可以做到这一点,尽管 Derek 是对的,而且它不是内置的。
All you need to do is make a custom Trust Manager that trusts all certs and then also override the "hostname verifier" to allow a non-real domain name.
您需要做的就是创建一个信任所有证书的自定义信任管理器,然后还覆盖“主机名验证器”以允许非真实域名。
There is somewhat of an example I found on Google here:
我在谷歌上找到了一个例子:
http://grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.selenium.server/selenium-server-coreless/1.0.3/org/openqa/selenium/server/TrustEverythingSSLTrustManager.java
This is the same method you would use with Apache HC componentsto override SSL settings without using WebDriver. I've used this method a lot with direct HTTP posts using Apache HT components and it "appears" that from the link above , this concept should also work with WebDriver.
这与您使用Apache HC 组件覆盖 SSL 设置而不使用 WebDriver 的方法相同。我在使用 Apache HT 组件的直接 HTTP 帖子中经常使用这种方法,并且从上面的链接“看来”,这个概念也应该适用于 WebDriver。