【问题标题】:Using Personal SSL certificates with Webdriver (Selenium 2.0)将个人 SSL 证书与 Webdriver (Selenium 2.0) 一起使用
【发布时间】:2011-07-31 11:38:45
【问题描述】:

我正在测试一个需要个人 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);
}

【问题讨论】:

  • 奇怪的是,当我运行测试时,我中途停止了它并访问了 Firefox -> Preferences -> Advanced -> Encyption 并选择了查看证书的选项...浏览器崩溃了我。

标签: java selenium webdriver


【解决方案1】:

无需按照建议覆盖方法 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());

使用 selenium 3.5.3 测试。

【讨论】:

    【解决方案2】:

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

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

    我不知道你是如何在 java 中做到这一点的,但是在 ruby​​ 中我覆盖了 FirefoxProfile 的 layout_on_disk 方法来添加我需要的额外文件。 Java 有same class,所以你应该可以做同样的事情。

    【讨论】:

    • 链接已更新。我已经有一段时间没有使用此代码了,所以我不能保证它与问题的持续相关性 - 我只能假设它没有太大变化。
    【解决方案3】:

    Webdriver 可以做到这一点,尽管 Derek 是对的而且它不是内置的。

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

    我在 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
    

    这与您在不使用 WebDriver 的情况下使用 Apache HC components 覆盖 SSL 设置的方法相同。我已经在使用 Apache HT 组件的直接 HTTP 帖子中大量使用了这种方法,从上面的链接“看来”,这个概念也应该适用于 WebDriver。

    【讨论】:

      猜你喜欢
      • 2013-08-03
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 2018-04-19
      • 1970-01-01
      相关资源
      最近更新 更多