Pages

WebDriverWait Reusable Methods

How to check page is loaded completely or not?

public static void waitForPageLoaded(WebDriver driver) {

         ExpectedCondition<Boolean> expectation = new
    ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
              return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
            }
          };

          WebDriverWait wait = new WebDriverWait(driver,30);
          try {
                  wait.until(expectation);
          } catch(Throwable error) {
                  Assert.assertFalse(true, "Timeout waiting for Page Load Request to complete.");
          }
     } 
And you can call this method into your function. Since it is a static method, you can directly call with the class name.
public class Test(){
    WebDriver driver;

    @Test
    public void testing(){
         driver = new FirefoxDriver();
         driver.get("http://www.gmail.com");
         Functions.waitForPageLoaded(driver);
   }
}

======================================================


 public void waitForPageLoadAndTitleContains(int timeout, String pageTitle) {

    WebDriverWait wait = new WebDriverWait(driver, timeout, 1000);
    wait.until(ExpectedConditions.titleContains(pageTitle));
}

public void waitForElementPresence(By locator, int seconds) {
    WebDriverWait wait = new WebDriverWait(driver, seconds);
    wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}

public void jsWaitForPageToLoad(int timeOutInSeconds) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String jsCommand = "return document.readyState";

    // Validate readyState before doing any waits
    if (js.executeScript(jsCommand).toString().equals("complete")) {
        return;
    }

    for (int i = 0; i < timeOutInSeconds; i++) {
        TimeManager.waitInSeconds(3);
        if (js.executeScript(jsCommand).toString().equals("complete")) {
            break;
        }
    }
}

   /**
     * Looks for a visible OR invisible element via the provided locator for up
     * to maxWaitTime. Returns as soon as the element is found.
     *
     * @param byLocator
     * @param maxWaitTime - In seconds
     * @return
     *
     */
    public WebElement findElementThatIsPresent(final By byLocator, int maxWaitTime) {
        if (driver == null) {
            nullDriverNullPointerExeption();
        }
        FluentWait<WebDriver> wait = new FluentWait<>(driver).withTimeout(maxWaitTime, java.util.concurrent.TimeUnit.SECONDS)
                .pollingEvery(200, java.util.concurrent.TimeUnit.MILLISECONDS);

        try {
            return wait.until((WebDriver webDriver) -> {
                List<WebElement> elems = driver.findElements(byLocator);
                if (elems.size() > 0) {
                    return elems.get(0);
                } else {
                    return null;
                }
            });
        } catch (Exception e) {
            return null;
        }
    }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.