Search in sources :

Example 61 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project carina by qaprosoft.

the class AbstractPage method waitForJSToLoad.

/**
 * Waits till JS and jQuery (if applicable for the page) are completely processed on the page
 *
 * @param timeout Completing of JS loading will be verified within specified timeout
 */
public void waitForJSToLoad(long timeout) {
    // wait for jQuery to load
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    ExpectedCondition<Boolean> jQueryLoad = new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            try {
                return ((Long) executor.executeScript("return jQuery.active") == 0);
            } catch (Exception e) {
                return true;
            }
        }
    };
    // wait for Javascript to load
    ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            return executor.executeScript("return document.readyState").toString().equals("complete");
        }
    };
    String errMsg = "JS was not loaded on page during expected time";
    if ((Boolean) executor.executeScript("return window.jQuery != undefined")) {
        Assert.assertTrue(waitUntil(jQueryLoad, timeout) && waitUntil(jsLoad, timeout), errMsg);
    } else {
        Assert.assertTrue(waitUntil(jsLoad, timeout), errMsg);
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) IOException(java.io.IOException) DocumentException(com.itextpdf.text.DocumentException)

Aggregations

ExpectedCondition (org.openqa.selenium.support.ui.ExpectedCondition)61 WebDriver (org.openqa.selenium.WebDriver)57 WebElement (org.openqa.selenium.WebElement)46 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)46 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)32 By (org.openqa.selenium.By)19 Wait (org.openqa.selenium.support.ui.Wait)19 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 List (java.util.List)15 WebDriverException (org.openqa.selenium.WebDriverException)15 Test (org.testng.annotations.Test)15 NoAlertPresentException (org.openqa.selenium.NoAlertPresentException)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 Map (java.util.Map)13 ExpectedConditions (org.openqa.selenium.support.ui.ExpectedConditions)12 AfterMethod (org.testng.annotations.AfterMethod)12 ArrayList (java.util.ArrayList)11 Iterator (java.util.Iterator)11 Optional (java.util.Optional)11