-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/9 element action retrier #26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/aquality/selenium/core/utilities/ElementActionRetrier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package aquality.selenium.core.utilities; | ||
|
||
import aquality.selenium.core.configurations.IRetryConfiguration; | ||
import com.google.inject.Inject; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
public class ElementActionRetrier implements IElementActionRetrier { | ||
|
||
private IRetryConfiguration retryConfiguration; | ||
|
||
@Inject | ||
public ElementActionRetrier(IRetryConfiguration retryConfiguration) { | ||
this.retryConfiguration = retryConfiguration; | ||
} | ||
|
||
@Override | ||
public void doWithRetry(Runnable runnable) { | ||
Supplier<?> supplier = () -> { | ||
runnable.run(); | ||
return true; | ||
}; | ||
doWithRetry(supplier); | ||
} | ||
|
||
@Override | ||
public <T> T doWithRetry(Supplier<T> function) { | ||
int retryAttemptsLeft = retryConfiguration.getNumber(); | ||
Optional<T> result = Optional.empty(); | ||
while (retryAttemptsLeft >= 0) { | ||
try { | ||
result = Optional.of(function.get()); | ||
break; | ||
} catch (Exception exception) { | ||
if (isExceptionHandled(exception) && retryAttemptsLeft != 0) { | ||
try { | ||
Thread.sleep(retryConfiguration.getPollingInterval()); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
retryAttemptsLeft--; | ||
} else { | ||
throw exception; | ||
} | ||
} | ||
} | ||
return result.orElse(null); | ||
} | ||
|
||
protected boolean isExceptionHandled(Exception exception) { | ||
return getHandledExceptions().contains(exception.getClass()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/aquality/selenium/core/utilities/IElementActionRetrier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package aquality.selenium.core.utilities; | ||
|
||
import org.openqa.selenium.InvalidElementStateException; | ||
import org.openqa.selenium.StaleElementReferenceException; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Retries an action or function when {@link #getHandledExceptions()} occurs. | ||
*/ | ||
public interface IElementActionRetrier { | ||
|
||
/** | ||
* Retries the action when the handled exception {@link #getHandledExceptions()} occurs. | ||
* @param runnable Action to be applied. | ||
*/ | ||
void doWithRetry(Runnable runnable); | ||
|
||
/** | ||
* Retries the function when the handled exception {@link #getHandledExceptions()} occurs. | ||
* @param function Function to be applied. | ||
* @param <T> Return type of function. | ||
* @return Result of the function. | ||
*/ | ||
<T> T doWithRetry(Supplier<T> function); | ||
|
||
/** | ||
* Exceptions to be ignored during action retrying. | ||
* @return By the default implementation, {@link StaleElementReferenceException} and {@link InvalidElementStateException} are handled. | ||
*/ | ||
default List<Class<? extends Exception>> getHandledExceptions() { | ||
return Arrays.asList(StaleElementReferenceException.class, InvalidElementStateException.class); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/aquality/selenium/core/utilities/IUtilitiesModule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package aquality.selenium.core.utilities; | ||
|
||
/** | ||
* Provides implementations for utilities module. | ||
*/ | ||
public interface IUtilitiesModule { | ||
|
||
/** | ||
* @return implementation of {@link IElementActionRetrier}. | ||
*/ | ||
default Class<? extends IElementActionRetrier> getElementActionRetrierImplementation() { | ||
knysh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ElementActionRetrier.class; | ||
} | ||
|
||
/** | ||
* Provides default {@link ISettingsFile} with settings. | ||
* Default value is settings.json. | ||
* You are able to override this path, by setting environment variable 'profile'. | ||
* In this case, settings file will be settings.{profile}.json. | ||
* | ||
* @return An instance of settings. | ||
*/ | ||
default ISettingsFile getInstanceOfSettingsFile() { | ||
String settingsProfile = System.getProperty("profile") == null ? "settings.json" : "settings." + System.getProperty("profile") + ".json"; | ||
return new JsonSettingsFile(settingsProfile); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/test/java/tests/configurations/ConfigurationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package tests.configurations; | ||
|
||
import aquality.selenium.core.configurations.*; | ||
import aquality.selenium.core.utilities.ISettingsFile; | ||
import org.testng.annotations.Test; | ||
import tests.application.CustomAqualityServices; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
|
||
public class ConfigurationTests { | ||
private static final ISettingsFile settingsFile = CustomAqualityServices.getServiceProvider().getInstance(ISettingsFile.class); | ||
|
||
@Test | ||
public void testShouldBePossibleCheckIsEnableElementCache() { | ||
boolean isEnable = new ElementCacheConfiguration(settingsFile).isEnabled(); | ||
assertFalse(isEnable, "Element cache is disabled by default"); | ||
} | ||
|
||
@Test | ||
public void testShouldBePossibleToGetLanguage() { | ||
String language = new LoggerConfiguration(settingsFile).getLanguage(); | ||
assertEquals(language, "en", "Current language should be got from logger configuration"); | ||
} | ||
|
||
@Test | ||
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"); | ||
} | ||
|
||
@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"); | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
src/test/java/tests/configurations/ElementCacheConfigurationTests.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/test/java/tests/configurations/LoggerConfigurationTests.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
src/test/java/tests/configurations/RetryConfigurationTests.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/test/java/tests/configurations/TimeoutConfigurationTests.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.