在Webdriver中使用个人SSL证书(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); } 

谢谢,贝西

Webdriver没有用于添加个人证书的内置机制。

如果您使用的是firefox,我发现这样做的唯一方法是创建一个firefox配置文件并将证书添加到它。 然后,您可以在运行测试时重用配置文件,这是我的首选选项,请获取cert8.db和key3.db文件,并将它们添加到webdriver在运行时创建的配置文件中。

我不确定你是如何在java中做到这一点的,但是在ruby中我覆盖了FirefoxProfile的layout_on_disk方法来添加我需要的额外文件。 Java具有相同的类,因此您应该能够执行相同的操作。

无需按建议覆盖方法layout_on_disk()。
您只需将包含文件cert8.db和key3.db的文件夹作为配置文件加载即可。

Selenium将为您完成配置文件。

然后,您可以将所需的首选项添加到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()); 

用selenium3.5.3测试。

Webdriver可以做到这一点,虽然Derek是正确的,它不是内置的。

您需要做的就是创建一个信任所有证书的自定义信任管理器,然后覆盖“主机名validation程序”以允许非真实域名。

我在Google上找到了一些例子:

 http://grepcode.com/file/repo1.maven.org/maven2/org.seleniumhq.selenium.server/selenium-server-coreless/1.0.3/org/openqa/selenium/server/TrustEverythingSSLTrustManager.java 

这与使用Apache HC组件在不使用WebDriver时覆盖SSL设置的方法相同。 我在使用Apache HT组件的直接HTTPpost中使用了这个方法,并且“看起来”从上面的链接开始,这个概念也适用于WebDriver。