Search in sources :

Example 16 with Wait

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

the class AbstractUIObjectListHandler method waitUntil.

/**
 * Wait until any condition happens.
 *
 * @param condition - ExpectedCondition.
 * @return true if condition happen.
 */
@SuppressWarnings("unchecked")
private boolean waitUntil(ExpectedCondition<?> condition) {
    boolean result;
    long timeout = Configuration.getLong(Parameter.EXPLICIT_TIMEOUT);
    long RETRY_TIME = Configuration.getLong(Parameter.RETRY_INTERVAL);
    @SuppressWarnings("rawtypes") Wait wait = new WebDriverWait(webDriver, timeout, RETRY_TIME).ignoring(WebDriverException.class).ignoring(NoSuchSessionException.class);
    try {
        wait.until(condition);
        result = true;
        LOGGER.debug("waitUntil: finished true...");
    } catch (NoSuchElementException | TimeoutException e) {
        // don't write exception even in debug mode
        LOGGER.debug("waitUntil: NoSuchElementException | TimeoutException e..." + condition.toString());
        result = false;
    } catch (Exception e) {
        LOGGER.error("waitUntil: " + condition.toString(), e);
        result = false;
    }
    return result;
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Wait(org.openqa.selenium.support.ui.Wait) NoSuchElementException(org.openqa.selenium.NoSuchElementException) WebDriverException(org.openqa.selenium.WebDriverException) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) StaleElementReferenceException(org.openqa.selenium.StaleElementReferenceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) TimeoutException(org.openqa.selenium.TimeoutException) NoSuchSessionException(org.openqa.selenium.NoSuchSessionException) WebDriverException(org.openqa.selenium.WebDriverException) TimeoutException(org.openqa.selenium.TimeoutException)

Example 17 with Wait

use of org.openqa.selenium.support.ui.Wait in project java-client by appium.

the class AppiumFluentWaitTest method testIntervalCalculationForCustomStrategy.

