1

当我尝试使用 MVN 测试命令行运行我的 selenium 测试时,我收到了这个错误。奇怪的是,我在 3 天前尝试过,它运行成功:

------------------------------------------------------
T E S T S
-------------------------------------------------------
Running GoogleNavigationTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 45.672 sec <<< FAILURE!

Results :

   Failed tests:   testApp(GoogleNavigationTest): Unable to bind to locking port 70
   54 within 45000 ms

  Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

这是我的测试:

import java.util.List;

 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.firefox.FirefoxDriver;
 import org.openqa.selenium.firefox.FirefoxProfile;
 import org.testng.annotations.Test;

public class GoogleNavigationTest {
@Test
public void testApp(){
    // The Firefox driver supports javascript 
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    WebDriver driver = new FirefoxDriver();

    // Go to the Google Suggest home page
    driver.get("http://www.google.com/webhp?complete=1&hl=en");

    // Enter the query string "Cheese"
    WebElement query = driver.findElement(By.name("q"));
    query.sendKeys("Cheese");

    // Sleep until the div we want is visible or 5 seconds is over
    long end = System.currentTimeMillis() + 5000;
    while (System.currentTimeMillis() < end) {
        WebElement resultsDiv = driver.findElement(By.className("gssb_e"));

        // If results have been returned, the results are displayed in a drop down.
        if (resultsDiv.isDisplayed()) {
          break;
        }
    }

    // And now list the suggestions
    List<WebElement> allSuggestions =   
    driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));

    for (WebElement suggestion : allSuggestions) {
        System.out.println(suggestion.getText());
    }
     }
   }
4

4 回答 4

4

迟到的回复,但试试这个更新的代码。Selenium Webdriver 将实例绑定到特定的 TCP 端口。如果您的测试失败并且驱动程序未正确关闭,则端口将保持 x 时间。使用 driver.Quit() 通常会释放 TCP 端口。

要测试查看当前可能仍保留的端口,您可以使用 netstat -a 命令(Windows)来查找活动连接列表。

解决/绕过此问题的一种方法是允许 selenium 绑定到由您的代码生成的另一个端口。7000 到 7100 以上的大多数端口都可以使用。Selenium 内置的处理端口的方法是最初尝试绑定到 7055,如果失败,则绑定到 7054,如果失败则绑定到 7056。在大多数测试用例中,这很好,但我发现多个测试仍然遇到失败. 因此,不要使用默认值,而是指定您自己的配置文件。

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;

public class GoogleNavigationTest {
@Test
public void testApp(){

    // Specify a new or randomly generated port for the driver to use
    int genPort = 7052;
    // The Firefox driver supports javascript 
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    //In the profile, assign it a different port to use instead of 7054,7055,7056
    //In my tests, I have a method that will generate a port to use that is open
    firefoxProfile.Port = genPort; 
    WebDriver driver = new FirefoxDriver(firefoxProfile);

编辑:深入研究这个问题发现有两件事导致了这个错误。第一个是本地计算机上的 TCP / UDP 端口选择,第二个是 selenium 必须将基本 firefox 配置文件从驱动器复制到它的临时文件夹的时间量。传输越慢,端口绑定问题的可能性就越大。

要解决此问题,请使您的个人资料尽可能小。这可能涉及删除启动时生成的一些基本 firefox 文件。我的 Firefox 配置文件大小 > 5 MB。在我进行这项研究之前,我的个人资料大小超过 60 MB。在每次测试开始时,它会尝试将 60MB 传输到临时位置并绑定到锁定端口。

我的新代码没有让我失望

    var smallerProfile = @"C:\Firefox Profiles\SmallProfile";
    var genPort = new Random(); 

     FirefoxProfile profile = new FirefoxProfile(smallerProfile);
     profile.Clean();
     profile.Port = genPort.Next(7000, 7500);

我在每次主要运行开始时复制较小的配置文件。

于 2013-12-11T19:12:33.117 回答
1

Selenium v​​2.21 不支持 Firefox 17。事实上,Firefox 17 仅支持几天前发布的 v2.27 版本。

降级 Firefox 或更新 Selenium。

可能是也可能不是此特定错误的原因,但您必须执行上述操作之一才能使其有一半的机会使其正常工作。

于 2012-12-10T17:22:19.553 回答
0

根据此处找到的答案

这是因为在后台运行了不止一个 javaw.exe。查看此转到任务管理器并选择进程选项卡。您可以看到将有不止一个 javaw.exe 正在运行。一一选择进程javaw.exe,然后单击“结束进程”并尝试再次运行脚本。

于 2012-12-10T17:02:59.910 回答
-1

我刚刚使用了 chromeDriver,它工作正常。

于 2013-02-23T11:10:29.957 回答