将个人SSL证书与Webdriver一起使用(Selenium 2.0)
问题内容:
我正在测试一个网站,该网站需要个人SSL证书才能执行某些操作,例如登录。
我有一个使用代理设置的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);
这将可以访问主页。然后,测试单击登录按钮,输入正确的凭据,然后单击提交。此时,浏览器随后进入加载状态,我认为这是因为SSL证书在我这边丢失了,因此无法连接到登录服务。
我搜索了不同的代理解决方案,发现了这一点:
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(true);
所以我将其添加到我的代码中,但它似乎并没有实现我想要的功能。我想我正在寻找一种告诉WebDriver我的ssl证书在x目录中的方法,请在访问此网站时使用它。有谁知道如何做到这一点?
我的测试代码是:
@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);
}
谢谢,Beccy
问题答案:
Webdriver没有用于添加个人证书的内置机制。
如果您使用的是firefox,我发现这样做的唯一方法是创建一个firefox配置文件并将证书添加到其中。然后,您可以在运行测试时重用配置文件,或者这是我的首选,选择cert8.db和key3.db文件并将它们添加到webdriver在运行时创建的配置文件中。
我不确定如何在Java中执行此操作,但是在ruby中,我重写了FirefoxProfile的layout_on_disk方法来添加所需的额外文件。Java具有
相同的类,
因此您应该能够执行相同的操作。