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

时间:2013-01-09 19:19:17

标签: java webdriver selenium-webdriver

我正在尝试使用JUnit实现一些Selenium 2 Webdriver测试。 SeleniumHQ.org和Web上的文档让我感到困惑,因为它似乎在Selenium RC和Webdriver之间来回跳转。另外,我的Java不是很强大。我几年前参加了几门课程,但没有多少用过。我希望从无头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证书?

3 个答案:

答案 0 :(得分:2)

如您的问题所述,您无需设置任何属性即可明确接受不受信任的证书。默认情况下,webdriver接受不受信任的证书。您应该直接使用remotewebdriver,而不是使用webdriverbacked selenium:

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

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

我个人建议在http://code.google.com/p/selenium/wiki/Grid2而不是seleniumhq.org阅读文档。据我所知,selenium团队正试图更新seleniumhq文档。您也可以参与其中:)

答案 1 :(得分:2)

首先,如果你使用webdriver支持的selenium只是为了配置文件,我会建议坚持使用webdriver。您可以定义要在本地计算机上使用的配置文件

   File file = new File("firebug-1.8.1.xpi");
   FirefoxProfile firefoxProfile = new FirefoxProfile();
   firefoxProfile.addExtension(file);
   firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); 
   WebDriver driver = new FirefoxDriver(firefoxProfile);

回答你的问题:我将引用Simon here提出的解决方案:

 FirefoxProfile profile = new FirefoxProfile(); 
 profile.setAcceptUntrustedCertificates(true); 
 DesiredCapabilities caps = DesiredCapabilities.firefox(); 
 caps.setCapability(FirefoxDriver.PROFILE, profile);

使用此配置文件创建远程驱动程序。

现在,如果这不起作用,我们可以写出错误(或至少是一个功能请求)。

发布后编辑: 我无法真正测试此解决方案,因为我没有现成的证书问题网站。因此,在某种程度上,我会向您寻求反馈,以获得真实的图片...... :)

答案 2 :(得分:-1)

当我问这个问题时,我不理解Selenium对象和WebDriver对象之间的区别。虽然我特意试图了解Selenium 2的“WebDriver”功能,但我愚蠢地认为我可以用Selenium 2对象编写一个“Selenium 2 Webdriver”项目。对于有这些工具经验的人来说,这听起来很明显,但在阅读“Selenium 2”书籍和项目文档之后,我的想法仍然不明确。

结果,我正在编写Java代码来实例化Selenium对象以检查网页,并尝试将Selenium对象传递给WebDriver对象,希望测试可以在远程服务器上运行。

现在看起来更清楚:Selenium和WebDriver项目合并为一个名为(令人困惑的)Selenium 2.0的新伞项目,但它们是Selenium 2中独特且独立的工具。如果我想使用WebDriver API,似乎我必须将任何现有的Selenium对象转换为WebDriver对象。两种工具之间似乎没有有用的互动。

例如,在我的项目中,我有以下代码。它在我的本地桌面系统的Web浏览器上运行得很好:

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

selenium.get(urlPath);

selenium.type(username_field, username);
selenium.type(password_field, password);
selenium.click(login_button);

但我希望能够在无头的持续集成服务器上运行该测试,而不是我的桌面系统。我已将代码转换为使用WebDriver对象而不是Selenium对象。现在它运行在连接到Selenium Grid 2服务器的远程系统上:

WebDriver driver = new RemoteWebDriver(new URL("http://10.0.0.29:4444/wd/hub"), capability);

driver.get(urlPath);

driver.findElement(By.name(username_field)).sendKeys(username);
driver.findElement(By.name(password_field)).sendKeys(password);
driver.findElement(By.className(login_button)).submit();

我希望其他想要学习如何在Selenium 2中使用WebDriver的人不会浪费我阅读Selenium对象的时间,认为Selenium对象是WebDriver的一部分。我当前的[n00b]建议是忽略提及Selenium对象的任何内容,并且只关注于尽可能多地查找WebDriver对象。一个好的起点是SeleniumHQ.org上的WebDriver文档:

作为A.J.在他的回答中建议,还要看一下Selenium Grid文档:

PS:默认情况下,远程Selenium 2 Webdriver实例会自动接受不受信任的SSL / TLS证书。无需代码。

相关问题