From 5c07ff9ce1266d8785c834f9efefbf876bb55632 Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Wed, 12 Feb 2020 17:05:20 +0300 Subject: [PATCH 1/9] Migrate RetryConfiguration to Duration timeouts --- .../selenium/core/configurations/IRetryConfiguration.java | 4 +++- .../selenium/core/configurations/RetryConfiguration.java | 8 +++++--- .../selenium/core/utilities/ElementActionRetrier.java | 2 +- .../java/tests/configurations/ConfigurationTests.java | 2 +- .../java/tests/utilities/ElementActionRetrierTests.java | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/aquality/selenium/core/configurations/IRetryConfiguration.java b/src/main/java/aquality/selenium/core/configurations/IRetryConfiguration.java index e6d9ca3..f0f76f6 100644 --- a/src/main/java/aquality/selenium/core/configurations/IRetryConfiguration.java +++ b/src/main/java/aquality/selenium/core/configurations/IRetryConfiguration.java @@ -1,5 +1,7 @@ package aquality.selenium.core.configurations; +import java.time.Duration; + /** * Describes retry configuration. */ @@ -17,5 +19,5 @@ public interface IRetryConfiguration { * * @return Polling interval for retry. */ - long getPollingInterval(); + Duration getPollingInterval(); } diff --git a/src/main/java/aquality/selenium/core/configurations/RetryConfiguration.java b/src/main/java/aquality/selenium/core/configurations/RetryConfiguration.java index 4121510..690ee29 100644 --- a/src/main/java/aquality/selenium/core/configurations/RetryConfiguration.java +++ b/src/main/java/aquality/selenium/core/configurations/RetryConfiguration.java @@ -3,14 +3,16 @@ import aquality.selenium.core.utilities.ISettingsFile; import com.google.inject.Inject; +import java.time.Duration; + public class RetryConfiguration implements IRetryConfiguration { private final int number; - private final long pollingInterval; + private final Duration pollingInterval; @Inject public RetryConfiguration(ISettingsFile settingsFile) { this.number = Integer.parseInt(settingsFile.getValue("/retry/number").toString()); - this.pollingInterval = Long.parseLong(settingsFile.getValue("/retry/pollingInterval").toString()); + this.pollingInterval = Duration.ofMillis(Long.parseLong(settingsFile.getValue("/retry/pollingInterval").toString())); } @Override @@ -19,7 +21,7 @@ public int getNumber() { } @Override - public long getPollingInterval() { + public Duration getPollingInterval() { return pollingInterval; } } diff --git a/src/main/java/aquality/selenium/core/utilities/ElementActionRetrier.java b/src/main/java/aquality/selenium/core/utilities/ElementActionRetrier.java index f8ac474..43ff8bb 100644 --- a/src/main/java/aquality/selenium/core/utilities/ElementActionRetrier.java +++ b/src/main/java/aquality/selenium/core/utilities/ElementActionRetrier.java @@ -35,7 +35,7 @@ public T doWithRetry(Supplier function) { } catch (Exception exception) { if (isExceptionHandled(exception) && retryAttemptsLeft != 0) { try { - Thread.sleep(retryConfiguration.getPollingInterval()); + Thread.sleep(retryConfiguration.getPollingInterval().toMillis()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } diff --git a/src/test/java/tests/configurations/ConfigurationTests.java b/src/test/java/tests/configurations/ConfigurationTests.java index a259ced..16ad4fd 100644 --- a/src/test/java/tests/configurations/ConfigurationTests.java +++ b/src/test/java/tests/configurations/ConfigurationTests.java @@ -27,7 +27,7 @@ public void testShouldBePossibleToGetLanguage() { public void testShouldBePossibleToGetRetryConfiguration() { RetryConfiguration retryConfiguration = new RetryConfiguration(settingsFile); assertEquals(retryConfiguration.getNumber(), 2, "Number of retry attempts timeout should be got"); - assertEquals(retryConfiguration.getPollingInterval(), 300, "Polling interval of retrier should be got"); + assertEquals(retryConfiguration.getPollingInterval().toMillis(), 300, "Polling interval of retrier should be got"); } @Test diff --git a/src/test/java/tests/utilities/ElementActionRetrierTests.java b/src/test/java/tests/utilities/ElementActionRetrierTests.java index 8f29c3e..195e9ef 100644 --- a/src/test/java/tests/utilities/ElementActionRetrierTests.java +++ b/src/test/java/tests/utilities/ElementActionRetrierTests.java @@ -22,7 +22,7 @@ public class ElementActionRetrierTests { private static final IRetryConfiguration RETRY_CONFIGURATION = AqualityServices.getServiceProvider().getInstance(IRetryConfiguration.class); private static final Logger LOGGER = AqualityServices.getServiceProvider().getInstance(Logger.class); private static final int RETRIES_COUNT = RETRY_CONFIGURATION.getNumber(); - private static final long POLLING_INTERVAL = RETRY_CONFIGURATION.getPollingInterval(); + private static final long POLLING_INTERVAL = RETRY_CONFIGURATION.getPollingInterval().toMillis(); private static final ElementActionRetrier ELEMENT_ACTION_RETRIER = new ElementActionRetrier(RETRY_CONFIGURATION); @DataProvider From 3a9739bc35f5bf5b15b6a5dc5adb159774f82dd4 Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Wed, 12 Feb 2020 17:36:22 +0300 Subject: [PATCH 2/9] Migrate TimeoutConfiguration to Duration timeouts --- .../core/applications/IApplication.java | 5 +-- .../configurations/ITimeoutConfiguration.java | 10 +++-- .../configurations/TimeoutConfiguration.java | 39 +++++++++++-------- .../core/waitings/ConditionalWait.java | 9 ++--- .../tests/application/IApplicationTests.java | 4 +- .../browser/ChromeApplication.java | 16 ++++---- .../windowsApp/WindowsApplication.java | 11 +++--- .../configurations/ConfigurationTests.java | 8 ++-- .../configurations/EnvConfigurationTests.java | 2 +- .../IWebElementStateProviderTests.java | 2 +- .../tests/waitings/WaitForObjectTests.java | 12 +++--- .../java/tests/waitings/WaitForTests.java | 12 +++--- .../java/tests/waitings/WaitForTrueTests.java | 12 +++--- 13 files changed, 75 insertions(+), 67 deletions(-) diff --git a/src/main/java/aquality/selenium/core/applications/IApplication.java b/src/main/java/aquality/selenium/core/applications/IApplication.java index 6b61943..f3fc5a4 100644 --- a/src/main/java/aquality/selenium/core/applications/IApplication.java +++ b/src/main/java/aquality/selenium/core/applications/IApplication.java @@ -2,7 +2,7 @@ import org.openqa.selenium.remote.RemoteWebDriver; -import java.util.concurrent.TimeUnit; +import java.time.Duration; /** * Interface of any application controlled by Selenium WebDriver API @@ -22,7 +22,6 @@ public interface IApplication { /** * Sets implicit wait timeout to Selenium WebDriver. * @param value timeout value to set. - * @param timeUnit timeUnit of timeout value. */ - void setImplicitWaitTimeout(long value, TimeUnit timeUnit); + void setImplicitWaitTimeout(Duration value); } diff --git a/src/main/java/aquality/selenium/core/configurations/ITimeoutConfiguration.java b/src/main/java/aquality/selenium/core/configurations/ITimeoutConfiguration.java index bfc69c5..892f59e 100644 --- a/src/main/java/aquality/selenium/core/configurations/ITimeoutConfiguration.java +++ b/src/main/java/aquality/selenium/core/configurations/ITimeoutConfiguration.java @@ -1,5 +1,7 @@ package aquality.selenium.core.configurations; +import java.time.Duration; + /** * Provides timeouts configuration. */ @@ -10,26 +12,26 @@ public interface ITimeoutConfiguration { * * @return ImplicitWait timeout. */ - long getImplicit(); + Duration getImplicit(); /** * Gets default ConditionalWait timeout. * * @return ConditionalWait timeout. */ - long getCondition(); + Duration getCondition(); /** * Gets ConditionalWait polling interval. * * @return polling interval. */ - long getPollingInterval(); + Duration getPollingInterval(); /** * Gets Command timeout. * * @return Command timeout. */ - long getCommand(); + Duration getCommand(); } diff --git a/src/main/java/aquality/selenium/core/configurations/TimeoutConfiguration.java b/src/main/java/aquality/selenium/core/configurations/TimeoutConfiguration.java index b5d8cd9..a6ef488 100644 --- a/src/main/java/aquality/selenium/core/configurations/TimeoutConfiguration.java +++ b/src/main/java/aquality/selenium/core/configurations/TimeoutConfiguration.java @@ -3,40 +3,46 @@ import aquality.selenium.core.utilities.ISettingsFile; import com.google.inject.Inject; -public class TimeoutConfiguration implements ITimeoutConfiguration{ +import java.time.Duration; + +public class TimeoutConfiguration implements ITimeoutConfiguration { private final ISettingsFile settingsFile; - private final long condition; - private final long pollInterval; - private final long implicit; - private final long command; + private final Duration condition; + private final Duration pollInterval; + private final Duration implicit; + private final Duration command; @Inject public TimeoutConfiguration(ISettingsFile settingsFile) { this.settingsFile = settingsFile; - condition = getTimeout(TIMEOUT.CONDITION); - pollInterval = getTimeout(TIMEOUT.POLL_INTERVAL); - implicit = getTimeout(TIMEOUT.IMPLICIT); - command = getTimeout(TIMEOUT.COMMAND); + condition = getDurationFromSeconds(TIMEOUT.CONDITION); + pollInterval = Duration.ofMillis(getTimeout(TIMEOUT.POLL_INTERVAL)); + implicit = getDurationFromSeconds(TIMEOUT.IMPLICIT); + command = getDurationFromSeconds(TIMEOUT.COMMAND); } - private long getTimeout(TIMEOUT timeout){ + private long getTimeout(TIMEOUT timeout) { return Long.valueOf(settingsFile.getValue(timeout.getKey()).toString()); } - public long getImplicit(){ + private Duration getDurationFromSeconds(TIMEOUT timeout) { + return Duration.ofSeconds(getTimeout(timeout)); + } + + public Duration getImplicit() { return implicit; } - public long getCondition(){ + public Duration getCondition() { return condition; } - public long getPollingInterval(){ + public Duration getPollingInterval() { return pollInterval; } - public long getCommand(){ + public Duration getCommand() { return command; } @@ -47,11 +53,12 @@ private enum TIMEOUT { COMMAND("/timeouts/timeoutCommand"); private String key; - TIMEOUT(String key){ + + TIMEOUT(String key) { this.key = key; } - private String getKey(){ + private String getKey() { return key; } } diff --git a/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java b/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java index 79cc46b..694c979 100644 --- a/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java +++ b/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java @@ -13,7 +13,6 @@ import java.util.Collection; import java.util.Collections; import java.util.Optional; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.BooleanSupplier; @@ -67,7 +66,7 @@ public void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long p @Override public T waitFor(ExpectedCondition condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore) { IApplication app = applicationProvider.get(); - app.setImplicitWaitTimeout(0, TimeUnit.SECONDS); + app.setImplicitWaitTimeout(Duration.ZERO); Long timeout = resolveConditionTimeout(timeoutInSeconds); Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); String exMessage = resolveMessage(message); @@ -78,7 +77,7 @@ public T waitFor(ExpectedCondition condition, Long timeoutInSeconds, Long try { return wait.until(condition); } finally { - app.setImplicitWaitTimeout(timeoutConfiguration.getImplicit(), TimeUnit.SECONDS); + app.setImplicitWaitTimeout(timeoutConfiguration.getImplicit()); } } @@ -99,11 +98,11 @@ private boolean isConditionSatisfied(BooleanSupplier condition, Collection(serviceUrl, capabilities); - setImplicitWaitTimeout(implicitWaitSeconds, TimeUnit.SECONDS); + setImplicitWaitTimeout(Duration.ofSeconds(implicitWaitSeconds)); } @Override @@ -31,10 +32,10 @@ public boolean isStarted() { } @Override - public void setImplicitWaitTimeout(long value, TimeUnit timeUnit) { - long valueInSeconds = TimeUnit.SECONDS.convert(value, timeUnit); + public void setImplicitWaitTimeout(Duration value) { + long valueInSeconds = value.getSeconds(); if (implicitWaitSeconds != valueInSeconds){ - driver.manage().timeouts().implicitlyWait(value, timeUnit); + driver.manage().timeouts().implicitlyWait(value.toMillis(), TimeUnit.MILLISECONDS); implicitWaitSeconds = valueInSeconds; } } diff --git a/src/test/java/tests/configurations/ConfigurationTests.java b/src/test/java/tests/configurations/ConfigurationTests.java index 16ad4fd..584c06e 100644 --- a/src/test/java/tests/configurations/ConfigurationTests.java +++ b/src/test/java/tests/configurations/ConfigurationTests.java @@ -33,9 +33,9 @@ public void testShouldBePossibleToGetRetryConfiguration() { @Test public void testShouldBePossibleToGetTimeoutConfiguration() { TimeoutConfiguration timeoutConfiguration = new TimeoutConfiguration(settingsFile); - assertEquals(timeoutConfiguration.getCommand(), 60, "Command timeout should be got"); - assertEquals(timeoutConfiguration.getCondition(), 30, "Condition timeout should be got"); - assertEquals(timeoutConfiguration.getImplicit(), 0, "Implicit timeout should be got"); - assertEquals(timeoutConfiguration.getPollingInterval(), 300, "Polling interval should be got"); + assertEquals(timeoutConfiguration.getCommand().getSeconds(), 60, "Command timeout should be got"); + assertEquals(timeoutConfiguration.getCondition().getSeconds(), 30, "Condition timeout should be got"); + assertEquals(timeoutConfiguration.getImplicit().getSeconds(), 0, "Implicit timeout should be got"); + assertEquals(timeoutConfiguration.getPollingInterval().toMillis(), 300, "Polling interval should be got"); } } diff --git a/src/test/java/tests/configurations/EnvConfigurationTests.java b/src/test/java/tests/configurations/EnvConfigurationTests.java index cab602d..07af7d3 100644 --- a/src/test/java/tests/configurations/EnvConfigurationTests.java +++ b/src/test/java/tests/configurations/EnvConfigurationTests.java @@ -43,7 +43,7 @@ public void testShouldBePossibleToOverrideLanguageWithEnvVariable() { @Test public void testShouldBePossibleToOverrideTimeoutWithEnvVariable() { - long conditionTimeout = new TimeoutConfiguration(settingsFile).getCondition(); + long conditionTimeout = new TimeoutConfiguration(settingsFile).getCondition().getSeconds(); assertEquals(conditionTimeout, Long.parseLong(NEW_INT_VALUE), "Condition timeout should be overridden with env variable"); } diff --git a/src/test/java/tests/elements/IWebElementStateProviderTests.java b/src/test/java/tests/elements/IWebElementStateProviderTests.java index cf2b078..b62dac8 100644 --- a/src/test/java/tests/elements/IWebElementStateProviderTests.java +++ b/src/test/java/tests/elements/IWebElementStateProviderTests.java @@ -33,7 +33,7 @@ default DynamicLoadingForm getDynamicLoadingForm() { IElementStateProvider state(By locator); default long getConditionTimeout() { - return AqualityServices.get(ITimeoutConfiguration.class).getCondition(); + return AqualityServices.get(ITimeoutConfiguration.class).getCondition().getSeconds(); } default void checkWaitingTimeForInputState(Long waitTime, Predicate notExpectedCondition) { diff --git a/src/test/java/tests/waitings/WaitForObjectTests.java b/src/test/java/tests/waitings/WaitForObjectTests.java index c5d38c0..ae53a7c 100644 --- a/src/test/java/tests/waitings/WaitForObjectTests.java +++ b/src/test/java/tests/waitings/WaitForObjectTests.java @@ -91,14 +91,14 @@ public void testShouldThrowException(Callable throwAction, long timeout) public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithExceptions = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition()); + checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()); } @Test public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), "Condition should be true", ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition()); + checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()); } @Test @@ -136,10 +136,10 @@ private Object[][] getDataProvider(ExpectedCondition action) { Callable actionWithCustomTimeoutsAndExceptions = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, Collections.emptyList()); Callable actionWithAllParameters = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", Collections.emptyList()); return new Object[][]{ - {onlyAction, timeoutConfiguration.getCondition()}, - {actionWithMessage, timeoutConfiguration.getCondition()}, - {actionWithExceptions, timeoutConfiguration.getCondition()}, - {actionWithMessageAndExceptions, timeoutConfiguration.getCondition()}, + {onlyAction, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithMessage, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()}, {actionWithCustomTimeouts, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition}, diff --git a/src/test/java/tests/waitings/WaitForTests.java b/src/test/java/tests/waitings/WaitForTests.java index b8ded4d..9dc647c 100644 --- a/src/test/java/tests/waitings/WaitForTests.java +++ b/src/test/java/tests/waitings/WaitForTests.java @@ -44,14 +44,14 @@ public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver(BooleanS public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithExceptions = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition()); + checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()); } @Test public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), "Condition should be true", ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition()); + checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()); } @Test @@ -107,10 +107,10 @@ private Object[][] getDataProvider(BooleanSupplier action) { BooleanSupplier actionWithCustomTimeoutsAndExceptions = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, Collections.emptyList()); BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", Collections.emptyList()); return new Object[][]{ - {onlyAction, timeoutConfiguration.getCondition()}, - {actionWithMessage, timeoutConfiguration.getCondition()}, - {actionWithExceptions, timeoutConfiguration.getCondition()}, - {actionWithMessageAndExceptions, timeoutConfiguration.getCondition()}, + {onlyAction, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithMessage, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()}, {actionWithCustomTimeouts, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition}, diff --git a/src/test/java/tests/waitings/WaitForTrueTests.java b/src/test/java/tests/waitings/WaitForTrueTests.java index f3bf3f0..6bc138c 100644 --- a/src/test/java/tests/waitings/WaitForTrueTests.java +++ b/src/test/java/tests/waitings/WaitForTrueTests.java @@ -77,7 +77,7 @@ public void testCustomExceptionShouldBeIgnoredWithoutCustomParameters() throws E checkExceptionIsIgnored(() -> { conditionalWait.waitForTrue(throwNewException(atomicBoolean), ignoredExceptions); return true; - }, timeoutConfiguration.getCondition()); + }, timeoutConfiguration.getCondition().getSeconds()); } @Test @@ -86,7 +86,7 @@ public void testCustomExceptionShouldBeIgnoredWithDefaultTimeout() throws Except checkExceptionIsIgnored(() -> { conditionalWait.waitForTrue(throwNewException(atomicBoolean), "Condition should be true", ignoredExceptions); return true; - }, timeoutConfiguration.getCondition()); + }, timeoutConfiguration.getCondition().getSeconds()); } @Test @@ -159,10 +159,10 @@ private Object[][] getDataProvider(BooleanSupplier action) { }; return new Object[][]{ - {onlyAction, timeoutConfiguration.getCondition()}, - {actionWithMessage, timeoutConfiguration.getCondition()}, - {actionWithExceptions, timeoutConfiguration.getCondition()}, - {actionWithMessageAndExceptions, timeoutConfiguration.getCondition()}, + {onlyAction, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithMessage, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()}, + {actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()}, {actionWithCustomTimeouts, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndException, waitForTimeoutCondition}, From d967b3823ba7c0993cd274e26571e21cebed49a0 Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Thu, 13 Feb 2020 11:56:40 +0300 Subject: [PATCH 3/9] Migrate ConditionalWait to Duration timeouts --- .../elements/CachedElementStateProvider.java | 5 +- .../elements/DefaultElementStateProvider.java | 6 +- .../selenium/core/elements/ElementFinder.java | 4 +- .../core/elements/RelativeElementFinder.java | 4 +- .../core/elements/interfaces/IElement.java | 4 +- .../core/waitings/ConditionalWait.java | 32 +-- .../core/waitings/IConditionalWait.java | 182 +++++++++++------- .../tests/elements/factory/CustomElement.java | 4 +- .../waitings/BaseConditionalWaitTest.java | 8 +- .../tests/waitings/WaitForObjectTests.java | 31 +-- .../java/tests/waitings/WaitForTests.java | 29 +-- .../java/tests/waitings/WaitForTrueTests.java | 31 +-- 12 files changed, 193 insertions(+), 147 deletions(-) diff --git a/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java b/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java index db0c56c..79f926e 100644 --- a/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java +++ b/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java @@ -8,6 +8,7 @@ import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.WebElement; +import java.time.Duration; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -52,7 +53,7 @@ protected boolean tryInvokeFunction(Predicate predicate, List !isDisplayed(), timeout, null); + return conditionalWait.waitFor(() -> !isDisplayed(), timeout == null ? null : Duration.ofSeconds(timeout)); } @Override @@ -67,7 +69,7 @@ public boolean waitForExist(Long timeout) { @Override public boolean waitForNotExist(Long timeout) { - return conditionalWait.waitFor(() -> !isExist(), timeout, null); + return conditionalWait.waitFor(() -> !isExist(), timeout == null ? null : Duration.ofSeconds(timeout)); } @Override diff --git a/src/main/java/aquality/selenium/core/elements/ElementFinder.java b/src/main/java/aquality/selenium/core/elements/ElementFinder.java index 89455d0..6a15ab1 100644 --- a/src/main/java/aquality/selenium/core/elements/ElementFinder.java +++ b/src/main/java/aquality/selenium/core/elements/ElementFinder.java @@ -6,6 +6,7 @@ import com.google.inject.Inject; import org.openqa.selenium.*; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; @@ -30,8 +31,7 @@ public List findElements(By locator, DesiredState desiredState, Long try { conditionalWait.waitFor(driver -> tryToFindElements(locator, desiredState, wasAnyElementFound, resultElements, driver), - timeoutInSeconds, - null); + timeoutInSeconds == null ? null : Duration.ofSeconds(timeoutInSeconds)); } catch (TimeoutException e) { handleTimeoutException(e, locator, desiredState, wasAnyElementFound.get()); } diff --git a/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java b/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java index 847114a..d885483 100644 --- a/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java +++ b/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java @@ -6,6 +6,7 @@ import org.openqa.selenium.SearchContext; import org.openqa.selenium.WebElement; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeoutException; @@ -34,8 +35,7 @@ public List findElements(By locator, DesiredState desiredState, Long conditionalWait.waitForTrue(() -> tryToFindElements(locator, desiredState, wasAnyElementFound, resultElements, searchContextSupplier.get()), - timeoutInSeconds, - null); + timeoutInSeconds == null ? null : Duration.ofSeconds(timeoutInSeconds)); } catch (TimeoutException e) { handleTimeoutException(new org.openqa.selenium.TimeoutException(e.getMessage(), e), locator, desiredState, wasAnyElementFound.get()); diff --git a/src/main/java/aquality/selenium/core/elements/interfaces/IElement.java b/src/main/java/aquality/selenium/core/elements/interfaces/IElement.java index f3b5b63..f8700b4 100644 --- a/src/main/java/aquality/selenium/core/elements/interfaces/IElement.java +++ b/src/main/java/aquality/selenium/core/elements/interfaces/IElement.java @@ -3,6 +3,8 @@ import org.openqa.selenium.By; import org.openqa.selenium.remote.RemoteWebElement; +import java.time.Duration; + public interface IElement extends IParent { /** @@ -20,7 +22,7 @@ default RemoteWebElement getElement() { * @param timeout Timeout for waiting * @return WebElement */ - RemoteWebElement getElement(Long timeout); + RemoteWebElement getElement(Duration timeout); /** * Gets element name. diff --git a/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java b/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java index 694c979..dbd06ee 100644 --- a/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java +++ b/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java @@ -28,9 +28,9 @@ public ConditionalWait(Provider applicationProvider, ITimeoutConfi } @Override - public boolean waitFor(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore) { + public boolean waitFor(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) { try { - waitForTrue(condition, timeoutInSeconds, pollingIntervalInMilliseconds, message, exceptionsToIgnore); + waitForTrue(condition, timeout, pollingInterval, message, exceptionsToIgnore); return true; } catch (TimeoutException e) { return false; @@ -38,10 +38,10 @@ public boolean waitFor(BooleanSupplier condition, Long timeoutInSeconds, Long po } @Override - public void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore) throws TimeoutException { + 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 timeout = resolveConditionTimeout(timeoutInSeconds); - Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); + long timeoutInSeconds = resolveConditionTimeoutInSeconds(timeout); + long pollingIntervalInMilliseconds = resolvePollingInterval(pollingInterval).toMillis(); String exMessage = resolveMessage(message); double startTime = getCurrentTime(); while (true) { @@ -50,13 +50,13 @@ public void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long p } double currentTime = getCurrentTime(); - if ((currentTime - startTime) > timeout) { + if ((currentTime - startTime) > timeoutInSeconds) { String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeout, exMessage); throw new TimeoutException(exceptionMessage); } try { - Thread.sleep(pollingInterval); + Thread.sleep(pollingIntervalInMilliseconds); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } @@ -64,14 +64,14 @@ public void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long p } @Override - public T waitFor(ExpectedCondition condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore) { + public T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) { IApplication app = applicationProvider.get(); app.setImplicitWaitTimeout(Duration.ZERO); - Long timeout = resolveConditionTimeout(timeoutInSeconds); - Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); + long timeoutInSeconds = resolveConditionTimeoutInSeconds(timeout); + Duration actualPollingInterval = resolvePollingInterval(pollingInterval); String exMessage = resolveMessage(message); - WebDriverWait wait = new WebDriverWait(app.getDriver(), timeout); - wait.pollingEvery(Duration.ofMillis(pollingInterval)); + WebDriverWait wait = new WebDriverWait(app.getDriver(), timeoutInSeconds); + wait.pollingEvery(actualPollingInterval); wait.withMessage(exMessage); wait.ignoreAll(exceptionsToIgnore == null ? Collections.singleton(StaleElementReferenceException.class) : exceptionsToIgnore); try { @@ -97,12 +97,12 @@ private boolean isConditionSatisfied(BooleanSupplier condition, Collection> exceptionsToIgnore) { - return waitFor(condition, timeoutInSeconds, pollingIntervalInMilliseconds, null, exceptionsToIgnore); + default boolean waitFor(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message) { + return waitFor(condition, timeout, pollingInterval, message, null); } /** * Wait for some condition within timeout. Method does not use WebDriverWait * - * @param condition Condition with boolean result (predicate) - * @param timeoutInSeconds Condition timeout - * @param pollingIntervalInMilliseconds Condition check interval - * @param message Part of error message in case of Timeout exception - * @param exceptionsToIgnore Exceptions to ignore + * @param condition Condition with boolean result (predicate) + * @param timeout Condition timeout + * @param pollingInterval Condition check interval + * @param exceptionsToIgnore Exceptions to ignore + * @return true if the condition has been met during the timeout + */ + default boolean waitFor(BooleanSupplier condition, Duration timeout, Duration pollingInterval, Collection> exceptionsToIgnore) { + return waitFor(condition, timeout, pollingInterval, null, exceptionsToIgnore); + } + + /** + * Wait for some condition within timeout. Method does not use WebDriverWait + * + * @param condition Condition with boolean result (predicate) + * @param timeout Condition timeout + * @param pollingInterval Condition check interval + * @param message Part of error message in case of Timeout exception + * @param exceptionsToIgnore Exceptions to ignore * @return true if the condition has been met during the timeout */ - boolean waitFor(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore); + boolean waitFor(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore); /** * Wait for some condition within timeout. Method does not use WebDriverWait @@ -161,53 +173,64 @@ default void waitForTrue(BooleanSupplier condition, String message, Collection> exceptionsToIgnore) throws TimeoutException { - waitForTrue(condition, timeoutInSeconds, pollingIntervalInMilliseconds, null, exceptionsToIgnore); + default void waitForTrue(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message) throws TimeoutException { + waitForTrue(condition, timeout, pollingInterval, message, null); + } + + /** + * Wait for some condition within timeout. Method does not use WebDriverWait + * + * @param condition Condition with boolean result (predicate) + * @param timeout Condition timeout + * @param pollingInterval Condition check interval + * @param exceptionsToIgnore Exceptions to ignore + * @throws TimeoutException will be thrown in case if timeout is over but condition was not met + */ + default void waitForTrue(BooleanSupplier condition, Duration timeout, Duration pollingInterval, Collection> exceptionsToIgnore) throws TimeoutException { + waitForTrue(condition, timeout, pollingInterval, null, exceptionsToIgnore); } /** * Wait for some condition within timeout. Method does not use WebDriverWait * - * @param condition Condition with boolean result (predicate) - * @param timeoutInSeconds Condition timeout - * @param pollingIntervalInMilliseconds Condition check interval - * @param message Part of error message in case of Timeout exception - * @param exceptionsToIgnore Exceptions to ignore + * @param condition Condition with boolean result (predicate) + * @param timeout Condition timeout + * @param pollingInterval Condition check interval + * @param message Part of error message in case of Timeout exception + * @param exceptionsToIgnore Exceptions to ignore * @throws TimeoutException will be thrown in case if timeout is over but condition was not met */ - void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore) throws TimeoutException; + void waitForTrue(BooleanSupplier condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore) throws TimeoutException; /** * Waits for function will be true or return some except false. @@ -267,55 +290,68 @@ default T waitFor(ExpectedCondition condition, String message, Collection * Waits for function will be true or return some except false. * StaleElementReferenceException will be handled by default * - * @param condition Function for waiting {@link Function} - * @param timeOutInSeconds Time-out in seconds - * @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match - * @param Type of object which is waiting + * @param condition Function for waiting {@link Function} + * @param timeout Duration before the time-out + * @param pollingInterval interval between checks whether condition match + * @param Type of object which is waiting * @return Object which waiting for or null - is exceptions occured */ - default T waitFor(ExpectedCondition condition, Long timeOutInSeconds, Long pollingIntervalInMilliseconds) { - return waitFor(condition, timeOutInSeconds, pollingIntervalInMilliseconds, null, null); + default T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval) { + return waitFor(condition, timeout, pollingInterval, null, null); } /** * Waits for function will be true or return some except false. * StaleElementReferenceException will be handled by default * - * @param condition Function for waiting {@link Function} - * @param timeoutInSeconds Time-out in seconds - * @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match - * @param message the message that will be added to an error in case if the condition is not matched during the timeout - * @param Type of object which is waiting + * @param condition Function for waiting {@link Function} + * @param timeout Duration before the time-out + * @param Type of object which is waiting * @return Object which waiting for or null - is exceptions occured */ - default T waitFor(ExpectedCondition condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message) { - return waitFor(condition, timeoutInSeconds, pollingIntervalInMilliseconds, message, null); + default T waitFor(ExpectedCondition condition, Duration timeout) { + return waitFor(condition, timeout, null); } /** * Waits for function will be true or return some except false. + * StaleElementReferenceException will be handled by default * - * @param condition Function for waiting {@link Function} - * @param timeoutInSeconds Time-out in seconds - * @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match - * @param exceptionsToIgnore list of exceptions that should be ignored during waiting - * @param Type of object which is waiting + * @param condition Function for waiting {@link Function} + * @param timeout Duration before the time-out + * @param pollingInterval interval between checks whether condition match + * @param message the message that will be added to an error in case if the condition is not matched during the timeout + * @param Type of object which is waiting * @return Object which waiting for or null - is exceptions occured */ - default T waitFor(ExpectedCondition condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, Collection> exceptionsToIgnore) { - return waitFor(condition, timeoutInSeconds, pollingIntervalInMilliseconds, null, exceptionsToIgnore); + default T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval, String message) { + return waitFor(condition, timeout, pollingInterval, message, null); } /** * Waits for function will be true or return some except false. * - * @param condition Function for waiting {@link Function} - * @param timeoutInSeconds Time-out in seconds - * @param pollingIntervalInMilliseconds interval in milliseconds between checks whether condition match - * @param message the message that will be added to an error in case if the condition is not matched during the timeout - * @param exceptionsToIgnore list of exceptions that should be ignored during waiting - * @param Type of object which is waiting + * @param condition Function for waiting {@link Function} + * @param timeout Duration before the time-out + * @param pollingInterval interval between checks whether condition match + * @param exceptionsToIgnore list of exceptions that should be ignored during waiting + * @param Type of object which is waiting + * @return Object which waiting for or null - is exceptions occured + */ + default T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval, Collection> exceptionsToIgnore) { + return waitFor(condition, timeout, pollingInterval, null, exceptionsToIgnore); + } + + /** + * Waits for function will be true or return some except false. + * + * @param condition Function for waiting {@link Function} + * @param timeout Duration before the time-out + * @param pollingInterval interval between checks whether condition match + * @param message the message that will be added to an error in case if the condition is not matched during the timeout + * @param exceptionsToIgnore list of exceptions that should be ignored during waiting + * @param Type of object which is waiting * @return Object which waiting for or null - is exceptions occured */ - T waitFor(ExpectedCondition condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection> exceptionsToIgnore); + T waitFor(ExpectedCondition condition, Duration timeout, Duration pollingInterval, String message, Collection> exceptionsToIgnore); } diff --git a/src/test/java/tests/elements/factory/CustomElement.java b/src/test/java/tests/elements/factory/CustomElement.java index 358feac..5cd04a4 100644 --- a/src/test/java/tests/elements/factory/CustomElement.java +++ b/src/test/java/tests/elements/factory/CustomElement.java @@ -7,6 +7,8 @@ import org.openqa.selenium.By; import org.openqa.selenium.remote.RemoteWebElement; +import java.time.Duration; + public class CustomElement implements ICustomElement { private final By locator; @@ -20,7 +22,7 @@ protected CustomElement(By locator, String name, ElementState state) { } @Override - public RemoteWebElement getElement(Long timeout) { + public RemoteWebElement getElement(Duration timeout) { return null; } diff --git a/src/test/java/tests/waitings/BaseConditionalWaitTest.java b/src/test/java/tests/waitings/BaseConditionalWaitTest.java index 247d592..2dd81e9 100644 --- a/src/test/java/tests/waitings/BaseConditionalWaitTest.java +++ b/src/test/java/tests/waitings/BaseConditionalWaitTest.java @@ -3,20 +3,20 @@ import aquality.selenium.core.applications.IApplication; import aquality.selenium.core.configurations.ITimeoutConfiguration; import aquality.selenium.core.waitings.ConditionalWait; -import com.google.inject.Injector; import com.google.inject.Provider; import org.testng.annotations.AfterMethod; import tests.application.browser.AqualityServices; import utils.Timer; +import java.time.Duration; import java.util.Collection; import java.util.Collections; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BooleanSupplier; -class BaseConditionalWaitTest { - static final long waitForTimeoutCondition = 10; - static final long waitForTimeoutPolling = 150; +abstract class BaseConditionalWaitTest { + static final Duration waitForTimeoutCondition = Duration.ofSeconds(10); + static final Duration waitForTimeoutPolling = Duration.ofMillis(150); static final long accuracy = 3; static final Collection> ignoredExceptions = Collections.singleton(IllegalStateException.class); ThreadLocal timer = ThreadLocal.withInitial(Timer::new); diff --git a/src/test/java/tests/waitings/WaitForObjectTests.java b/src/test/java/tests/waitings/WaitForObjectTests.java index ae53a7c..c5b8fad 100644 --- a/src/test/java/tests/waitings/WaitForObjectTests.java +++ b/src/test/java/tests/waitings/WaitForObjectTests.java @@ -7,6 +7,7 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import java.time.Duration; import java.util.Collections; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicBoolean; @@ -37,14 +38,14 @@ public Object[][] failWaitForAction() { } @Test(dataProvider = "failWaitForAction") - public void testShouldThrowTimeoutExceptionIfConditionIsNotMetAndTimeoutIsOver(Callable failedAction, long timeout) throws Exception { + public void testShouldThrowTimeoutExceptionIfConditionIsNotMetAndTimeoutIsOver(Callable failedAction, Duration timeout) throws Exception { timer.get().start(); try { failedAction.call(); } catch (TimeoutException e) { double duration = timer.get().stop(); - long interval = 2 * timeout + accuracy; - assertTrue(duration >= timeout && duration < interval, + long interval = 2 * timeout.getSeconds() + accuracy; + assertTrue(duration >= timeout.getSeconds() && duration < interval, String.format("Duration '%s' should be between '%s' and '%s' (timeout and (2*timeout + accuracy)) when condition is not satisfied. ", duration, timeout, interval)); } @@ -56,11 +57,11 @@ public Object[][] successWaitForAction() { } @Test(dataProvider = "successWaitForAction") - public void testShouldReturnAnObjectIfConditionIsMetAndTimeoutIsNotOver(Callable successAction, long timeout) throws Exception { + public void testShouldReturnAnObjectIfConditionIsMetAndTimeoutIsNotOver(Callable successAction, Duration timeout) throws Exception { timer.get().start(); String result = successAction.call(); double duration = timer.get().stop(); - assertTrue(duration <= timeout, + assertTrue(duration <= timeout.getSeconds(), String.format("Duration '%s' should be less than accuracyTimeout '%s'", duration, timeout)); assertEquals(result, RESULT_STRING, "Method should return correct object"); @@ -74,13 +75,13 @@ public Object[][] throwWaitForAction() { } @Test(dataProvider = "throwWaitForAction") - public void testShouldThrowException(Callable throwAction, long timeout) throws Exception { + public void testShouldThrowException(Callable throwAction, Duration timeout) throws Exception { try { timer.get().start(); throwAction.call(); } catch (IllegalArgumentException e) { double duration = timer.get().stop(); - assertTrue(duration <= timeout, + assertTrue(duration <= timeout.getSeconds(), String.format("Duration '%s' should be less than accuracyTimeout '%s'", duration, timeout)); assertEquals(e.getMessage(), "I am exception", "It should be custom exception"); @@ -91,14 +92,14 @@ public void testShouldThrowException(Callable throwAction, long timeout) public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithExceptions = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()); + checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition()); } @Test public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), "Condition should be true", ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()); + checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition()); } @Test @@ -115,8 +116,8 @@ public void testShouldIgnoreExceptionWaitingWithCustomTimeout() { checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutCondition); } - private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, long timeout) { - long accuracyTimeout = timeout + accuracy; + private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, Duration timeout) { + long accuracyTimeout = timeout.getSeconds() + accuracy; timer.get().start(); boolean result = waitAction.getAsBoolean(); double duration = timer.get().stop(); @@ -136,10 +137,10 @@ private Object[][] getDataProvider(ExpectedCondition action) { Callable actionWithCustomTimeoutsAndExceptions = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, Collections.emptyList()); Callable actionWithAllParameters = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", Collections.emptyList()); return new Object[][]{ - {onlyAction, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithMessage, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()}, + {onlyAction, timeoutConfiguration.getCondition()}, + {actionWithMessage, timeoutConfiguration.getCondition()}, + {actionWithExceptions, timeoutConfiguration.getCondition()}, + {actionWithMessageAndExceptions, timeoutConfiguration.getCondition()}, {actionWithCustomTimeouts, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition}, diff --git a/src/test/java/tests/waitings/WaitForTests.java b/src/test/java/tests/waitings/WaitForTests.java index 9dc647c..668a101 100644 --- a/src/test/java/tests/waitings/WaitForTests.java +++ b/src/test/java/tests/waitings/WaitForTests.java @@ -4,6 +4,7 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import java.time.Duration; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.BooleanSupplier; @@ -19,13 +20,13 @@ public Object[][] falseWaitForAction() { } @Test(dataProvider = "falseWaitForAction") - public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(BooleanSupplier waitAction, long timeout) { + public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(BooleanSupplier waitAction, Duration timeout) { Date startTime = new Date(); boolean result = waitAction.getAsBoolean(); long duration = (new Date().getTime() - startTime.getTime()) / 1000; - long interval = 2 * timeout + accuracy; + long interval = 2 * timeout.getSeconds() + accuracy; assertFalse(result, "waitFor should return false when condition is not satisfied."); - assertTrue(duration >= timeout && duration < interval, + assertTrue(duration >= timeout.getSeconds() && duration < interval, String.format("Duration '%s' should be between '%s' and '%s' (timeout and (2*timeout + accuracy)) when condition is not satisfied. ", duration, timeout, interval)); } @@ -36,7 +37,7 @@ public Object[][] trueWaitForAction() { } @Test(dataProvider = "trueWaitForAction") - public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver(BooleanSupplier waitAction, long timeout) { + public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver(BooleanSupplier waitAction, Duration timeout) { checkWaitForMethodForPassedCondition(waitAction, timeout); } @@ -44,14 +45,14 @@ public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver(BooleanS public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithExceptions = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()); + checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition()); } @Test public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout() { AtomicBoolean atomicBoolean = new AtomicBoolean(true); BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), "Condition should be true", ignoredExceptions); - checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()); + checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition()); } @Test @@ -68,13 +69,13 @@ public void testShouldIgnoreExceptionWaitingWithCustomTimeout() { checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutCondition); } - private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, long timeout) { - long accuracyTimeout = timeout + accuracy; + private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, Duration timeout) { + long accuracyTimeout = timeout.getSeconds() + accuracy; timer.get().start(); boolean result = waitAction.getAsBoolean(); double duration = timer.get().stop(); assertTrue(result, "waitFor should return true when condition is satisfied."); - assertTrue(duration <= timeout, + assertTrue(duration <= timeout.getSeconds(), String.format("Duration '%s' should be less than accuracyTimeout '%s'", duration, accuracyTimeout)); } @@ -88,7 +89,7 @@ public Object[][] throwExceptionAction() { } @Test(dataProvider = "throwExceptionAction", expectedExceptions = StaleElementReferenceException.class) - public void testShouldThrowException(BooleanSupplier waitAction, long timeout) { + public void testShouldThrowException(BooleanSupplier waitAction, Duration timeout) { waitAction.getAsBoolean(); } @@ -107,10 +108,10 @@ private Object[][] getDataProvider(BooleanSupplier action) { BooleanSupplier actionWithCustomTimeoutsAndExceptions = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, Collections.emptyList()); BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", Collections.emptyList()); return new Object[][]{ - {onlyAction, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithMessage, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()}, + {onlyAction, timeoutConfiguration.getCondition()}, + {actionWithMessage, timeoutConfiguration.getCondition()}, + {actionWithExceptions, timeoutConfiguration.getCondition()}, + {actionWithMessageAndExceptions, timeoutConfiguration.getCondition()}, {actionWithCustomTimeouts, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition}, diff --git a/src/test/java/tests/waitings/WaitForTrueTests.java b/src/test/java/tests/waitings/WaitForTrueTests.java index 6bc138c..48f0c87 100644 --- a/src/test/java/tests/waitings/WaitForTrueTests.java +++ b/src/test/java/tests/waitings/WaitForTrueTests.java @@ -4,6 +4,7 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Test; +import java.time.Duration; import java.util.Collections; import java.util.concurrent.Callable; import java.util.concurrent.TimeoutException; @@ -20,14 +21,14 @@ public Object[][] falseWaitForAction() { } @Test(dataProvider = "falseWaitForTrueAction") - public void testTimeoutExceptionShouldBeThrownIfConditionIsMetAndTimeoutIsOver(Callable waitForTrueAction, long timeout) throws Exception { + public void testTimeoutExceptionShouldBeThrownIfConditionIsMetAndTimeoutIsOver(Callable waitForTrueAction, Duration timeout) throws Exception { timer.get().start(); try { waitForTrueAction.call(); } catch (TimeoutException e) { double duration = timer.get().stop(); - long interval = 2 * timeout + accuracy; - assertTrue(duration >= timeout && duration < interval, + long interval = 2 * timeout.getSeconds() + accuracy; + assertTrue(duration >= timeout.getSeconds() && duration < interval, String.format("Duration '%s' should be between '%s' and '%s' (timeout and (2*timeout + accuracy)) when condition is not satisfied.", duration, timeout, interval)); } @@ -39,11 +40,11 @@ public Object[][] successWaitForAction() { } @Test(dataProvider = "successWaitForAction") - public void testTimeoutExceptionShouldNotBeThrownIfConditionIsMetAndTimeoutIsNotOver(Callable waitForTrueAction, long timeout) throws Exception { + public void testTimeoutExceptionShouldNotBeThrownIfConditionIsMetAndTimeoutIsNotOver(Callable waitForTrueAction, Duration timeout) throws Exception { timer.get().start(); waitForTrueAction.call(); double duration = timer.get().stop(); - assertTrue(duration < timeout, + assertTrue(duration < timeout.getSeconds(), String.format("Duration '%s' should be less than timeout '%s' when condition is satisfied.", duration, timeout)); } @@ -57,11 +58,11 @@ public Object[][] throwExceptionAction() { } @Test(dataProvider = "throwExceptionAction", expectedExceptions = StaleElementReferenceException.class) - public void testCustomExceptionShouldBeThrown(Callable waitForTrueAction, long timeout) throws Exception { + public void testCustomExceptionShouldBeThrown(Callable waitForTrueAction, Duration timeout) throws Exception { timer.get().start(); waitForTrueAction.call(); double duration = timer.get().stop(); - assertTrue(duration < timeout, + assertTrue(duration < timeout.getSeconds(), String.format("Duration '%s' should be less than timeout '%s' when condition is satisfied.", duration, timeout)); } @@ -77,7 +78,7 @@ public void testCustomExceptionShouldBeIgnoredWithoutCustomParameters() throws E checkExceptionIsIgnored(() -> { conditionalWait.waitForTrue(throwNewException(atomicBoolean), ignoredExceptions); return true; - }, timeoutConfiguration.getCondition().getSeconds()); + }, timeoutConfiguration.getCondition()); } @Test @@ -86,7 +87,7 @@ public void testCustomExceptionShouldBeIgnoredWithDefaultTimeout() throws Except checkExceptionIsIgnored(() -> { conditionalWait.waitForTrue(throwNewException(atomicBoolean), "Condition should be true", ignoredExceptions); return true; - }, timeoutConfiguration.getCondition().getSeconds()); + }, timeoutConfiguration.getCondition()); } @Test @@ -107,11 +108,11 @@ public void testCustomExceptionShouldBeIgnoredWithCustomTimeout() throws Excepti }, waitForTimeoutCondition); } - private void checkExceptionIsIgnored(Callable waitForTrueAction, long timeout) throws Exception { + private void checkExceptionIsIgnored(Callable waitForTrueAction, Duration timeout) throws Exception { timer.get().start(); waitForTrueAction.call(); double duration = timer.get().stop(); - assertTrue(duration < timeout, + assertTrue(duration < timeout.getSeconds(), String.format("Duration '%s' should be less than timeout '%s' when condition is satisfied.", duration, timeout)); } @@ -159,10 +160,10 @@ private Object[][] getDataProvider(BooleanSupplier action) { }; return new Object[][]{ - {onlyAction, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithMessage, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithExceptions, timeoutConfiguration.getCondition().getSeconds()}, - {actionWithMessageAndExceptions, timeoutConfiguration.getCondition().getSeconds()}, + {onlyAction, timeoutConfiguration.getCondition()}, + {actionWithMessage, timeoutConfiguration.getCondition()}, + {actionWithExceptions, timeoutConfiguration.getCondition()}, + {actionWithMessageAndExceptions, timeoutConfiguration.getCondition()}, {actionWithCustomTimeouts, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition}, {actionWithCustomTimeoutsAndException, waitForTimeoutCondition}, From 9fe50d59d64dc04d79a0f61ae9a7ace58206ac90 Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Thu, 13 Feb 2020 12:36:53 +0300 Subject: [PATCH 4/9] Migrate Element services to Duration timeouts --- .../elements/CachedElementStateProvider.java | 22 +++---- .../elements/DefaultElementStateProvider.java | 32 +++++------ .../core/elements/ElementCacheHandler.java | 4 +- .../core/elements/ElementFactory.java | 3 +- .../selenium/core/elements/ElementFinder.java | 4 +- .../core/elements/ElementStateProvider.java | 5 -- .../core/elements/RelativeElementFinder.java | 4 +- .../interfaces/IElementCacheHandler.java | 12 ++-- .../elements/interfaces/IElementFinder.java | 57 ++++++++++--------- .../interfaces/IElementStateProvider.java | 17 +++--- .../tests/elements/ICachedElementTests.java | 3 +- .../tests/elements/IElementFinderTests.java | 10 ++-- .../IWebElementStateProviderTests.java | 25 ++++---- .../java/theinternet/DynamicLoadingForm.java | 9 +-- 14 files changed, 109 insertions(+), 98 deletions(-) diff --git a/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java b/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java index 79f926e..5a1114d 100644 --- a/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java +++ b/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java @@ -43,7 +43,7 @@ protected boolean tryInvokeFunction(Predicate predicate) { protected boolean tryInvokeFunction(Predicate predicate, List> handledExceptions) { try { - return predicate.test(elementCacheHandler.getElement(getZeroTImeout(), ElementState.EXISTS_IN_ANY_STATE)); + return predicate.test(elementCacheHandler.getElement(Duration.ZERO, ElementState.EXISTS_IN_ANY_STATE)); } catch (Exception exception) { if (handledExceptions.contains(exception.getClass())) { return false; @@ -52,8 +52,8 @@ protected boolean tryInvokeFunction(Predicate predicate, List tryInvokeFunction(WebElement::isDisplayed), ElementState.DISPLAYED.toString(), timeout); } @Override - public boolean waitForNotDisplayed(Long timeout) { + public boolean waitForNotDisplayed(Duration timeout) { return waitForCondition(() -> !isDisplayed(), "invisible or absent", timeout); } @@ -98,12 +98,12 @@ public boolean isExist() { } @Override - public boolean waitForExist(Long timeout) { + public boolean waitForExist(Duration timeout) { return waitForCondition(() -> tryInvokeFunction(element -> true), ElementState.EXISTS_IN_ANY_STATE.toString(), timeout); } @Override - public boolean waitForNotExist(Long timeout) { + public boolean waitForNotExist(Duration timeout) { return waitForCondition(() -> !isExist(), "absent", timeout); } @@ -113,12 +113,12 @@ public boolean isEnabled() { } @Override - public boolean waitForEnabled(Long timeout) { + public boolean waitForEnabled(Duration timeout) { return waitForCondition(this::isEnabled, elementEnabled().getStateName(), timeout); } @Override - public boolean waitForNotEnabled(Long timeout) { + public boolean waitForNotEnabled(Duration timeout) { return waitForCondition(() -> !isEnabled(), elementNotEnabled().getStateName(), timeout); } } diff --git a/src/main/java/aquality/selenium/core/elements/DefaultElementStateProvider.java b/src/main/java/aquality/selenium/core/elements/DefaultElementStateProvider.java index 85a419a..8c89fea 100644 --- a/src/main/java/aquality/selenium/core/elements/DefaultElementStateProvider.java +++ b/src/main/java/aquality/selenium/core/elements/DefaultElementStateProvider.java @@ -20,70 +20,70 @@ public DefaultElementStateProvider(By locator, IConditionalWait conditionalWait, @Override public boolean isClickable() { - return waitForIsClickable(getZeroTImeout(), true); + return waitForIsClickable(Duration.ZERO, true); } @Override - public void waitForClickable(Long timeout) { + public void waitForClickable(Duration timeout) { waitForIsClickable(timeout, false); } - private boolean waitForIsClickable(Long timeout, boolean catchTimeoutException) { + private boolean waitForIsClickable(Duration timeout, boolean catchTimeoutException) { DesiredState desiredState = elementClickable(); desiredState = catchTimeoutException ? desiredState.withCatchingTimeoutException() : desiredState; return isElementInDesiredCondition(desiredState, timeout); } - private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Long timeout) { + private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Duration timeout) { return !elementFinder.findElements(locator, elementStateCondition, timeout).isEmpty(); } @Override public boolean isDisplayed() { - return waitForDisplayed(getZeroTImeout()); + return waitForDisplayed(Duration.ZERO); } @Override - public boolean waitForDisplayed(Long timeout) { + public boolean waitForDisplayed(Duration timeout) { return isAnyElementFound(timeout, ElementState.DISPLAYED); } - private boolean isAnyElementFound(Long timeout, ElementState state) { + private boolean isAnyElementFound(Duration timeout, ElementState state) { return !elementFinder.findElements(locator, state, timeout).isEmpty(); } @Override - public boolean waitForNotDisplayed(Long timeout) { - return conditionalWait.waitFor(() -> !isDisplayed(), timeout == null ? null : Duration.ofSeconds(timeout)); + public boolean waitForNotDisplayed(Duration timeout) { + return conditionalWait.waitFor(() -> !isDisplayed(), timeout); } @Override public boolean isExist() { - return waitForExist(getZeroTImeout()); + return waitForExist(Duration.ZERO); } @Override - public boolean waitForExist(Long timeout) { + public boolean waitForExist(Duration timeout) { return isAnyElementFound(timeout, ElementState.EXISTS_IN_ANY_STATE); } @Override - public boolean waitForNotExist(Long timeout) { - return conditionalWait.waitFor(() -> !isExist(), timeout == null ? null : Duration.ofSeconds(timeout)); + public boolean waitForNotExist(Duration timeout) { + return conditionalWait.waitFor(() -> !isExist(), timeout); } @Override public boolean isEnabled() { - return waitForEnabled(getZeroTImeout()); + return waitForEnabled(Duration.ZERO); } @Override - public boolean waitForEnabled(Long timeout) { + public boolean waitForEnabled(Duration timeout) { return isElementInDesiredCondition(elementEnabled(), timeout); } @Override - public boolean waitForNotEnabled(Long timeout) { + public boolean waitForNotEnabled(Duration timeout) { return isElementInDesiredCondition(elementNotEnabled(), timeout); } } diff --git a/src/main/java/aquality/selenium/core/elements/ElementCacheHandler.java b/src/main/java/aquality/selenium/core/elements/ElementCacheHandler.java index caadf0b..11a6123 100644 --- a/src/main/java/aquality/selenium/core/elements/ElementCacheHandler.java +++ b/src/main/java/aquality/selenium/core/elements/ElementCacheHandler.java @@ -5,6 +5,8 @@ import org.openqa.selenium.By; import org.openqa.selenium.remote.RemoteWebElement; +import java.time.Duration; + /** * Implementation of {@link IElementCacheHandler}. */ @@ -44,7 +46,7 @@ public boolean wasCached() { } @Override - public RemoteWebElement getElement(Long timeout, ElementState customState) { + public RemoteWebElement getElement(Duration timeout, ElementState customState) { ElementState requiredState = customState == null ? state : customState; if (isRefreshNeeded(requiredState)) { remoteElement = (RemoteWebElement) finder.findElement(locator, requiredState, timeout); diff --git a/src/main/java/aquality/selenium/core/elements/ElementFactory.java b/src/main/java/aquality/selenium/core/elements/ElementFactory.java index edacc1d..357aabb 100644 --- a/src/main/java/aquality/selenium/core/elements/ElementFactory.java +++ b/src/main/java/aquality/selenium/core/elements/ElementFactory.java @@ -16,6 +16,7 @@ import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; +import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -25,7 +26,7 @@ public class ElementFactory implements IElementFactory { private static final int XPATH_SUBSTRING_BEGIN_INDEX = 10; - private static final long ZERO_TIMEOUT = 0L; + private static final Duration ZERO_TIMEOUT = Duration.ZERO; private final IConditionalWait conditionalWait; private final IElementFinder elementFinder; diff --git a/src/main/java/aquality/selenium/core/elements/ElementFinder.java b/src/main/java/aquality/selenium/core/elements/ElementFinder.java index 6a15ab1..97e5634 100644 --- a/src/main/java/aquality/selenium/core/elements/ElementFinder.java +++ b/src/main/java/aquality/selenium/core/elements/ElementFinder.java @@ -25,13 +25,13 @@ public ElementFinder(ILocalizedLogger localizedLogger, IConditionalWait conditio } @Override - public List findElements(By locator, DesiredState desiredState, Long timeoutInSeconds) { + public List findElements(By locator, DesiredState desiredState, Duration timeout) { AtomicBoolean wasAnyElementFound = new AtomicBoolean(false); List resultElements = new ArrayList<>(); try { conditionalWait.waitFor(driver -> tryToFindElements(locator, desiredState, wasAnyElementFound, resultElements, driver), - timeoutInSeconds == null ? null : Duration.ofSeconds(timeoutInSeconds)); + timeout); } catch (TimeoutException e) { handleTimeoutException(e, locator, desiredState, wasAnyElementFound.get()); } diff --git a/src/main/java/aquality/selenium/core/elements/ElementStateProvider.java b/src/main/java/aquality/selenium/core/elements/ElementStateProvider.java index d0976fe..bc96bf0 100644 --- a/src/main/java/aquality/selenium/core/elements/ElementStateProvider.java +++ b/src/main/java/aquality/selenium/core/elements/ElementStateProvider.java @@ -4,11 +4,6 @@ import org.openqa.selenium.WebElement; public abstract class ElementStateProvider implements IElementStateProvider { - private static final long ZERO_TIMEOUT = 0L; - - protected Long getZeroTImeout() { - return ZERO_TIMEOUT; - } protected boolean isElementEnabled(WebElement element) { return element.isEnabled(); diff --git a/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java b/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java index d885483..8339451 100644 --- a/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java +++ b/src/main/java/aquality/selenium/core/elements/RelativeElementFinder.java @@ -28,14 +28,14 @@ public RelativeElementFinder(ILocalizedLogger localizedLogger, IConditionalWait } @Override - public List findElements(By locator, DesiredState desiredState, Long timeoutInSeconds) { + public List findElements(By locator, DesiredState desiredState, Duration timeout) { AtomicBoolean wasAnyElementFound = new AtomicBoolean(false); List resultElements = new ArrayList<>(); try { conditionalWait.waitForTrue(() -> tryToFindElements(locator, desiredState, wasAnyElementFound, resultElements, searchContextSupplier.get()), - timeoutInSeconds == null ? null : Duration.ofSeconds(timeoutInSeconds)); + timeout); } catch (TimeoutException e) { handleTimeoutException(new org.openqa.selenium.TimeoutException(e.getMessage(), e), locator, desiredState, wasAnyElementFound.get()); diff --git a/src/main/java/aquality/selenium/core/elements/interfaces/IElementCacheHandler.java b/src/main/java/aquality/selenium/core/elements/interfaces/IElementCacheHandler.java index 0239153..e384b9b 100644 --- a/src/main/java/aquality/selenium/core/elements/interfaces/IElementCacheHandler.java +++ b/src/main/java/aquality/selenium/core/elements/interfaces/IElementCacheHandler.java @@ -3,6 +3,8 @@ import aquality.selenium.core.elements.ElementState; import org.openqa.selenium.remote.RemoteWebElement; +import java.time.Duration; + /** * Allows to use cached element. */ @@ -27,6 +29,7 @@ default boolean isRefreshNeeded() { /** * Determines is the element stale. + * * @return true if the element was found previously and is currently stale, false otherwise. */ default boolean isStale() { @@ -35,6 +38,7 @@ default boolean isStale() { /** * Determines was the element cached previously. + * * @return true if the element was found and cached previously, false otherwise. */ boolean wasCached(); @@ -42,19 +46,19 @@ default boolean isStale() { /** * Allows to get cached element. * - * @param timeout timeout used to retrive the element when {@see isRefreshNeeded()} is true. + * @param timeout timeout used to retrive the element when {@link #isRefreshNeeded()} is true. * @param customState custom element's existance state used for search. * @return cached element. */ - RemoteWebElement getElement(Long timeout, ElementState customState); + RemoteWebElement getElement(Duration timeout, ElementState customState); /** * Allows to get cached element. * - * @param timeout timeout used to retrive the element when {@see isRefreshNeeded()} is true. + * @param timeout timeout used to retrive the element when {@link #isRefreshNeeded()} is true. * @return cached element. */ - default RemoteWebElement getElement(Long timeout) { + default RemoteWebElement getElement(Duration timeout) { return getElement(timeout, null); } diff --git a/src/main/java/aquality/selenium/core/elements/interfaces/IElementFinder.java b/src/main/java/aquality/selenium/core/elements/interfaces/IElementFinder.java index 400432e..f4fcc01 100644 --- a/src/main/java/aquality/selenium/core/elements/interfaces/IElementFinder.java +++ b/src/main/java/aquality/selenium/core/elements/interfaces/IElementFinder.java @@ -6,6 +6,7 @@ import org.openqa.selenium.SearchContext; import org.openqa.selenium.WebElement; +import java.time.Duration; import java.util.List; import java.util.function.Predicate; @@ -40,14 +41,14 @@ default DesiredState resolveState(ElementState state) { /** * Finds element in desired {@link ElementState} * - * @param locator elements locator - * @param state desired {@link ElementState} - * @param timeoutInSeconds timeout for search + * @param locator elements locator + * @param state desired {@link ElementState} + * @param timeout timeout for search * @return found element * @throws org.openqa.selenium.NoSuchElementException if element was not found in time in desired state */ - default WebElement findElement(By locator, ElementState state, Long timeoutInSeconds) { - return findElements(locator, resolveState(state).withThrowingNoSuchElementException(), timeoutInSeconds).get(0); + default WebElement findElement(By locator, ElementState state, Duration timeout) { + return findElements(locator, resolveState(state).withThrowingNoSuchElementException(), timeout).get(0); } /** @@ -65,13 +66,13 @@ default WebElement findElement(By locator, ElementState state) { /** * Finds element in any state. * - * @param locator elements locator - * @param timeoutInSeconds timeout for search + * @param locator elements locator + * @param timeout timeout for search * @return found element * @throws org.openqa.selenium.NoSuchElementException if element was not found in time */ - default WebElement findElement(By locator, Long timeoutInSeconds) { - return findElement(locator, ElementState.EXISTS_IN_ANY_STATE, timeoutInSeconds); + default WebElement findElement(By locator, Duration timeout) { + return findElement(locator, ElementState.EXISTS_IN_ANY_STATE, timeout); } /** @@ -90,14 +91,14 @@ default WebElement findElement(By locator) { * * @param locator elements locator * @param elementStateCondition predicate to define element state - * @param timeoutInSeconds timeout for search + * @param timeout timeout for search * @return found element * @throws org.openqa.selenium.NoSuchElementException if element was not found in time in desired state */ - default WebElement findElement(By locator, Predicate elementStateCondition, Long timeoutInSeconds) { + default WebElement findElement(By locator, Predicate elementStateCondition, Duration timeout) { DesiredState state = new DesiredState(elementStateCondition, "desired") .withThrowingNoSuchElementException(); - return findElements(locator, state, timeoutInSeconds).get(0); + return findElements(locator, state, timeout).get(0); } /** @@ -115,24 +116,24 @@ default WebElement findElement(By locator, Predicate elementStateCon /** * Finds elements in desired {@link ElementState}. * - * @param locator elements locator - * @param state desired {@link ElementState} - * @param timeoutInSeconds timeout for search + * @param locator elements locator + * @param state desired {@link ElementState} + * @param timeout timeout for search * @return list of found elements */ - default List findElements(By locator, ElementState state, Long timeoutInSeconds) { - return findElements(locator, resolveState(state).withCatchingTimeoutException(), timeoutInSeconds); + default List findElements(By locator, ElementState state, Duration timeout) { + return findElements(locator, resolveState(state).withCatchingTimeoutException(), timeout); } /** * Finds elements in any state. * - * @param locator elements locator - * @param timeoutInSeconds timeout for search + * @param locator elements locator + * @param timeout timeout for search * @return list of found elements */ - default List findElements(By locator, Long timeoutInSeconds) { - return findElements(locator, ElementState.EXISTS_IN_ANY_STATE, timeoutInSeconds); + default List findElements(By locator, Duration timeout) { + return findElements(locator, ElementState.EXISTS_IN_ANY_STATE, timeout); } /** @@ -160,13 +161,13 @@ default List findElements(By locator) { * Finds elements in state defined by predicate. * * @param locator elements locator - * @param timeoutInSeconds timeout for search + * @param timeout timeout for search * @param elementStateCondition predicate to define element state * @return list of found elements */ - default List findElements(By locator, Predicate elementStateCondition, Long timeoutInSeconds) { + default List findElements(By locator, Predicate elementStateCondition, Duration timeout) { DesiredState state = new DesiredState(elementStateCondition, "desired").withCatchingTimeoutException(); - return findElements(locator, state, timeoutInSeconds); + return findElements(locator, state, timeout); } /** @@ -183,12 +184,12 @@ default List findElements(By locator, Predicate elementS /** * Finds elements in state defined by {@link DesiredState}. * - * @param locator elements locator - * @param timeoutInSeconds timeout for search - * @param desiredState object with predicate to define element state + * @param locator elements locator + * @param timeout timeout for search + * @param desiredState object with predicate to define element state * @return list of found elements */ - List findElements(By locator, DesiredState desiredState, Long timeoutInSeconds); + List findElements(By locator, DesiredState desiredState, Duration timeout); /** * Finds elements in state defined by {@link DesiredState} with default timeout. diff --git a/src/main/java/aquality/selenium/core/elements/interfaces/IElementStateProvider.java b/src/main/java/aquality/selenium/core/elements/interfaces/IElementStateProvider.java index 980a1ff..c16ad68 100644 --- a/src/main/java/aquality/selenium/core/elements/interfaces/IElementStateProvider.java +++ b/src/main/java/aquality/selenium/core/elements/interfaces/IElementStateProvider.java @@ -1,5 +1,6 @@ package aquality.selenium.core.elements.interfaces; +import java.time.Duration; /** * Provides ability to define of element's state (whether it is displayed, exist or not) @@ -20,7 +21,7 @@ public interface IElementStateProvider { * @param timeout Timeout for waiting * @throws org.openqa.selenium.TimeoutException when timeout exceeded and element is not clickable. */ - void waitForClickable(Long timeout); + void waitForClickable(Duration timeout); /** * Waits for is element clickable on the form. @@ -45,7 +46,7 @@ default void waitForClickable() { * @param timeout Timeout for waiting * @return true if element displayed after waiting, false otherwise */ - boolean waitForDisplayed(Long timeout); + boolean waitForDisplayed(Duration timeout); /** * Waits for is element displayed on the form. @@ -63,7 +64,7 @@ default boolean waitForDisplayed() { * @param timeout Timeout for waiting * @return true if element displayed after waiting, false otherwise */ - boolean waitForNotDisplayed(Long timeout); + boolean waitForNotDisplayed(Duration timeout); /** @@ -89,7 +90,7 @@ default boolean waitForNotDisplayed() { * @param timeout Timeout for waiting * @return true if element exist after waiting, false otherwise */ - boolean waitForExist(Long timeout); + boolean waitForExist(Duration timeout); /** @@ -105,9 +106,10 @@ default boolean waitForExist() { /** * Waits until element does not exist in DOM (without visibility check). * + * @param timeout Timeout for waiting * @return true if element does not exist after waiting, false otherwise */ - boolean waitForNotExist(Long timeout); + boolean waitForNotExist(Duration timeout); /** * Waits until element does not exist in DOM (without visibility check). @@ -134,7 +136,7 @@ default boolean waitForNotExist() { * @return true if enabled * @throws org.openqa.selenium.NoSuchElementException when timeout exceeded and element not found. */ - boolean waitForEnabled(Long timeout); + boolean waitForEnabled(Duration timeout); /** @@ -151,10 +153,11 @@ default boolean waitForEnabled() { /** * Waits until element does not enabled in DOM * + * @param timeout Timeout for waiting * @return true if element does not enabled after waiting, false otherwise * @throws org.openqa.selenium.NoSuchElementException when timeout exceeded and element not found. */ - boolean waitForNotEnabled(Long timeout); + boolean waitForNotEnabled(Duration timeout); /** * Waits until element does not enabled in DOM diff --git a/src/test/java/tests/elements/ICachedElementTests.java b/src/test/java/tests/elements/ICachedElementTests.java index 933cc76..07349a6 100644 --- a/src/test/java/tests/elements/ICachedElementTests.java +++ b/src/test/java/tests/elements/ICachedElementTests.java @@ -3,12 +3,13 @@ import aquality.selenium.core.elements.interfaces.IElementStateProvider; import org.testng.annotations.DataProvider; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; public interface ICachedElementTests { - long ZERO_TIMEOUT = 0L; + Duration ZERO_TIMEOUT = Duration.ZERO; @DataProvider default Object[] stateFunctionsFalseWhenElementStale() { diff --git a/src/test/java/tests/elements/IElementFinderTests.java b/src/test/java/tests/elements/IElementFinderTests.java index c7f8979..fe5caeb 100644 --- a/src/test/java/tests/elements/IElementFinderTests.java +++ b/src/test/java/tests/elements/IElementFinderTests.java @@ -13,6 +13,8 @@ import tests.application.windowsApp.AqualityServices; import tests.application.windowsApp.CalculatorWindow; +import java.time.Duration; + public interface IElementFinderTests extends ITestWithApplication { IElementFinder getElementFinder(); @@ -29,12 +31,12 @@ default Object[] elementStates() { return ElementState.values(); } - default Long getCustomTimeout() { - return 10L; + default Duration getCustomTimeout() { + return Duration.ofSeconds(10L); } - default Long getSmallTimeout() { - return 1L; + default Duration getSmallTimeout() { + return Duration.ofSeconds(1L); } default By getAbsentLocator() { diff --git a/src/test/java/tests/elements/IWebElementStateProviderTests.java b/src/test/java/tests/elements/IWebElementStateProviderTests.java index b62dac8..b0b0aac 100644 --- a/src/test/java/tests/elements/IWebElementStateProviderTests.java +++ b/src/test/java/tests/elements/IWebElementStateProviderTests.java @@ -12,6 +12,7 @@ import theinternet.DynamicLoadingForm; import theinternet.TheInternetPage; +import java.time.Duration; import java.util.function.Predicate; import static utils.TimeUtil.calculateDuration; @@ -19,7 +20,7 @@ public interface IWebElementStateProviderTests extends ITheInternetPageTest { double OPERATION_TIME = 5; - long CUSTOM_WAIT_TIME = 2L; + Duration CUSTOM_WAIT_TIME = Duration.ofSeconds(2L); By ABSENT_ELEMENT_LOCATOR = By.xpath("//div[@class='not exist element']"); default DynamicControlsForm getDynamicControlsForm() { @@ -32,34 +33,34 @@ default DynamicLoadingForm getDynamicLoadingForm() { IElementStateProvider state(By locator); - default long getConditionTimeout() { - return AqualityServices.get(ITimeoutConfiguration.class).getCondition().getSeconds(); + default Duration getConditionTimeout() { + return AqualityServices.get(ITimeoutConfiguration.class).getCondition(); } - default void checkWaitingTimeForInputState(Long waitTime, Predicate notExpectedCondition) { + default void checkWaitingTimeForInputState(Duration waitTime, Predicate notExpectedCondition) { long startTime = getCurrentTime(); boolean isConditionSatisfied = notExpectedCondition.test(getDynamicControlsForm().inputState()); double duration = calculateDuration(startTime); Assert.assertFalse(isConditionSatisfied); - Assert.assertTrue(duration >= waitTime && duration <= (waitTime + OPERATION_TIME)); + Assert.assertTrue(duration >= waitTime.getSeconds() && duration <= (waitTime.getSeconds() + OPERATION_TIME)); } @Test default void testElementShouldWaitForEnabledWithCustomTimeout() { - long waitTime = CUSTOM_WAIT_TIME; + Duration waitTime = CUSTOM_WAIT_TIME; checkWaitingTimeForInputState(waitTime, state -> state.waitForEnabled(waitTime)); } @Test default void testElementShouldWaitForEnabledWithDefaultTimeout() { - long waitTime = getConditionTimeout(); + Duration waitTime = getConditionTimeout(); checkWaitingTimeForInputState(waitTime, IElementStateProvider::waitForEnabled); } @Test default void testElementShouldWaitForNotEnabledWithCustomTimeout() { - long waitTime = CUSTOM_WAIT_TIME; + Duration waitTime = CUSTOM_WAIT_TIME; getDynamicControlsForm().clickEnable(); getDynamicControlsForm().inputState().waitForEnabled(); @@ -78,7 +79,7 @@ default void testNoSuchShouldBeThrownForWaitNotEnabledIfElementNotFound() { @Test default void testElementShouldWaitForNotEnabledWithDefaultTimeout() { - long waitTime = getConditionTimeout(); + Duration waitTime = getConditionTimeout(); getDynamicControlsForm().clickEnable(); getDynamicControlsForm().inputState().waitForEnabled(); @@ -120,7 +121,7 @@ default void testWaitForExist() { @Test default void testShouldBePossibleToWaitElementNotExistsCustomTime() { - long waitTime = CUSTOM_WAIT_TIME; + Duration waitTime = CUSTOM_WAIT_TIME; getDynamicControlsForm().clickRemove(); checkWaitingTimeForInputState(waitTime, inputState -> getDynamicControlsForm().checkboxState().waitForNotExist(waitTime)); @@ -128,7 +129,7 @@ default void testShouldBePossibleToWaitElementNotExistsCustomTime() { @Test default void testShouldBePossibleToWaitElementNotExists() { - long waitTime = getConditionTimeout(); + Duration waitTime = getConditionTimeout(); getDynamicControlsForm().clickRemove(); long startTime = getCurrentTime(); @@ -136,6 +137,6 @@ default void testShouldBePossibleToWaitElementNotExists() { double duration = calculateDuration(startTime); Assert.assertTrue(isMissed); - Assert.assertTrue(duration < waitTime); + Assert.assertTrue(duration < waitTime.getSeconds()); } } diff --git a/src/test/java/theinternet/DynamicLoadingForm.java b/src/test/java/theinternet/DynamicLoadingForm.java index 8de7e90..85c388d 100644 --- a/src/test/java/theinternet/DynamicLoadingForm.java +++ b/src/test/java/theinternet/DynamicLoadingForm.java @@ -6,13 +6,14 @@ import org.openqa.selenium.By; import tests.application.browser.CachedLabel; +import java.time.Duration; import java.util.function.Function; import java.util.function.Supplier; public class DynamicLoadingForm extends BaseForm { - private static final long DEFAULT_LOADING_TIMEOUT = 5L; - private static final long SMALL_LOADING_TIMEOUT = 2L; + private static final Duration DEFAULT_LOADING_TIMEOUT = Duration.ofSeconds(5L); + private static final Duration SMALL_LOADING_TIMEOUT = Duration.ofSeconds(2L); private static final By LOADING_LABEL_LOCATOR = By.id("loading"); private static final By START_BUTTON_LOCATOR = By.xpath("//div[@id='start']/button"); @@ -28,11 +29,11 @@ public static CachedLabel getStartLabel() { return new CachedLabel(START_BUTTON_LOCATOR, ElementState.DISPLAYED); } - public long getTimeout() { + public Duration getTimeout() { return DEFAULT_LOADING_TIMEOUT; } - public long getSmallTimeout() { + public Duration getSmallTimeout() { return SMALL_LOADING_TIMEOUT; } From 28063cebc22cda30d5041c1ab0bba62d39beaf49 Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Fri, 14 Feb 2020 16:53:08 +0300 Subject: [PATCH 5/9] Remove parallel from dataproviders --- src/test/java/tests/waitings/WaitForObjectTests.java | 6 +++--- src/test/java/tests/waitings/WaitForTests.java | 6 +++--- src/test/java/tests/waitings/WaitForTrueTests.java | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/test/java/tests/waitings/WaitForObjectTests.java b/src/test/java/tests/waitings/WaitForObjectTests.java index e6d47cc..7e874ab 100644 --- a/src/test/java/tests/waitings/WaitForObjectTests.java +++ b/src/test/java/tests/waitings/WaitForObjectTests.java @@ -33,7 +33,7 @@ public void quit() { } } - @DataProvider(name = "failWaitForAction", parallel = true) + @DataProvider(name = "failWaitForAction") public Object[][] failWaitForAction() { return getDataProvider((app) -> false); } @@ -53,7 +53,7 @@ public void testShouldThrowTimeoutExceptionIfConditionIsNotMetAndTimeoutIsOver(C } } - @DataProvider(name = "successWaitForAction", parallel = true) + @DataProvider(name = "successWaitForAction") public Object[][] successWaitForAction() { return getDataProvider((app) -> RESULT_STRING); } @@ -70,7 +70,7 @@ public void testShouldReturnAnObjectIfConditionIsMetAndTimeoutIsNotOver(Callable assertEquals(result, RESULT_STRING, "Method should return correct object"); } - @DataProvider(name = "throwWaitForAction", parallel = true) + @DataProvider(name = "throwWaitForAction") public Object[][] throwWaitForAction() { return getDataProvider((app) -> { throw new IllegalArgumentException("I am exception"); diff --git a/src/test/java/tests/waitings/WaitForTests.java b/src/test/java/tests/waitings/WaitForTests.java index 53cf0c2..9c7c4fa 100644 --- a/src/test/java/tests/waitings/WaitForTests.java +++ b/src/test/java/tests/waitings/WaitForTests.java @@ -14,7 +14,7 @@ public class WaitForTests extends BaseConditionalWaitTest { - @DataProvider(name = "falseWaitForAction", parallel = true) + @DataProvider(name = "falseWaitForAction") public Object[][] falseWaitForAction() { return getDataProvider(() -> false); } @@ -31,7 +31,7 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(Boolean duration, timeout, accuracyPollingInterval)); } - @DataProvider(name = "trueWaitForAction", parallel = true) + @DataProvider(name = "trueWaitForAction") public Object[][] trueWaitForAction() { return getDataProvider(() -> true); } @@ -84,7 +84,7 @@ private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, do duration, checkedTimeout)); } - @DataProvider(name = "throwExceptionAction", parallel = true) + @DataProvider(name = "throwExceptionAction") public Object[][] throwExceptionAction() { BooleanSupplier throwEx = () -> { throw new StaleElementReferenceException(""); diff --git a/src/test/java/tests/waitings/WaitForTrueTests.java b/src/test/java/tests/waitings/WaitForTrueTests.java index c5dde3c..5af7788 100644 --- a/src/test/java/tests/waitings/WaitForTrueTests.java +++ b/src/test/java/tests/waitings/WaitForTrueTests.java @@ -16,7 +16,7 @@ public class WaitForTrueTests extends BaseConditionalWaitTest { - @DataProvider(name = "falseWaitForTrueAction", parallel = true) + @DataProvider(name = "falseWaitForTrueAction") public Object[][] falseWaitForAction() { return getDataProvider(() -> false); } @@ -36,7 +36,7 @@ public void testTimeoutExceptionShouldBeThrownIfConditionIsMetAndTimeoutIsOver(C } } - @DataProvider(name = "successWaitForAction", parallel = true) + @DataProvider(name = "successWaitForAction") public Object[][] successWaitForAction() { return getDataProvider(() -> true); } @@ -51,7 +51,7 @@ public void testTimeoutExceptionShouldNotBeThrownIfConditionIsMetAndTimeoutIsNot String.format("Duration '%s' should be less than accuracy polling interval '%s'", duration, accuracyPollingInterval)); } - @DataProvider(name = "throwExceptionAction", parallel = true) + @DataProvider(name = "throwExceptionAction") public Object[][] throwExceptionAction() { BooleanSupplier throwEx = () -> { throw new StaleElementReferenceException(""); From bd810946e3c546e9b558ebb63bc4d4c097baa50c Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Fri, 14 Feb 2020 17:07:22 +0300 Subject: [PATCH 6/9] Fix timeout logging --- .../selenium/core/elements/CachedElementStateProvider.java | 2 +- .../java/aquality/selenium/core/waitings/ConditionalWait.java | 2 +- src/test/java/tests/waitings/WaitForObjectTests.java | 2 +- src/test/java/tests/waitings/WaitForTests.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java b/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java index 5a1114d..1359e55 100644 --- a/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java +++ b/src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java @@ -55,7 +55,7 @@ protected boolean tryInvokeFunction(Predicate predicate, List timeoutInSeconds) { - String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeout, exMessage); + String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeout.getSeconds(), exMessage); throw new TimeoutException(exceptionMessage); } diff --git a/src/test/java/tests/waitings/WaitForObjectTests.java b/src/test/java/tests/waitings/WaitForObjectTests.java index 7e874ab..5065fbf 100644 --- a/src/test/java/tests/waitings/WaitForObjectTests.java +++ b/src/test/java/tests/waitings/WaitForObjectTests.java @@ -49,7 +49,7 @@ public void testShouldThrowTimeoutExceptionIfConditionIsNotMetAndTimeoutIsOver(C double interval = timeout.getSeconds() + pollingInterval.getSeconds() + accuracy; assertTrue(duration >= timeout.getSeconds() && duration < interval, String.format("Duration '%s' should be between '%s' and '%s' (timeout and (timeout + pollingInterval + accuracy)) when condition is not satisfied.", - duration, timeout, interval)); + duration, timeout.getSeconds(), interval)); } } diff --git a/src/test/java/tests/waitings/WaitForTests.java b/src/test/java/tests/waitings/WaitForTests.java index 9c7c4fa..2fcccae 100644 --- a/src/test/java/tests/waitings/WaitForTests.java +++ b/src/test/java/tests/waitings/WaitForTests.java @@ -28,7 +28,7 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(Boolean assertFalse(result, "waitFor should return false when condition is not satisfied."); assertTrue(duration >= timeout.getSeconds() && duration < accuracyPollingInterval, String.format("Duration '%s' should be between '%s' and '%s' (timeout and (timeout + pollingInterval + accuracy)) when condition is not satisfied.", - duration, timeout, accuracyPollingInterval)); + duration, timeout.getSeconds(), accuracyPollingInterval)); } @DataProvider(name = "trueWaitForAction") From 0858604fa2b3f66e5a419973d4dd57db64e88fc2 Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Fri, 14 Feb 2020 17:22:47 +0300 Subject: [PATCH 7/9] Fix nullpointer exception in conditional wait --- .../java/aquality/selenium/core/waitings/ConditionalWait.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java b/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java index ac1d8bc..96ad040 100644 --- a/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java +++ b/src/main/java/aquality/selenium/core/waitings/ConditionalWait.java @@ -51,7 +51,7 @@ public void waitForTrue(BooleanSupplier condition, Duration timeout, Duration po double currentTime = getCurrentTime(); if ((currentTime - startTime) > timeoutInSeconds) { - String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeout.getSeconds(), exMessage); + String exceptionMessage = String.format("Timed out after %1$s seconds during wait for condition '%2$s'", timeoutInSeconds, exMessage); throw new TimeoutException(exceptionMessage); } From af396cba579819a4b15f5ca20740af72d7af7baf Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Fri, 14 Feb 2020 17:50:37 +0300 Subject: [PATCH 8/9] Fix after merge --- src/main/java/aquality/selenium/core/elements/Element.java | 3 ++- src/test/java/tests/elements/factory/CustomElement.java | 3 +-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/aquality/selenium/core/elements/Element.java b/src/main/java/aquality/selenium/core/elements/Element.java index 95dc88b..7a11dec 100644 --- a/src/main/java/aquality/selenium/core/elements/Element.java +++ b/src/main/java/aquality/selenium/core/elements/Element.java @@ -12,6 +12,7 @@ import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.RemoteWebElement; +import java.time.Duration; import java.util.function.Supplier; public abstract class Element implements IElement { @@ -73,7 +74,7 @@ public IElementStateProvider state() { } @Override - public RemoteWebElement getElement(Long timeout) { + public RemoteWebElement getElement(Duration timeout) { try { return getElementCacheConfiguration().isEnabled() ? getCache().getElement(timeout) diff --git a/src/test/java/tests/elements/factory/CustomElement.java b/src/test/java/tests/elements/factory/CustomElement.java index 4b18dca..4eb0731 100644 --- a/src/test/java/tests/elements/factory/CustomElement.java +++ b/src/test/java/tests/elements/factory/CustomElement.java @@ -12,8 +12,6 @@ import org.openqa.selenium.By; import tests.applications.windowsApp.AqualityServices; -import java.time.Duration; - public class CustomElement extends Element implements ICustomElement { public CustomElement(By locator, String name, ElementState state) { @@ -28,6 +26,7 @@ protected IApplication getApplication() { @Override protected IElementFactory getElementFactory() { return AqualityServices.get(IElementFactory.class); + } @Override protected IElementFinder getElementFinder() { From 7bb6ab61f0f03a147d6415e37ea3b96dd37d701c Mon Sep 17 00:00:00 2001 From: "Meleshko, Aleksey2" Date: Fri, 14 Feb 2020 18:47:52 +0300 Subject: [PATCH 9/9] Fix compilation errors - timeouts in tests --- src/test/java/tests/elements/WebElementTests.java | 3 ++- src/test/java/tests/elements/WindowsElementTests.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/test/java/tests/elements/WebElementTests.java b/src/test/java/tests/elements/WebElementTests.java index 5aff7a2..59c94c9 100644 --- a/src/test/java/tests/elements/WebElementTests.java +++ b/src/test/java/tests/elements/WebElementTests.java @@ -14,6 +14,7 @@ import theinternet.InputsForm; import theinternet.TheInternetPage; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; @@ -60,7 +61,7 @@ public void testShouldBePossibleToGetElement() { @Test public void testShouldBePossibleToGetElementWithCustomTimeout() { getApplication().getDriver().navigate().to(TheInternetPage.DYNAMIC_CONTROLS.getAddress()); - RemoteWebElement element = DynamicControlsForm.getEnableButton().getElement(1L); + RemoteWebElement element = DynamicControlsForm.getEnableButton().getElement(Duration.ofSeconds(1L)); Assert.assertNotNull(element, "RemoteWebElement should not be null"); } diff --git a/src/test/java/tests/elements/WindowsElementTests.java b/src/test/java/tests/elements/WindowsElementTests.java index 649060e..4497397 100644 --- a/src/test/java/tests/elements/WindowsElementTests.java +++ b/src/test/java/tests/elements/WindowsElementTests.java @@ -12,6 +12,7 @@ import tests.applications.windowsApp.CalculatorWindow; import tests.elements.factory.CustomElement; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; @@ -56,7 +57,7 @@ public void testShouldBePossibleToGetElement() { @Test public void testShouldBePossibleToGetElementWithCustomTimeout() { - RemoteWebElement element = CalculatorWindow.getOneButton().getElement(1L); + RemoteWebElement element = CalculatorWindow.getOneButton().getElement(Duration.ofSeconds(1L)); Assert.assertNotNull(element, "RemoteWebElement should not be null"); }