@Test
public void testIntervalCalculationForCustomStrategy() {
    final FakeElement el = new FakeElement();
    final AtomicInteger callsCounter = new AtomicInteger(0);
    // Linear dependency
    final Function<Long, Long> pollingStrategy = x -> x * 2;
    final Wait<FakeElement> wait = new AppiumFluentWait<>(el, new SystemClock(), duration -> {
        int callNumber = callsCounter.incrementAndGet();
        assertThat(duration.in(TimeUnit.SECONDS), is(equalTo(pollingStrategy.apply((long) callNumber))));
        Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
    }).withPollingStrategy(info -> new Duration(pollingStrategy.apply(info.getNumber()), TimeUnit.SECONDS)).withTimeout(4, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
    try {
        wait.until(FakeElement::isDisplayed);
        Assert.fail("TimeoutException is expected");
    } catch (TimeoutException e) {
        // this is expected
        assertThat(callsCounter.get(), is(equalTo(2)));
    }
}
Also used : Duration(org.openqa.selenium.support.ui.Duration) Wait(org.openqa.selenium.support.ui.Wait) SystemClock(org.openqa.selenium.support.ui.SystemClock) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Test(org.junit.Test) Function(java.util.function.Function) TimeUnit(java.util.concurrent.TimeUnit) TimeoutException(org.openqa.selenium.TimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) AppiumFluentWait(io.appium.java_client.AppiumFluentWait) SystemClock(org.openqa.selenium.support.ui.SystemClock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(org.openqa.selenium.support.ui.Duration) TimeoutException(org.openqa.selenium.TimeoutException) Test(org.junit.Test)

Example 18 with Wait

use of org.openqa.selenium.support.ui.Wait in project java-client by appium.

the class AppiumFluentWaitTest method testCustomStrategyOverridesDefaultInterval.

@Test
public void testCustomStrategyOverridesDefaultInterval() {
    final FakeElement el = new FakeElement();
    final AtomicInteger callsCounter = new AtomicInteger(0);
    final Wait<FakeElement> wait = new AppiumFluentWait<>(el, new SystemClock(), duration -> {
        callsCounter.incrementAndGet();
        assertThat(duration.in(TimeUnit.SECONDS), is(equalTo(2L)));
        Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
    }).withPollingStrategy(info -> new Duration(2, TimeUnit.SECONDS)).withTimeout(3, TimeUnit.SECONDS).pollingEvery(1, TimeUnit.SECONDS);
    try {
        wait.until(FakeElement::isDisplayed);
        Assert.fail("TimeoutException is expected");
    } catch (TimeoutException e) {
        // this is expected
        assertThat(callsCounter.get(), is(equalTo(2)));
    }
}
Also used : Duration(org.openqa.selenium.support.ui.Duration) Wait(org.openqa.selenium.support.ui.Wait) SystemClock(org.openqa.selenium.support.ui.SystemClock) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Test(org.junit.Test) Function(java.util.function.Function) TimeUnit(java.util.concurrent.TimeUnit) TimeoutException(org.openqa.selenium.TimeoutException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Is.is(org.hamcrest.core.Is.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assert(org.junit.Assert) AppiumFluentWait(io.appium.java_client.AppiumFluentWait) SystemClock(org.openqa.selenium.support.ui.SystemClock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(org.openqa.selenium.support.ui.Duration) TimeoutException(org.openqa.selenium.TimeoutException) Test(org.junit.Test)

Example 19 with Wait

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

the class DriverHelper method isElementPresent.

@Deprecated
public boolean isElementPresent(String elementName, final By by, long timeout) {
    boolean result;
    final WebDriver drv = getDriver();
    wait = new WebDriverWait(drv, timeout, RETRY_TIME);
    try {
        setImplicitTimeout(0);
        wait.until((Function<WebDriver, Object>) dr -> !dr.findElements(by).isEmpty() && dr.findElement(by).isDisplayed());
        result = true;
    } catch (Exception e) {
        result = false;
    } finally {
        setImplicitTimeout(IMPLICIT_TIMEOUT);
    }
    return result;
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Wait(org.openqa.selenium.support.ui.Wait) SpecialKeywords(com.qaprosoft.carina.core.foundation.commons.SpecialKeywords) Parameter(com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter) Messager(com.qaprosoft.carina.core.foundation.utils.Messager) Set(java.util.Set) Configuration(com.qaprosoft.carina.core.foundation.utils.Configuration) ExtendedWebElement(com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement) Action(org.openqa.selenium.interactions.Action) Function(java.util.function.Function) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Logger(org.apache.log4j.Logger) BaseMatcher(org.hamcrest.BaseMatcher) List(java.util.List) CommonUtils(com.qaprosoft.carina.core.foundation.utils.common.CommonUtils) Matcher(java.util.regex.Matcher) CryptoTool(com.qaprosoft.carina.core.foundation.crypto.CryptoTool) AbstractPage(com.qaprosoft.carina.core.gui.AbstractPage) DevicePool(com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool) Pattern(java.util.regex.Pattern) org.openqa.selenium(org.openqa.selenium) Actions(org.openqa.selenium.interactions.Actions) LogicUtils(com.qaprosoft.carina.core.foundation.utils.LogicUtils) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait)

Example 20 with Wait

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

the class DriverHelper method isTitleAsExpected.

/**
 * Checks that page title is as expected.
 *
 * @param expectedTitle
 *            Expected title
 * @return validation result.
 */
public boolean isTitleAsExpected(final String expectedTitle) {
    boolean result;
    final String decryptedExpectedTitle = cryptoTool.decryptByPattern(expectedTitle, CRYPTO_PATTERN);
    final WebDriver drv = getDriver();
    wait = new WebDriverWait(drv, EXPLICIT_TIMEOUT, RETRY_TIME);
    try {
        wait.until((Function<WebDriver, Object>) dr -> drv.getTitle().contains(decryptedExpectedTitle));
        result = true;
        Messager.TITLE_CORERECT.info(drv.getCurrentUrl(), expectedTitle);
    } catch (Exception e) {
        result = false;
        Messager.TITLE_NOT_CORERECT.error(drv.getCurrentUrl(), expectedTitle, drv.getTitle());
    }
    return result;
}
Also used : WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) Wait(org.openqa.selenium.support.ui.Wait) SpecialKeywords(com.qaprosoft.carina.core.foundation.commons.SpecialKeywords) Parameter(com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter) Messager(com.qaprosoft.carina.core.foundation.utils.Messager) Set(java.util.Set) Configuration(com.qaprosoft.carina.core.foundation.utils.Configuration) ExtendedWebElement(com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement) Action(org.openqa.selenium.interactions.Action) Function(java.util.function.Function) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Logger(org.apache.log4j.Logger) BaseMatcher(org.hamcrest.BaseMatcher) List(java.util.List) CommonUtils(com.qaprosoft.carina.core.foundation.utils.common.CommonUtils) Matcher(java.util.regex.Matcher) CryptoTool(com.qaprosoft.carina.core.foundation.crypto.CryptoTool) AbstractPage(com.qaprosoft.carina.core.gui.AbstractPage) DevicePool(com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool) Pattern(java.util.regex.Pattern) org.openqa.selenium(org.openqa.selenium) Actions(org.openqa.selenium.interactions.Actions) LogicUtils(com.qaprosoft.carina.core.foundation.utils.LogicUtils) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait)

Aggregations

Wait (org.openqa.selenium.support.ui.Wait)29 WebDriverWait (org.openqa.selenium.support.ui.WebDriverWait)27 WebDriver (org.openqa.selenium.WebDriver)23 JavascriptExecutor (org.openqa.selenium.JavascriptExecutor)22 WebElement (org.openqa.selenium.WebElement)22 ExpectedCondition (org.openqa.selenium.support.ui.ExpectedCondition)22 Function (java.util.function.Function)9 TimeoutException (org.openqa.selenium.TimeoutException)7 SpecialKeywords (com.qaprosoft.carina.core.foundation.commons.SpecialKeywords)6 CryptoTool (com.qaprosoft.carina.core.foundation.crypto.CryptoTool)6 Configuration (com.qaprosoft.carina.core.foundation.utils.Configuration)6 Parameter (com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter)6 LogicUtils (com.qaprosoft.carina.core.foundation.utils.LogicUtils)6 Messager (com.qaprosoft.carina.core.foundation.utils.Messager)6 CommonUtils (com.qaprosoft.carina.core.foundation.utils.common.CommonUtils)6 ExtendedWebElement (com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement)6 AbstractPage (com.qaprosoft.carina.core.gui.AbstractPage)6 List (java.util.List)6 Matcher (java.util.regex.Matcher)6 Pattern (java.util.regex.Pattern)6