-
Notifications
You must be signed in to change notification settings - Fork 4
Extracted IConditionalWait #25
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
52 commits
Select commit
Hold shift + click to select a range
1278a63
extracted ISettingsFile and added tests
knysh 3cd01b3
Merge branch 'master' into Feature/10-implement-SettingsFile
knysh 1c72731
added documentation for ISettingsFile
knysh 1f0602a
extracted ILoggerConfiguration
knysh aee9151
extracted ITimeoutConfiguration
knysh 3ede916
extracted IRetryConfiguration
knysh 4e2b14f
renamed one test
knysh d4ec648
added empty end line and
knysh 5dd1015
renamed constants to upper case
knysh b7c5b5f
removed redundant method from ISettingsFile
knysh 0e62ec6
Merge remote-tracking branch 'origin/Feature/10-implement-SettingsFil…
knysh 890314e
fixed naming and added empty line at the end of files
knysh 5b47103
Merge branch 'Feature/11-LoggerConfiguration' into Feature/11-Timeout…
knysh 5ad2519
fixed naming and added empty line at the end of files
knysh fc6cfdb
Merge branch 'Feature/11-TimeoutConfiguration' into Feature/11-IRetry…
knysh b2f48c8
fixed naming and added empty line at the end of files
knysh 88b025a
Merge branch 'master' into Feature/10-implement-SettingsFile
knysh 4619ac0
merged with master and added test for CustomSettingsFile
knysh ccac733
fix for the tests
knysh 1abd298
Merge branch 'Feature/10-implement-SettingsFile' into Feature/11-Logg…
knysh d58605f
merge and small refactoring of tests
knysh 4983e88
Merge branch 'Feature/11-LoggerConfiguration' into Feature/11-Timeout…
knysh 7513559
merge from master
knysh cd4d3ed
Merge branch 'Feature/11-TimeoutConfiguration' into Feature/11-IRetry…
knysh a968e5c
merge from master
knysh 792cda2
extracted IElementCacheConfiguration
knysh adb00be
extracted IConditionalWait
knysh bd4b4fa
Merge branch 'master' into Feature/11-IRetryConfiguration
knysh a9ee1f3
merge and test fix
knysh 05b05b5
Merge branch 'Feature/11-IRetryConfiguration' into Feature/6-Conditio…
knysh 8545c56
application to applications
knysh 6266fd3
removed SupportedLanguaged and added default language if configuratio…
knysh 6093c76
support multiple threads in ConditionalWaitTests
knysh ee3383c
register configuration as singleton
knysh 4244600
fixed comments
knysh 327a682
moved SettingsFileUtil to ISettingsFile
knysh 70c1b01
added IWaitingsModule
knysh a88af11
small fix in summary
knysh 454cf84
Merge branch 'Feature/11-IRetryConfiguration' into Feature/6-Conditio…
knysh 60f56a4
merge
knysh 7d36c5c
Merge branch 'master' into Feature/6-ConditionalWait
knysh 84365cf
update doc
knysh 0b3e61b
rename applications to application in docs
knysh c4d26aa
#6 removed using of DI in tests
knysh 4803910
Merge branch 'master' into Feature/6-ConditionalWait
knysh 9e0d607
Merge branch 'master' into Feature/6-ConditionalWait
knysh 7afcfa0
Merge branch 'master' into Feature/6-ConditionalWait
knysh 7452a3e
small fix
knysh ec3d273
Merge branch 'master' into Feature/6-ConditionalWait
knysh 991d152
merge with master
knysh dffe9ec
optimized import
knysh 56b874b
fixed tests
knysh 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
2 changes: 1 addition & 1 deletion
2
...um/core/application/AqualityServices.java → ...m/core/applications/AqualityServices.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
2 changes: 1 addition & 1 deletion
2
...lenium/core/application/IApplication.java → ...enium/core/applications/IApplication.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
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
112 changes: 112 additions & 0 deletions
112
src/main/java/aquality/selenium/core/waitings/ConditionalWait.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,112 @@ | ||
package aquality.selenium.core.waitings; | ||
|
||
import aquality.selenium.core.applications.IApplication; | ||
import aquality.selenium.core.configurations.ITimeoutConfiguration; | ||
import com.google.common.base.Strings; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
import org.openqa.selenium.StaleElementReferenceException; | ||
import org.openqa.selenium.support.ui.ExpectedCondition; | ||
import org.openqa.selenium.support.ui.WebDriverWait; | ||
|
||
import java.time.Duration; | ||
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; | ||
|
||
public class ConditionalWait implements IConditionalWait { | ||
|
||
private final Provider<IApplication> applicationProvider; | ||
private final ITimeoutConfiguration timeoutConfiguration; | ||
|
||
@Inject | ||
public ConditionalWait(Provider<IApplication> applicationProvider, ITimeoutConfiguration timeoutConfiguration) { | ||
this.applicationProvider = applicationProvider; | ||
this.timeoutConfiguration = timeoutConfiguration; | ||
} | ||
|
||
@Override | ||
public boolean waitFor(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) { | ||
try { | ||
waitForTrue(condition, timeoutInSeconds, pollingIntervalInMilliseconds, message, exceptionsToIgnore); | ||
return true; | ||
} catch (TimeoutException e) { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public void waitForTrue(BooleanSupplier condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) throws TimeoutException { | ||
BooleanSupplier supplier = Optional.ofNullable(condition).orElseThrow(() -> new IllegalArgumentException("Condition cannot be null")); | ||
Long timeout = resolveConditionTimeout(timeoutInSeconds); | ||
Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); | ||
String exMessage = resolveMessage(message); | ||
double startTime = getCurrentTime(); | ||
while (true) { | ||
if (isConditionSatisfied(supplier, exceptionsToIgnore)) { | ||
return; | ||
} | ||
|
||
double currentTime = getCurrentTime(); | ||
if ((currentTime - startTime) > timeout) { | ||
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); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public <T> T waitFor(ExpectedCondition<T> condition, Long timeoutInSeconds, Long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) { | ||
IApplication app = applicationProvider.get(); | ||
app.setImplicitWaitTimeout(0, TimeUnit.SECONDS); | ||
Long timeout = resolveConditionTimeout(timeoutInSeconds); | ||
Long pollingInterval = resolvePollingInterval(pollingIntervalInMilliseconds); | ||
String exMessage = resolveMessage(message); | ||
WebDriverWait wait = new WebDriverWait(app.getDriver(), timeout); | ||
wait.pollingEvery(Duration.ofMillis(pollingInterval)); | ||
wait.withMessage(exMessage); | ||
wait.ignoreAll(exceptionsToIgnore == null ? Collections.singleton(StaleElementReferenceException.class) : exceptionsToIgnore); | ||
try { | ||
return wait.until(condition); | ||
} finally { | ||
app.setImplicitWaitTimeout(timeoutConfiguration.getImplicit(), TimeUnit.SECONDS); | ||
} | ||
} | ||
|
||
private double getCurrentTime() { | ||
return System.nanoTime() / Math.pow(10, 9); | ||
} | ||
|
||
private boolean isConditionSatisfied(BooleanSupplier condition, Collection<Class<? extends Throwable>> exceptionsToIgnore) { | ||
try { | ||
return condition.getAsBoolean(); | ||
} catch (Exception e) { | ||
if (exceptionsToIgnore == null || !exceptionsToIgnore.contains(e.getClass())) { | ||
throw e; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
private Long resolveConditionTimeout(Long timeout) { | ||
return Optional.ofNullable(timeout).orElse(timeoutConfiguration.getCommand()); | ||
} | ||
|
||
private Long resolvePollingInterval(Long timeout) { | ||
return Optional.ofNullable(timeout).orElse(timeoutConfiguration.getPollingInterval()); | ||
} | ||
|
||
private String resolveMessage(String message) { | ||
return Strings.isNullOrEmpty(message) ? "" : message; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please, refactor this as follow:
a. should return void
b. should not catch an exception
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored