1

MVN テスト コマンド ラインを使用してセレン テストを実行しようとすると、このエラーが発生します。不思議なことに、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) を使用して、アクティブな接続のリストを見つけます。

この問題を回避/回避する方法は、セレンがコードによって生成された別のポートにバインドできるようにすることです。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);

編集: この問題を深く掘り下げると、2 つのことがこのエラーに寄与していることが明らかになりました。1 つ目はローカル マシンでの TCP / UDP ポートの選択であり、2 つ目は、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 が実行されているためです。この Goto タスク マネージャを表示するには、[プロセス] タブを選択します。複数の javaw.exe が実行されていることがわかります。プロセス javaw.exe を 1 つずつ選択し、[プロセスの終了] をクリックして、もう一度スクリプトを実行してみてください。

于 2012-12-10T17:02:59.910 に答える
-1

chromeDriver を使用したところ、問題なく動作します。

于 2013-02-23T11:10:29.957 に答える