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;

public class ConditionalWait implements IConditionalWait {

    private final Provider applicationProvider;
    private final ITimeoutConfiguration timeoutConfiguration;

    @Inject
    public ConditionalWait(Provider applicationProvider, ITimeoutConfiguration timeoutConfiguration) {
        this.applicationProvider = applicationProvider;
        this.timeoutConfiguration = timeoutConfiguration;
    }

    @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;
        }
    }

    @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"));
        Duration currentTimeout = resolveConditionTimeout(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) > currentTimeout.getSeconds()) {
                String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", currentTimeout.getSeconds(), exMessage);
                throw new TimeoutException(exceptionMessage);
            }

            try {
                Thread.sleep(pollingIntervalInMilliseconds);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    }

    @Override
    public  T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) {
        IApplication app = applicationProvider.get();
        app.setImplicitWaitTimeout(Duration.ZERO);
        Duration currentTimeout = resolveConditionTimeout(timeout);
        Duration actualPollingInterval = resolvePollingInterval(pollingInterval);
        String exMessage = resolveMessage(message);
        WebDriverWait wait = new WebDriverWait(app.getDriver(), currentTimeout);
        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 Duration resolveConditionTimeout(Duration timeout) {
        return Optional.ofNullable(timeout).orElse(timeoutConfiguration.getCondition());
    }

    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