All Downloads are FREE. Search and download functionalities are using the official Maven repository.

aquality.selenium.core.waitings.ConditionalWait Maven / Gradle / Ivy

Go to download

Library with core functions simplifying work with Selenium-controlled applications.

There is a newer version: 4.2.0
Show newest version
package aquality.selenium.core.waitings;

import aquality.selenium.core.applications.IApplication;
import aquality.selenium.core.configurations.ITimeoutConfiguration;
import com.google.common.base.Strings;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
import java.util.concurrent.TimeoutException;
import java.util.function.BooleanSupplier;

/**
 * 

ConditionalWait class.

* * @author lenovo * @version $Id: $Id */ public class ConditionalWait implements IConditionalWait { private final Provider applicationProvider; private final ITimeoutConfiguration timeoutConfiguration; @Inject /** *

Constructor for ConditionalWait.

* * @param applicationProvider a {@link com.google.inject.Provider} object. * @param timeoutConfiguration a {@link aquality.selenium.core.configurations.ITimeoutConfiguration} object. */ public ConditionalWait(Provider applicationProvider, ITimeoutConfiguration timeoutConfiguration) { this.applicationProvider = applicationProvider; this.timeoutConfiguration = timeoutConfiguration; } /** {@inheritDoc} */ @Override public boolean waitFor(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) { try { waitForTrue(condition, timeout, pollingInterval, message, exceptionsToIgnore); return true; } catch (TimeoutException e) { return false; } } /** {@inheritDoc} */ @Override public void waitForTrue(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) throws TimeoutException { BooleanSupplier supplier = Optional.ofNullable(condition).orElseThrow(() -> new IllegalArgumentException("Condition cannot be null")); long timeoutInSeconds = resolveConditionTimeoutInSeconds(timeout); long pollingIntervalInMilliseconds = resolvePollingInterval(pollingInterval).toMillis(); String exMessage = resolveMessage(message); double startTime = getCurrentTime(); while (true) { if (isConditionSatisfied(supplier, exceptionsToIgnore)) { return; } double currentTime = getCurrentTime(); if ((currentTime - startTime) > timeoutInSeconds) { String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeoutInSeconds, exMessage); throw new TimeoutException(exceptionMessage); } try { Thread.sleep(pollingIntervalInMilliseconds); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } /** {@inheritDoc} */ @Override public T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) { IApplication app = applicationProvider.get(); app.setImplicitWaitTimeout(Duration.ZERO); long timeoutInSeconds = resolveConditionTimeoutInSeconds(timeout); Duration actualPollingInterval = resolvePollingInterval(pollingInterval); String exMessage = resolveMessage(message); WebDriverWait wait = new WebDriverWait(app.getDriver(), timeoutInSeconds); wait.pollingEvery(actualPollingInterval); wait.withMessage(exMessage); wait.ignoreAll(exceptionsToIgnore == null ? Collections.singleton(StaleElementReferenceException.class) : exceptionsToIgnore); try { return wait.until(condition); } finally { app.setImplicitWaitTimeout(timeoutConfiguration.getImplicit()); } } private double getCurrentTime() { return System.nanoTime() / Math.pow(10, 9); } private boolean isConditionSatisfied(BooleanSupplier condition, Collection> exceptionsToIgnore) { try { return condition.getAsBoolean(); } catch (Exception e) { if (exceptionsToIgnore == null || !exceptionsToIgnore.contains(e.getClass())) { throw e; } return false; } } private long resolveConditionTimeoutInSeconds(Duration timeout) { return Optional.ofNullable(timeout).orElse(timeoutConfiguration.getCondition()).getSeconds(); } private Duration resolvePollingInterval(Duration pollingInterval) { return Optional.ofNullable(pollingInterval).orElse(timeoutConfiguration.getPollingInterval()); } private String resolveMessage(String message) { return Strings.isNullOrEmpty(message) ? "" : message; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy