2016-03-01 132 views
0

我檢查我的硒腳本,如果Ajax調用完成後,我第一次檢查,如果文件已經準備好,然後檢查是否jQuery的定義,然後檢查工作狀態不確定jQuery是內部的document.ready

public class WaitForAjaxToLoad { 
public static void waitForAjaxToLoad(WebDriver driver) { 

     final JavascriptExecutor js= (JavascriptExecutor) driver; 
     WebDriverWait wait = new WebDriverWait(driver, 15); 
     ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() { 
      public Boolean apply(WebDriver driver) { 

       if(js.executeScript("return document.readyState").equals("complete")) 
       { 
        if((boolean) js.executeScript("return window.jQuery != undefined")) { 
         Boolean status= (Boolean) js.executeScript("return jQuery.active==0"); 
         System.out.println("Jquery is defined active status:"+status+"for thread:"+Thread.currentThread().getId()); 
         if (status) { 
          return true; 
         } else { 
          return false; 
         } 
        } 
        else { 
         System.out.println("jQuery is undefined for thread "+Thread.currentThread().getId()); 
         return false; 
        } 
       } 
       else { 
        System.out.println("Document is not ready for thread "+Thread.currentThread().getId()); 
        return false; 
       } 


      } 
     }; 
     wait.until(e); 

} 
} 

當我執行腳本時,文檔已準備就緒,但jQuery未定義。 這種情況發生在並行執行測試方法時。 爲什麼我一旦文檔準備就緒,jQuery不確定?

回答

0

適應this answer爲Java,您的測試可以大大簡化 - 不,在我看來,失去任何有價值的東西:

WebDriverWait wait = new WebDriverWait(driver, 15); 
wait.until(new Function<WebDriver, WebElement>() { 
    public Boolean apply(WebDriver driver) { 
     return js.executeScript("return jQuery.active == 0"); 
    } 
});