【问题标题】:Unable to bind to locking port 70 54 within 45000 ms无法在 45000 毫秒内绑定到锁定端口 70 54
【发布时间】:2012-11-28 00:33:13
【问题描述】:

当我尝试使用 MVN 测试命令行运行我的 selenium 测试时,我遇到了这个错误。奇怪的是,我三天前试了一下,运行成功:

------------------------------------------------------
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());
    }
     }
   }

【问题讨论】:

  • 您已验证端口 7054 上是否有 sumtin 运行。此外,如果您不使用某些特定的 Firefox 配置文件,则无需创建新的 ff 配置文件。
  • 你有什么版本的火狐?你有什么版本的 Selenium?
  • 我没有创建任何 Firefox 配置文件

标签: maven selenium selenium-webdriver


【解决方案1】:

回复晚了,但试试这个更新的代码。 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);

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

【讨论】:

    【解决方案2】:

    我刚刚使用了 chromeDriver,它运行良好。

    【讨论】:

      【解决方案3】:

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

      降级 Firefox 或更新 Selenium。

      可能是也可能不是此特定错误的原因,但您必须执行上述操作之一,甚至有一半的机会让它工作。

      【讨论】:

        【解决方案4】:

        基于找到的答案here

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-02
          • 2012-09-17
          • 2013-04-27
          相关资源
          最近更新 更多