Search in sources :

Example 56 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project scout.rt by eclipse.

the class SeleniumExpectedConditions method tableToHaveNumberOfRows.

/**
 * @param parentElement
 *          if not null, findElement below the given parent, if null, findElements in document
 * @param numRows
 * @return The table-rows found by the expected condition
 */
public static ExpectedCondition<List<WebElement>> tableToHaveNumberOfRows(final WebElement parentElement, final int numRows) {
    return new ExpectedCondition<List<WebElement>>() {

        @Override
        public List<WebElement> apply(WebDriver driver) {
            try {
                By by = By.className("table-row");
                List<WebElement> tableRows = parentElement != null ? parentElement.findElements(by) : driver.findElements(by);
                if (numRows == tableRows.size()) {
                    return tableRows;
                }
            } catch (StaleElementReferenceException e) {
            // NOSONAR
            // NOP
            }
            return null;
        }

        @Override
        public String toString() {
            return String.format("table should have %d rows", numRows);
        }
    };
}
Also used : WebDriver(org.openqa.selenium.WebDriver) By(org.openqa.selenium.By) StaleElementReferenceException(org.openqa.selenium.StaleElementReferenceException) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) WebElement(org.openqa.selenium.WebElement)

Example 57 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project selenium-tests by Wikia.

the class CommonExpectedConditions method elementInViewPort.

public static ExpectedCondition<Boolean> elementInViewPort(final WebElement element) {
    return new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            Dimension size = element.getSize();
            Point location = element.getLocation();
            if (((size.height + location.y) > -1) && (size.width + location.x > -1)) {
                return true;
            }
            return false;
        }

        @Override
        public String toString() {
            return String.format("Element ('%s') not in viewport!", element.getTagName());
        }
    };
}
Also used : ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) Dimension(org.openqa.selenium.Dimension) Point(org.openqa.selenium.Point)

Example 58 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project selenium-tests by Wikia.

the class CommonExpectedConditions method elementToHaveColor.

/**
 * @param accuracy in percentage between 0 and 100.
 */
public static ExpectedCondition<Boolean> elementToHaveColor(final WebElement element, final Color color, final int accuracy) {
    final Shooter shooter = new Shooter();
    final ImageComparison imageComparison = new ImageComparison();
    return new ExpectedCondition<Boolean>() {

        @Override
        public Boolean apply(WebDriver driver) {
            BufferedImage image = shooter.takeScreenshot(element, driver);
            return imageComparison.isColorImage(image, color, accuracy);
        }

        @Override
        public String toString() {
            return String.format("At least %s percents of element does not have %s color", (100 - accuracy), color.toString());
        }
    };
}
Also used : Shooter(com.wikia.webdriver.common.core.imageutilities.Shooter) ImageComparison(com.wikia.webdriver.common.core.imageutilities.ImageComparison) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) BufferedImage(java.awt.image.BufferedImage)

Example 59 with ExpectedCondition

use of org.openqa.selenium.support.ui.ExpectedCondition in project selenium-tests by Wikia.

the class IntraWikiSearchPageObject method verifyNamespace.

public void verifyNamespace(String namespace) {
    driver.manage().timeouts().implicitlyWait(250, TimeUnit.MILLISECONDS);
    try {
        new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) webDriver -> !titles.isEmpty());
    } finally {
        restoreDefaultImplicitWait();
    }
    Assertion.assertTrue(titles.get(0).getText().startsWith(namespace));
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Assertion(com.wikia.webdriver.common.core.Assertion) URLsContent(com.wikia.webdriver.common.contentpatterns.URLsContent) Select(org.openqa.selenium.support.ui.Select) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) SearchPageObject(com.wikia.webdriver.pageobjectsfactory.pageobject.SearchPageObject) org.openqa.selenium(org.openqa.selenium) Log(com.wikia.webdriver.common.logging.Log) Collectors(java.util.stream.Collectors) FindBy(org.openqa.selenium.support.FindBy) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait)

Example 60 with ExpectedCondition

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

the class WaitUtils method waitForPageToLoad.

/**
 * Waits for page to finish any pending redirects, REST API requests etc.
 * Because Keycloak's Admin Console is a single-page application, we need to
 * take extra steps to ensure the page is fully loaded
 */
public static void waitForPageToLoad() {
    WebDriver driver = getCurrentDriver();
    if (driver instanceof HtmlUnitDriver) {
        // not needed
        return;
    }
    String currentUrl = null;
    // Ensure the URL is "stable", i.e. is not changing anymore; if it'd changing, some redirects are probably still in progress
    for (int maxRedirects = 4; maxRedirects > 0; maxRedirects--) {
        currentUrl = driver.getCurrentUrl();
        FluentWait<WebDriver> wait = new FluentWait<>(driver).withTimeout(Duration.ofMillis(250));
        try {
            wait.until(not(urlToBe(currentUrl)));
        } catch (TimeoutException e) {
            // URL has not changed recently - ok, the URL is stable and page is current
            break;
        }
        if (maxRedirects == 1) {
            log.warn("URL seems unstable! (Some redirect are probably still in progress)");
        }
    }
    WebDriverWait wait = new WebDriverWait(getCurrentDriver(), PAGELOAD_TIMEOUT_MILLIS / 1000);
    ExpectedCondition waitCondition = null;
    // Different wait strategies for Admin and Account Consoles
    if (currentUrl.matches("^[^\\/]+:\\/\\/[^\\/]+\\/auth\\/admin\\/.*$")) {
        // Admin Console
        // Checks if the document is ready and asks AngularJS, if present, whether there are any REST API requests in progress
        waitCondition = javaScriptThrowsNoExceptions("if (document.readyState !== 'complete' " + "|| (typeof angular !== 'undefined' && angular.element(document.body).injector().get('$http').pendingRequests.length !== 0)) {" + "throw \"Not ready\";" + "}");
    } else if (// check for new Account Console URL
    currentUrl.matches("^[^\\/]+:\\/\\/[^\\/]+\\/auth\\/realms\\/[^\\/]+\\/account\\/.*#/.+$")) {
        // TODO rework this temporary workaround once KEYCLOAK-11201 and/or KEYCLOAK-8181 are fixed
        pause(2000);
    }
    if (waitCondition != null) {
        try {
            wait.until(waitCondition);
        } catch (TimeoutException e) {
            log.warn("waitForPageToLoad time exceeded!");
        }
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) ExpectedCondition(org.openqa.selenium.support.ui.ExpectedCondition) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) TimeoutException(org.openqa.selenium.TimeoutException)

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