2012-12-10 67 views
1

當我嘗試使用MVN測試命令行運行我的硒測試時出現此錯誤。奇怪的是,我嘗試了4天前,它成功運行:無法在45000毫秒內綁定到鎖定端口70 54

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

Hv您驗證了端口7054上的sumtintin runnin。另外如果你不使用一些特定的firefoxprofile,那麼就不需要創建新的ff配置文件。 –

+1

你有什麼版本的Firefox?你有什麼版本的Selenium? – Arran

+0

我沒有創建任何Firefox的配置文件 – AmiraGL

回答

-1

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

0

基於答案發現,因爲一個以上的javaw.exe的here

其正在後臺運行。 要查看此Goto任務管理器並選擇進程選項卡。 您可以看到將有多個javaw.exe正在運行。 逐個選擇進程javaw.exe並點擊「結束進程」,然後再次運行腳本。

+0

我也試過了,但錯誤仍然存​​在 – AmiraGL

1

Selenium v​​2.21不支持Firefox 17.事實上,Firefox 17只支持前兩天發佈的v2.27版本。

可以降級Firefox或更新Selenium。

可能會或可能不會成爲此特定錯誤的原因,但您必須完成上述操作之一,甚至有一半的機會使其工作。

4

最新回覆,但試試這個更新的代碼。 Selenium Webdriver將實例綁定到特定的TCP端口。如果您的測試失敗並且駕駛員沒有正確關閉,該端口將保持x時間。使用driver.Quit()通常會釋放TCP端口。

要測試以查看哪些端口當前仍可以保留,可以使用netstat -a命令(窗口)查找活動連接的列表。

一種解決/側步問題的方法是允許selenium綁定到由您的代碼生成的另一個端口。大多數7000到7100以上的端口都可以使用。 Selenium的內置方式來處理端口是最初嘗試綁定到7055,如果失敗,綁定到7054,如果失敗綁定到7056.在MOST測試案例中,這很好,但我發現多個測試仍然遇到失敗。因此,不要使用默認設置,請指定您自己的配置文件。

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端口選擇,第二個是硒必須將基本的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); 

我複製在更小的外形在每個主運行的開始。

相關問題