2016-04-02 20 views
3

私は、SeleniumとSelenium IDEでテストしている、かなりajax-heavyなWebアプリケーションを持っています。最終的な提出まですべてがうまく動作します。通常は処理中の未処理のajaxリクエスト数(通常は約20)のためにエラーになります。セレンをすべてのAjaxリクエストが完了するのを待つようにする方法はありますか?私はwaitForEval Value = "$。アクティブ== 0"(下図)を試しましたが、何もしないようです。enter image description hereSelenium IDEですべてのajaxリクエストが完了するまで待つ方法は?

これはSelenium IDEで可能ですか?

注 - ビジネスタイプと私はスクリプトを前後に渡しているため、IDEを使用する必要があります。

答えて

4

回答はかなり単純であることが判明しました。 waitForEvalコマンドでctiveリクエスト(selenium.browserbot.getUserWindow()。$。active)を呼び出し、値を0に設定すると、すべてideで発生します。

+2

単純に 'waitForEval |ウィンドウ。$。アクティブ| 0 ' –

1

しばらくIDEを使用していません。これは私がWebDriverに使うものです。しかし、アルゴリズムは翻訳されます。 JavaScriptはJavaScriptです。つまり、それはあなたのフレームワークに依存します。私が使用してプロトタイプのために

public boolean waitForAngularToLoad(WebDriver driver, int waitTimeInSeconds) { 

    WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L); 

    ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() { 

     public Boolean apply(WebDriver driver) { 
     try { 
      return ((Boolean)((JavascriptExecutor)driver).executeScript(
        "return angular.element(document.body).injector().get(\'$http\').pendingRequests.length == 0;" 
       )); 
     } 
     catch (Exception e) { 
      // Angular not found 
      log.info("Not found: " + "return angular.element(document.body).injector().get(\'$http\').pendingRequests.length == 0;"); 
      return true; 
     } 
     } 
    }; 

    // wait for browser readystate complete; it is arguable if selenium does this all the time 
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() { 

     public Boolean apply(WebDriver driver) { 
     return ((JavascriptExecutor)driver).executeScript("return document.readyState;") 
     .toString().equals("complete"); 
     } 
    }; 

    return wait.until(libraryLoad) && wait.until(jsLoad); 

} 

:アンギュラについては

、私はこの使用

public boolean waitForPrototypeToLoad(WebDriver driver, int waitTimeInSeconds) { 

    WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L); 

    // wait for jQuery to load 
    ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() { 

     public Boolean apply(WebDriver driver) { 
     try { 
      return ((Boolean)((JavascriptExecutor)driver).executeScript("return Ajax.activeRequestCount == 0;")); 
     } 
     catch (Exception e) { 
      // Prototype not found 
      log.info("Not found: " + "return Ajax.activeRequestCount == 0;"); 
      return true; 
     } 
     } 
    }; 

    // wait for browser readystate complete; it is arguable if selenium does this all the time 
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() { 

     public Boolean apply(WebDriver driver) { 
     return ((JavascriptExecutor)driver).executeScript("return document.readyState;") 
     .toString().equals("complete"); 
     } 
    }; 

    return wait.until(libraryLoad) && wait.until(jsLoad); 

} 

をjQueryのために、私は誰もがそれをしない、あなたはスピナーロジックのための待ち時間をカスタマイズする必要がある(これを使用します異なって):

public boolean waitForJSandJQueryToLoad(WebDriver driver, long waitTimeInSeconds) { 

    WebDriverWait wait = new WebDriverWait(driver, waitTimeInSeconds, 2000L); 

    /* 
    * If you are curious about what follows see: 
    * http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/ExpectedCondition.html 
    * 
    * We are creating an anonymous class that inherits from ExpectedCondition and then implements interface 
    * method apply(...) 
    */ 
    ExpectedCondition<Boolean> libraryLoad = new ExpectedCondition<Boolean>() { 

     public Boolean apply(WebDriver driver) { 
     boolean isAjaxFinished = false; 
     boolean isLoaderSpinning = false; 
     boolean isPageLoadComplete = false; 
     try { 
      isAjaxFinished = ((Boolean)((JavascriptExecutor)driver).executeScript("return jQuery.active == 0;")); 
     } catch (Exception e) { 
      // no Javascript library not found 
      isAjaxFinished = true; 
     } 
     try { // Check your page, not everyone uses class=spinner 
      // Reduce implicit wait time for spinner 
      driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS); 

//    isLoaderSpinning = driver.findElement(By.className("spinner")).isDisplayed(); // This is the default 
      // Next was modified for GoComics 
      isLoaderSpinning = driver.findElement(By.cssSelector("#progress_throbber > ul > li:nth-child(1) > img[alt='spinner']")).isDisplayed(); 

      if (isLoaderSpinning) 
       log.info("jquery loader is spinning"); 
     } catch (Exception f) { 
      // no loading spinner found 
      isLoaderSpinning = false; 
     } finally { // Restore implicit wait time to default 
      driver.manage().timeouts().implicitlyWait(new DriverFactory().getImplicitWait(), TimeUnit.SECONDS); 
     } 
     isPageLoadComplete = ((JavascriptExecutor)driver).executeScript("return document.readyState;") 
       .toString().equals("complete"); 
     if (!(isAjaxFinished & !(isLoaderSpinning) & isPageLoadComplete)) 
      log.info(isAjaxFinished + ", " + !(isLoaderSpinning) +", " + isPageLoadComplete); 

     return isAjaxFinished & !(isLoaderSpinning) & isPageLoadComplete; 
     } 
    }; // Terminates statement started by ExpectedCondition<Boolean> libraryLoad = ... 

    return wait.until(libraryLoad); 
} 
0

最後に、問題を解決するとAJAXが追加されました。私は数日間それをテストしています - 独創的な解決策です。付録は、すべてのステップでセレンが自動的にAJAXの実行を待つようにしました。

https://addons.mozilla.org/pl/firefox/addon/sideex/

関連する問題