Skip to content

Commit 9e066cb

Browse files
authored
Bug/43 cast json value to double (#44)
* #43 add cast to double in JsonSettingsFile rename ElementActionRetrier to ActionRetrier * #43 update version of lib * #43 add deprecated ElementActionRetrier * #43 revert ElementActionRetrier split with ActionRetrier * #43 add tests for ActionRetrier fix comments for methods * #43 add tests to suite * #43 add isExceptionHandled method update version to 1.0.0
1 parent 0fe7e5b commit 9e066cb

18 files changed

+389
-89
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.github.aquality-automation</groupId>
77
<artifactId>aquality-selenium-core</artifactId>
8-
<version>0.0.3</version>
8+
<version>1.0.0</version>
99
<packaging>jar</packaging>
1010
<name>Aquality Selenium Core</name>
1111
<description>Library with core functions simplifying work with Selenium-controlled applications.</description>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import aquality.selenium.core.localization.ILocalizationModule;
99
import aquality.selenium.core.localization.ILocalizedLogger;
1010
import aquality.selenium.core.logging.Logger;
11+
import aquality.selenium.core.utilities.IActionRetrier;
1112
import aquality.selenium.core.utilities.IElementActionRetrier;
1213
import aquality.selenium.core.utilities.ISettingsFile;
1314
import aquality.selenium.core.utilities.IUtilitiesModule;
@@ -42,6 +43,7 @@ protected void configure() {
4243
bind(IRetryConfiguration.class).to(getRetryConfigurationImplementation()).in(Singleton.class);
4344
bind(IElementCacheConfiguration.class).to(getElementCacheConfigurationImplementation()).in(Singleton.class);
4445
bind(IElementActionRetrier.class).to(getElementActionRetrierImplementation()).in(Singleton.class);
46+
bind(IActionRetrier.class).to(getActionRetrierImplementation()).in(Singleton.class);
4547
bind(ILocalizationManager.class).to(getLocalizationManagerImplementation()).in(Singleton.class);
4648
bind(ILocalizedLogger.class).to(getLocalizedLoggerImplementation()).in(Singleton.class);
4749
bind(IConditionalWait.class).to(getConditionalWaitImplementation());
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package aquality.selenium.core.utilities;
2+
3+
import aquality.selenium.core.configurations.IRetryConfiguration;
4+
import com.google.inject.Inject;
5+
6+
import java.util.Collection;
7+
import java.util.Optional;
8+
import java.util.function.Supplier;
9+
10+
public class ActionRetrier implements IActionRetrier {
11+
12+
private IRetryConfiguration retryConfiguration;
13+
14+
@Inject
15+
public ActionRetrier(IRetryConfiguration retryConfiguration) {
16+
this.retryConfiguration = retryConfiguration;
17+
}
18+
19+
@Override
20+
public void doWithRetry(Runnable runnable, Collection<Class<? extends Throwable>> handledExceptions) {
21+
Supplier<?> supplier = () -> {
22+
runnable.run();
23+
return true;
24+
};
25+
doWithRetry(supplier, handledExceptions);
26+
}
27+
28+
@Override
29+
public <T> T doWithRetry(Supplier<T> function, Collection<Class<? extends Throwable>> handledExceptions) {
30+
int retryAttemptsLeft = retryConfiguration.getNumber();
31+
Optional<T> result = Optional.empty();
32+
while (retryAttemptsLeft >= 0) {
33+
try {
34+
result = Optional.of(function.get());
35+
break;
36+
} catch (Exception exception) {
37+
if (isExceptionHandled(handledExceptions, exception) && retryAttemptsLeft != 0) {
38+
try {
39+
Thread.sleep(retryConfiguration.getPollingInterval().toMillis());
40+
} catch (InterruptedException e) {
41+
Thread.currentThread().interrupt();
42+
}
43+
retryAttemptsLeft--;
44+
} else {
45+
throw exception;
46+
}
47+
}
48+
}
49+
return result.orElse(null);
50+
}
51+
52+
protected boolean isExceptionHandled(Collection<Class<? extends Throwable>> handledExceptions, Exception exception) {
53+
return handledExceptions.contains(exception.getClass());
54+
}
55+
}

src/main/java/aquality/selenium/core/utilities/ElementActionRetrier.java

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,22 @@
33
import aquality.selenium.core.configurations.IRetryConfiguration;
44
import com.google.inject.Inject;
55

6-
import java.util.Optional;
76
import java.util.function.Supplier;
87

9-
public class ElementActionRetrier implements IElementActionRetrier {
10-
11-
private IRetryConfiguration retryConfiguration;
8+
public class ElementActionRetrier extends ActionRetrier implements IElementActionRetrier {
129

1310
@Inject
1411
public ElementActionRetrier(IRetryConfiguration retryConfiguration) {
15-
this.retryConfiguration = retryConfiguration;
12+
super(retryConfiguration);
1613
}
1714

1815
@Override
1916
public void doWithRetry(Runnable runnable) {
20-
Supplier<?> supplier = () -> {
21-
runnable.run();
22-
return true;
23-
};
24-
doWithRetry(supplier);
17+
doWithRetry(runnable, getHandledExceptions());
2518
}
2619

2720
@Override
2821
public <T> T doWithRetry(Supplier<T> function) {
29-
int retryAttemptsLeft = retryConfiguration.getNumber();
30-
Optional<T> result = Optional.empty();
31-
while (retryAttemptsLeft >= 0) {
32-
try {
33-
result = Optional.of(function.get());
34-
break;
35-
} catch (Exception exception) {
36-
if (isExceptionHandled(exception) && retryAttemptsLeft != 0) {
37-
try {
38-
Thread.sleep(retryConfiguration.getPollingInterval().toMillis());
39-
} catch (InterruptedException e) {
40-
Thread.currentThread().interrupt();
41-
}
42-
retryAttemptsLeft--;
43-
} else {
44-
throw exception;
45-
}
46-
}
47-
}
48-
return result.orElse(null);
49-
}
50-
51-
protected boolean isExceptionHandled(Exception exception) {
52-
return getHandledExceptions().contains(exception.getClass());
22+
return doWithRetry(function, getHandledExceptions());
5323
}
5424
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package aquality.selenium.core.utilities;
2+
3+
import java.util.Collection;
4+
import java.util.function.Supplier;
5+
6+
/**
7+
* Retries an action or function when handledExceptions occurs.
8+
*/
9+
public interface IActionRetrier {
10+
11+
/**
12+
* Retries the action when the handled exception occurs.
13+
* @param runnable Action to be applied.
14+
* @param handledExceptions Exceptions to be handled.
15+
*/
16+
void doWithRetry(Runnable runnable, Collection<Class<? extends Throwable>> handledExceptions);
17+
18+
/**
19+
* Retries the function when the handled exception occurs.
20+
* @param function Function to be applied.
21+
* @param handledExceptions Exceptions to be handled.
22+
* @param <T> Return type of function.
23+
* @return Result of the function.
24+
*/
25+
<T> T doWithRetry(Supplier<T> function, Collection<Class<? extends Throwable>> handledExceptions);
26+
}

src/main/java/aquality/selenium/core/utilities/IElementActionRetrier.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
/**
1111
* Retries an action or function when {@link #getHandledExceptions()} occurs.
1212
*/
13-
public interface IElementActionRetrier {
13+
public interface IElementActionRetrier extends IActionRetrier {
14+
1415

1516
/**
1617
* Retries the action when the handled exception {@link #getHandledExceptions()} occurs.
@@ -30,7 +31,7 @@ public interface IElementActionRetrier {
3031
* Exceptions to be ignored during action retrying.
3132
* @return By the default implementation, {@link StaleElementReferenceException} and {@link InvalidElementStateException} are handled.
3233
*/
33-
default List<Class<? extends Exception>> getHandledExceptions() {
34+
default List<Class<? extends Throwable>> getHandledExceptions() {
3435
return Arrays.asList(StaleElementReferenceException.class, InvalidElementStateException.class);
3536
}
3637
}

src/main/java/aquality/selenium/core/utilities/IUtilitiesModule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
*/
66
public interface IUtilitiesModule {
77

8+
/**
9+
* @return implementation of {@link IActionRetrier}.
10+
*/
11+
default Class<? extends IActionRetrier> getActionRetrierImplementation() {
12+
return ActionRetrier.class;
13+
}
14+
815
/**
916
* @return implementation of {@link IElementActionRetrier}.
1017
*/

src/main/java/aquality/selenium/core/utilities/JsonSettingsFile.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ private Object castEnvOrDefaulValue(JsonNode node, String envVar) {
5353
return envVar == null ? node.asLong() : Long.parseLong(envVar);
5454
} else if (node.isInt()) {
5555
return envVar == null ? node.asInt() : Integer.parseInt(envVar);
56+
} else if (node.isDouble()) {
57+
return envVar == null ? node.asDouble() : Double.parseDouble(envVar);
5658
} else {
5759
return envVar == null ? node.asText() : envVar;
5860
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tests.applications;
2+
3+
import aquality.selenium.core.utilities.ISettingsFile;
4+
import com.google.inject.Inject;
5+
6+
public class CustomConfiguration implements ICustomConfiguration {
7+
private final ISettingsFile settingsFile;
8+
9+
@Inject
10+
public CustomConfiguration(ISettingsFile settingsFile){
11+
this.settingsFile = settingsFile;
12+
}
13+
14+
public double getDoubleValue() {
15+
return (double) settingsFile.getValue("/custom/doubleValue");
16+
}
17+
18+
public String getStringValue() {
19+
return settingsFile.getValue("/custom/stringValue").toString();
20+
}
21+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package tests.applications;
2+
3+
public interface ICustomConfiguration {
4+
5+
double getDoubleValue();
6+
7+
String getStringValue();
8+
}

0 commit comments

Comments
 (0)