Skip to content

Commit 238925a

Browse files
authored
Enhancement/27 migrate long timeouts to duration (#37)
* Migrate RetryConfiguration to Duration timeouts * Migrate TimeoutConfiguration to Duration timeouts * Migrate ConditionalWait to Duration timeouts * Migrate Element services to Duration timeouts * Fix timeout logging
1 parent c7f728e commit 238925a

36 files changed

+349
-281
lines changed

src/main/java/aquality/selenium/core/applications/IApplication.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.openqa.selenium.remote.RemoteWebDriver;
44

5-
import java.util.concurrent.TimeUnit;
5+
import java.time.Duration;
66

77
/**
88
* Interface of any application controlled by Selenium WebDriver API
@@ -22,7 +22,6 @@ public interface IApplication {
2222
/**
2323
* Sets implicit wait timeout to Selenium WebDriver.
2424
* @param value timeout value to set.
25-
* @param timeUnit timeUnit of timeout value.
2625
*/
27-
void setImplicitWaitTimeout(long value, TimeUnit timeUnit);
26+
void setImplicitWaitTimeout(Duration value);
2827
}

src/main/java/aquality/selenium/core/configurations/IRetryConfiguration.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package aquality.selenium.core.configurations;
22

3+
import java.time.Duration;
4+
35
/**
46
* Describes retry configuration.
57
*/
@@ -17,5 +19,5 @@ public interface IRetryConfiguration {
1719
*
1820
* @return Polling interval for retry.
1921
*/
20-
long getPollingInterval();
22+
Duration getPollingInterval();
2123
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package aquality.selenium.core.configurations;
22

3+
import java.time.Duration;
4+
35
/**
46
* Provides timeouts configuration.
57
*/
@@ -10,26 +12,26 @@ public interface ITimeoutConfiguration {
1012
*
1113
* @return ImplicitWait timeout.
1214
*/
13-
long getImplicit();
15+
Duration getImplicit();
1416

1517
/**
1618
* Gets default ConditionalWait timeout.
1719
*
1820
* @return ConditionalWait timeout.
1921
*/
20-
long getCondition();
22+
Duration getCondition();
2123

2224
/**
2325
* Gets ConditionalWait polling interval.
2426
*
2527
* @return polling interval.
2628
*/
27-
long getPollingInterval();
29+
Duration getPollingInterval();
2830

2931
/**
3032
* Gets Command timeout.
3133
*
3234
* @return Command timeout.
3335
*/
34-
long getCommand();
36+
Duration getCommand();
3537
}

src/main/java/aquality/selenium/core/configurations/RetryConfiguration.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import aquality.selenium.core.utilities.ISettingsFile;
44
import com.google.inject.Inject;
55

6+
import java.time.Duration;
7+
68
public class RetryConfiguration implements IRetryConfiguration {
79
private final int number;
8-
private final long pollingInterval;
10+
private final Duration pollingInterval;
911

1012
@Inject
1113
public RetryConfiguration(ISettingsFile settingsFile) {
1214
this.number = Integer.parseInt(settingsFile.getValue("/retry/number").toString());
13-
this.pollingInterval = Long.parseLong(settingsFile.getValue("/retry/pollingInterval").toString());
15+
this.pollingInterval = Duration.ofMillis(Long.parseLong(settingsFile.getValue("/retry/pollingInterval").toString()));
1416
}
1517

1618
@Override
@@ -19,7 +21,7 @@ public int getNumber() {
1921
}
2022

2123
@Override
22-
public long getPollingInterval() {
24+
public Duration getPollingInterval() {
2325
return pollingInterval;
2426
}
2527
}

src/main/java/aquality/selenium/core/configurations/TimeoutConfiguration.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,46 @@
33
import aquality.selenium.core.utilities.ISettingsFile;
44
import com.google.inject.Inject;
55

6-
public class TimeoutConfiguration implements ITimeoutConfiguration{
6+
import java.time.Duration;
7+
8+
public class TimeoutConfiguration implements ITimeoutConfiguration {
79

810
private final ISettingsFile settingsFile;
9-
private final long condition;
10-
private final long pollInterval;
11-
private final long implicit;
12-
private final long command;
11+
private final Duration condition;
12+
private final Duration pollInterval;
13+
private final Duration implicit;
14+
private final Duration command;
1315

1416
@Inject
1517
public TimeoutConfiguration(ISettingsFile settingsFile) {
1618
this.settingsFile = settingsFile;
17-
condition = getTimeout(TIMEOUT.CONDITION);
18-
pollInterval = getTimeout(TIMEOUT.POLL_INTERVAL);
19-
implicit = getTimeout(TIMEOUT.IMPLICIT);
20-
command = getTimeout(TIMEOUT.COMMAND);
19+
condition = getDurationFromSeconds(TIMEOUT.CONDITION);
20+
pollInterval = Duration.ofMillis(getTimeout(TIMEOUT.POLL_INTERVAL));
21+
implicit = getDurationFromSeconds(TIMEOUT.IMPLICIT);
22+
command = getDurationFromSeconds(TIMEOUT.COMMAND);
2123
}
2224

23-
private long getTimeout(TIMEOUT timeout){
25+
private long getTimeout(TIMEOUT timeout) {
2426
return Long.valueOf(settingsFile.getValue(timeout.getKey()).toString());
2527
}
2628

27-
public long getImplicit(){
29+
private Duration getDurationFromSeconds(TIMEOUT timeout) {
30+
return Duration.ofSeconds(getTimeout(timeout));
31+
}
32+
33+
public Duration getImplicit() {
2834
return implicit;
2935
}
3036

31-
public long getCondition(){
37+
public Duration getCondition() {
3238
return condition;
3339
}
3440

35-
public long getPollingInterval(){
41+
public Duration getPollingInterval() {
3642
return pollInterval;
3743
}
3844

39-
public long getCommand(){
45+
public Duration getCommand() {
4046
return command;
4147
}
4248

@@ -47,11 +53,12 @@ private enum TIMEOUT {
4753
COMMAND("/timeouts/timeoutCommand");
4854

4955
private String key;
50-
TIMEOUT(String key){
56+
57+
TIMEOUT(String key) {
5158
this.key = key;
5259
}
5360

54-
private String getKey(){
61+
private String getKey() {
5562
return key;
5663
}
5764
}

src/main/java/aquality/selenium/core/elements/CachedElementStateProvider.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.openqa.selenium.StaleElementReferenceException;
99
import org.openqa.selenium.WebElement;
1010

11+
import java.time.Duration;
1112
import java.util.Arrays;
1213
import java.util.Collections;
1314
import java.util.List;
@@ -42,7 +43,7 @@ protected boolean tryInvokeFunction(Predicate<WebElement> predicate) {
4243

4344
protected boolean tryInvokeFunction(Predicate<WebElement> predicate, List<Class<? extends Exception>> handledExceptions) {
4445
try {
45-
return predicate.test(elementCacheHandler.getElement(getZeroTImeout(), ElementState.EXISTS_IN_ANY_STATE));
46+
return predicate.test(elementCacheHandler.getElement(Duration.ZERO, ElementState.EXISTS_IN_ANY_STATE));
4647
} catch (Exception exception) {
4748
if (handledExceptions.contains(exception.getClass())) {
4849
return false;
@@ -51,10 +52,10 @@ protected boolean tryInvokeFunction(Predicate<WebElement> predicate, List<Class<
5152
}
5253
}
5354

54-
protected boolean waitForCondition(BooleanSupplier condition, String conditionName, Long timeout) {
55-
boolean result = conditionalWait.waitFor(condition, timeout, null);
55+
protected boolean waitForCondition(BooleanSupplier condition, String conditionName, Duration timeout) {
56+
boolean result = conditionalWait.waitFor(condition, timeout);
5657
if (!result) {
57-
String timeoutString = timeout == null ? "" : String.format("%1$s s.", timeout);
58+
String timeoutString = timeout == null ? "" : String.format("%1$s s.", timeout.getSeconds());
5859
localizedLogger.warn("loc.element.not.in.state", locator, conditionName.toUpperCase(), timeoutString);
5960
}
6061
return result;
@@ -66,7 +67,7 @@ public boolean isClickable() {
6667
}
6768

6869
@Override
69-
public void waitForClickable(Long timeout) {
70+
public void waitForClickable(Duration timeout) {
7071
String errorMessage = String.format("Element %1$s has not become clickable after timeout.", locator);
7172
try {
7273
conditionalWait.waitForTrue(this::isClickable, timeout, null, errorMessage);
@@ -82,12 +83,12 @@ public boolean isDisplayed() {
8283
}
8384

8485
@Override
85-
public boolean waitForDisplayed(Long timeout) {
86+
public boolean waitForDisplayed(Duration timeout) {
8687
return waitForCondition(() -> tryInvokeFunction(WebElement::isDisplayed), ElementState.DISPLAYED.toString(), timeout);
8788
}
8889

8990
@Override
90-
public boolean waitForNotDisplayed(Long timeout) {
91+
public boolean waitForNotDisplayed(Duration timeout) {
9192
return waitForCondition(() -> !isDisplayed(), "invisible or absent", timeout);
9293
}
9394

@@ -97,12 +98,12 @@ public boolean isExist() {
9798
}
9899

99100
@Override
100-
public boolean waitForExist(Long timeout) {
101+
public boolean waitForExist(Duration timeout) {
101102
return waitForCondition(() -> tryInvokeFunction(element -> true), ElementState.EXISTS_IN_ANY_STATE.toString(), timeout);
102103
}
103104

104105
@Override
105-
public boolean waitForNotExist(Long timeout) {
106+
public boolean waitForNotExist(Duration timeout) {
106107
return waitForCondition(() -> !isExist(), "absent", timeout);
107108
}
108109

@@ -112,12 +113,12 @@ public boolean isEnabled() {
112113
}
113114

114115
@Override
115-
public boolean waitForEnabled(Long timeout) {
116+
public boolean waitForEnabled(Duration timeout) {
116117
return waitForCondition(this::isEnabled, elementEnabled().getStateName(), timeout);
117118
}
118119

119120
@Override
120-
public boolean waitForNotEnabled(Long timeout) {
121+
public boolean waitForNotEnabled(Duration timeout) {
121122
return waitForCondition(() -> !isEnabled(), elementNotEnabled().getStateName(), timeout);
122123
}
123124
}

src/main/java/aquality/selenium/core/elements/DefaultElementStateProvider.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import aquality.selenium.core.waitings.IConditionalWait;
55
import org.openqa.selenium.By;
66

7+
import java.time.Duration;
8+
79
public class DefaultElementStateProvider extends ElementStateProvider {
810

911
private final By locator;
@@ -18,70 +20,70 @@ public DefaultElementStateProvider(By locator, IConditionalWait conditionalWait,
1820

1921
@Override
2022
public boolean isClickable() {
21-
return waitForIsClickable(getZeroTImeout(), true);
23+
return waitForIsClickable(Duration.ZERO, true);
2224
}
2325

2426
@Override
25-
public void waitForClickable(Long timeout) {
27+
public void waitForClickable(Duration timeout) {
2628
waitForIsClickable(timeout, false);
2729
}
2830

29-
private boolean waitForIsClickable(Long timeout, boolean catchTimeoutException) {
31+
private boolean waitForIsClickable(Duration timeout, boolean catchTimeoutException) {
3032
DesiredState desiredState = elementClickable();
3133
desiredState = catchTimeoutException ? desiredState.withCatchingTimeoutException() : desiredState;
3234
return isElementInDesiredCondition(desiredState, timeout);
3335
}
3436

35-
private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Long timeout) {
37+
private boolean isElementInDesiredCondition(DesiredState elementStateCondition, Duration timeout) {
3638
return !elementFinder.findElements(locator, elementStateCondition, timeout).isEmpty();
3739
}
3840

3941
@Override
4042
public boolean isDisplayed() {
41-
return waitForDisplayed(getZeroTImeout());
43+
return waitForDisplayed(Duration.ZERO);
4244
}
4345

4446
@Override
45-
public boolean waitForDisplayed(Long timeout) {
47+
public boolean waitForDisplayed(Duration timeout) {
4648
return isAnyElementFound(timeout, ElementState.DISPLAYED);
4749
}
4850

49-
private boolean isAnyElementFound(Long timeout, ElementState state) {
51+
private boolean isAnyElementFound(Duration timeout, ElementState state) {
5052
return !elementFinder.findElements(locator, state, timeout).isEmpty();
5153
}
5254

5355
@Override
54-
public boolean waitForNotDisplayed(Long timeout) {
55-
return conditionalWait.waitFor(() -> !isDisplayed(), timeout, null);
56+
public boolean waitForNotDisplayed(Duration timeout) {
57+
return conditionalWait.waitFor(() -> !isDisplayed(), timeout);
5658
}
5759

5860
@Override
5961
public boolean isExist() {
60-
return waitForExist(getZeroTImeout());
62+
return waitForExist(Duration.ZERO);
6163
}
6264

6365
@Override
64-
public boolean waitForExist(Long timeout) {
66+
public boolean waitForExist(Duration timeout) {
6567
return isAnyElementFound(timeout, ElementState.EXISTS_IN_ANY_STATE);
6668
}
6769

6870
@Override
69-
public boolean waitForNotExist(Long timeout) {
70-
return conditionalWait.waitFor(() -> !isExist(), timeout, null);
71+
public boolean waitForNotExist(Duration timeout) {
72+
return conditionalWait.waitFor(() -> !isExist(), timeout);
7173
}
7274

7375
@Override
7476
public boolean isEnabled() {
75-
return waitForEnabled(getZeroTImeout());
77+
return waitForEnabled(Duration.ZERO);
7678
}
7779

7880
@Override
79-
public boolean waitForEnabled(Long timeout) {
81+
public boolean waitForEnabled(Duration timeout) {
8082
return isElementInDesiredCondition(elementEnabled(), timeout);
8183
}
8284

8385
@Override
84-
public boolean waitForNotEnabled(Long timeout) {
86+
public boolean waitForNotEnabled(Duration timeout) {
8587
return isElementInDesiredCondition(elementNotEnabled(), timeout);
8688
}
8789
}

src/main/java/aquality/selenium/core/elements/Element.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.openqa.selenium.WebDriverException;
1313
import org.openqa.selenium.remote.RemoteWebElement;
1414

15+
import java.time.Duration;
1516
import java.util.function.Supplier;
1617

1718
public abstract class Element implements IElement {
@@ -73,7 +74,7 @@ public IElementStateProvider state() {
7374
}
7475

7576
@Override
76-
public RemoteWebElement getElement(Long timeout) {
77+
public RemoteWebElement getElement(Duration timeout) {
7778
try {
7879
return getElementCacheConfiguration().isEnabled()
7980
? getCache().getElement(timeout)

src/main/java/aquality/selenium/core/elements/ElementCacheHandler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.openqa.selenium.By;
66
import org.openqa.selenium.remote.RemoteWebElement;
77

8+
import java.time.Duration;
9+
810
/**
911
* Implementation of {@link IElementCacheHandler}.
1012
*/
@@ -44,7 +46,7 @@ public boolean wasCached() {
4446
}
4547

4648
@Override
47-
public RemoteWebElement getElement(Long timeout, ElementState customState) {
49+
public RemoteWebElement getElement(Duration timeout, ElementState customState) {
4850
ElementState requiredState = customState == null ? state : customState;
4951
if (isRefreshNeeded(requiredState)) {
5052
remoteElement = (RemoteWebElement) finder.findElement(locator, requiredState, timeout);

0 commit comments

Comments
 (0)