java - 远程 Selenium 2 Webdriver 实例如何接受不受信任的证书?

标签 java webdriver selenium-webdriver

我正在尝试使用 JUnit 实现一些 Selenium 2 Webdriver 测试。 SeleniumHQ.org 和网络上的文档让我感到困惑,因为它似乎在 Selenium RC 和 Webdriver 之间来回跳转。另外,我的Java不是很强。我几年前学过一些类(class),但没怎么用过。我希望从 headless CI 服务器运行 JUnit 测试,并使用 Webdriver 在远程客户端系统上运行 Firefox。

根据我收集的信息,我可以使用以下代码在本地系统上打开 Webdriver 控制的 Firefox 实例。我正在测试的网站有一个不受信任的 SSL/TLS 证书,因此我需要告诉 Firefox 驱动程序接受不受信任的证书。这在本地效果很好:

FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);  // NOTE: this is the default behavior
RemoteWebDriver driver = new FirefoxDriver(profile);
Selenium selenium = new WebDriverBackedSelenium(driver, baseurl);

但我不知道如何使用 Webdriver 在远程系统上执行此操作。这两种方法似乎完全不兼容。上面的代码无论如何都不适合我远程使用 Webdriver 时使用的以下代码:

Selenium selenium = new DefaultSelenium(host, port, browser, baseurl);
selenium.start();

现在,我花了很多时间在远程测试系统上使用自定义 Firefox 配置文件。它在 2012 年夏天可以工作,但在最近的操作系统和浏览器更新之后,它停止工作了。创建 Firefox 驱动程序配置文件并调用 setAcceptUntrustedCertificates(true) 似乎要好得多。是否可以使用 Webdriver 在远程系统上的浏览器中运行测试,并且让浏览器忽略不受信任的 SSL/TLS 证书?

最佳答案

正如您的问题中所提到的,您不需要设置任何属性来显式接受不受信任的证书。默认情况下,webdriver 接受不受信任的证书。您应该直接使用 RemoteWebdriver,而不是使用 webdriverbacked selenium,如下所示:

Webdriver wd = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());

这里http://localhost:4444/wd/hub是测试应发送到执行的集线器的URL。当您开始测试时,集线器将查找已注册 Firefox 功能的远程节点。

我个人建议阅读http://code.google.com/p/selenium/wiki/Grid2处的文档而不是 seleniumhq.org。据我所知,selenium 团队正在尝试更新 seleniumhq 文档。您也可以为此做出贡献:)

关于java - 远程 Selenium 2 Webdriver 实例如何接受不受信任的证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14244773/

相关文章:

java - 为什么 HashSet.add(Object) 不替换现有对象?

selenium - CreatePlatformSocket() 尝试通过网络路径访问 chromedriver 时返回错误 : An invalid argument was supplied. (0x2726)

selenium - 使用 selenium webdriver 登录弹出窗口?

c# - Selenium phantomjs webdriver,如何使用 session ?

java - 停止/终止可运行线程 - 启动新的可运行线程 - Java

java - 使用 Selenium Java 捕获 onclick() javascript 调用的参数

java - 在 Spring Boot 日志记录中使用外部 JUL "logging.properties"

java - 为什么我得到错误的输出

java - 嵌入式 hazelcast 防止 Tomcat 中的干净关闭

ruby - 如何为 Chrome 构建远程 Webdriver