2016-07-12 71 views
3

我想建立一個更好的方法來等待頁面加載後,每次點擊。 在我用什麼樣的瞬間是這樣的:Selenium Webdriver - 等待頁面完全加載Java和JavaScript(ajax/jQuery /動畫等)

public boolean waitForJSandJQueryToLoad() { 
    WebDriverWait wait = new WebDriverWait(webDriver, EnvironmentUtils.getPageLoadTimeout().intValue()); 
    // wait for jQuery to load 
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() { 
     @Override 
     public Boolean apply(WebDriver driver) { 
      try { 
       if (!((Boolean) callJS("return (window.jQuery != null) && (jQuery.active === 0);"))) { 
        log.info("JQUERY.ACTIVE IS WORKING AT THE MOMENT! jQuery.active= " + callJS("return jQuery.active")); 
       } 
       return (Boolean) callJS("return (window.jQuery != null) && (jQuery.active === 0);"); 
      } catch (Exception e) { 
       // no jQuery present 
       return true; 
      } 
     } 
    }; 
    // wait for Javascript to load 
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() { 
     @Override 
     public Boolean apply(WebDriver driver) { 
      if (!callJS("return document.readyState").toString().equals("complete")) { 
       log.info("document.readyState is not complete at the moment! status is->" + callJS("return document.readyState").toString()); 
      } 
      return callJS("return document.readyState").toString().equals("complete"); 
     } 
    }; 

    ExpectedCondition<Boolean> animationLoad = new ExpectedCondition<Boolean>() { 
     @Override 
     public Boolean apply(WebDriver driver) { 
      if (!callJS("return $(\":animated\").length").toString().equals("0")) { 
       log.info("Animation is currently executing on-page. value ->" + callJS("return $(\":animated\").length").toString()); 
      } 
      return callJS("return $(\":animated\").length").toString().equals("0"); 
     } 
    }; 

    return wait.until(jQueryLoad) && wait.until(jsLoad) && wait.until(animationLoad); 
} 
在某些情況下,我的測試中點擊一些按鈕,並等待表之後加載後仍無法

,但是當我跑的countRowsInTable方法(該方法在計算行硒指令表),它帶來了零,而實際的視覺不爲零可言,count行的命令是否正常工作,這裏的代碼,如果你想檢查一下:

public int countDisplayedRowsInTable(String tableId) { 
    waitForJSandJQueryToLoad(); 
    int count = 0; 
    List<WebElement> rows = webDriver.findElements(By.xpath("//table[@id='" + tableId + "']/tbody/tr")); 
    for (WebElement row : rows) { 
     if (row.isDisplayed()) 
      count++; 
    } 
    return count; 
} 

我敢肯定,我用我的waitForJSandJQueryToLoad方法覆蓋了任何東西,我希望你可以給我額外的陳述,也許我錯過了。 在此先感謝。

+0

您能否提供指向應用程序或應用程序的任何特定框架的鏈接? – Madhan

+0

我很抱歉,但我不被允許。你能讓我知道你在找什麼嗎? –

+0

而不是檢查所有這些條件,確定每次點擊後會發生什麼......就像加載器圖標正在運行或正在顯示某些文本一樣。如果等待該元素可見/不可見,那麼我們如何檢查您的函數正在工作或不是。它可能適用於某些網站,但不適用於其他人或您的應用程序..您可以至少告訴構建應用程序的框架[angular-js,ext-js ..] – Madhan

回答

0

我認爲你可以定義一個明確的等待,在代碼中繼續之前等待一定的條件發生。

public int countDisplayedRowsInTable(String tableId) { 
     String e = "//table[@id='" + tableId + "']"; 

     // Wait for the table to load 
     WebElement table = (new WebDriverWait(driver, intCustomWait)) 
      .until(ExpectedConditions.elementToBeClickable(By.xpath(e)); 

     // Get all the rows from within the table 
     List<WebElement> rows = table.findElements(By.xpath("./tbody/tr")); 
     if(rows.size() > 0) 
     { 
      int count = 0; 
      for (WebElement row : rows) { 
       if (row.isDisplayed()) 
       count++; 
      } 
      return count; 
     } 
     else return -1; 
    } 
+0

感謝您的評論,我試圖看看ExpectedConditions.elementToBeClickable By.id的表,但它似乎不工作。根據其代碼加載的ajax/jquery。但再次,我試圖用我的waitForJSandJQueryToLoad,也許我需要結合這兩種方法,以使其工作。 –

+0

嘿,我只是試過你的方法,它不工作,因爲在表中沒有行但仍然顯示的情況下,Xseconds等待元素可點擊後超時。 elementToBeClickable似乎目前沒有解決問題,謝謝。 –

相關問題