|
| 1 | +package aquality.selenium.core.waitings; |
| 2 | + |
| 3 | +import aquality.selenium.core.applications.IApplication; |
| 4 | +import aquality.selenium.core.configurations.ITimeoutConfiguration; |
| 5 | +import com.google.common.base.Strings; |
| 6 | +import com.google.inject.Inject; |
| 7 | +import com.google.inject.Provider; |
| 8 | +import org.openqa.selenium.StaleElementReferenceException; |
| 9 | +import org.openqa.selenium.support.ui.ExpectedCondition; |
| 10 | +import org.openqa.selenium.support.ui.WebDriverWait; |
| 11 | + |
| 12 | +import java.time.Duration; |
| 13 | +import java.util.Collection; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Optional; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.concurrent.TimeoutException; |
| 18 | +import java.util.function.BooleanSupplier; |
| 19 | + |
| 20 | +public class ConditionalWait implements IConditionalWait { |
| 21 | + |
| 22 | + private final Provider<IApplication> applicationProvider; |
| 23 | + private final ITimeoutConfiguration timeoutConfiguration; |
| 24 | + |
| 25 | + @Inject |
| 26 | + public ConditionalWait(Provider<IApplication> applicationProvider, ITimeoutConfiguration timeoutConfiguration) { |
| 27 | + this.applicationProvider = applicationProvider; |
| 28 | + this.timeoutConfiguration = timeoutConfiguration; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public boolean waitFor(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) { |
| 33 | + try { |
| 34 | + waitForTrue(condition, timeoutInSeconds, pollingIntervalInMilliseconds, message, exceptionsToIgnore); |
| 35 | + return true; |
| 36 | + } catch (TimeoutException e) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) throws TimeoutException { |
| 43 | + BooleanSupplier supplier = Optional.ofNullable(condition).orElseThrow(() -> new IllegalArgumentException("Condition cannot be null")); |
| 44 | + Long timeout = resolveConditionTimeout(timeoutInSeconds); |
| 45 | + Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); |
| 46 | + String exMessage = resolveMessage(message); |
| 47 | + double startTime = getCurrentTime(); |
| 48 | + while (true) { |
| 49 | + if (isConditionSatisfied(supplier, exceptionsToIgnore)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + double currentTime = getCurrentTime(); |
| 54 | + if ((currentTime - startTime) > timeout) { |
| 55 | + String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeout, exMessage); |
| 56 | + throw new TimeoutException(exceptionMessage); |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + Thread.sleep(pollingInterval); |
| 61 | + } catch (InterruptedException e) { |
| 62 | + Thread.currentThread().interrupt(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public <T> T waitFor(ExpectedCondition<T> condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) { |
| 69 | + IApplication app = applicationProvider.get(); |
| 70 | + app.setImplicitWaitTimeout(0, TimeUnit.SECONDS); |
| 71 | + Long timeout = resolveConditionTimeout(timeoutInSeconds); |
| 72 | + Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); |
| 73 | + String exMessage = resolveMessage(message); |
| 74 | + WebDriverWait wait = new WebDriverWait(app.getDriver(), timeout); |
| 75 | + wait.pollingEvery(Duration.ofMillis(pollingInterval)); |
| 76 | + wait.withMessage(exMessage); |
| 77 | + wait.ignoreAll(exceptionsToIgnore == null ? Collections.singleton(StaleElementReferenceException.class) : exceptionsToIgnore); |
| 78 | + try { |
| 79 | + return wait.until(condition); |
| 80 | + } finally { |
| 81 | + app.setImplicitWaitTimeout(timeoutConfiguration.getImplicit(), TimeUnit.SECONDS); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private double getCurrentTime() { |
| 86 | + return System.nanoTime() / Math.pow(10, 9); |
| 87 | + } |
| 88 | + |
| 89 | + private boolean isConditionSatisfied(BooleanSupplier condition, Collection<Class<? extends Throwable>> exceptionsToIgnore) { |
| 90 | + try { |
| 91 | + return condition.getAsBoolean(); |
| 92 | + } catch (Exception e) { |
| 93 | + if (exceptionsToIgnore == null || !exceptionsToIgnore.contains(e.getClass())) { |
| 94 | + throw e; |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private Long resolveConditionTimeout(Long timeout) { |
| 102 | + return Optional.ofNullable(timeout).orElse(timeoutConfiguration.getCommand()); |
| 103 | + } |
| 104 | + |
| 105 | + private Long resolvePollingInterval(Long timeout) { |
| 106 | + return Optional.ofNullable(timeout).orElse(timeoutConfiguration.getPollingInterval()); |
| 107 | + } |
| 108 | + |
| 109 | + private String resolveMessage(String message) { |
| 110 | + return Strings.isNullOrEmpty(message) ? "" : message; |
| 111 | + } |
| 112 | +} |
0 commit comments