- Notifications
You must be signed in to change notification settings - Fork 35
Bring conditionalwait back #28 #41
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
15 commits Select commit Hold shift + click to select a range
7125a1a refactored state of elements
0c16676 re-check waitForNotExists methods for correct work. split 1 test to 2…
b0567a6 removed IElementWithState interface
9fc21fd refactored conditional wait
59f2641 merged with the master branch
6e6fe54 Merge branch 'master' of https://github.com/aquality-automation/aqual…
f1ca748 Merge branch 'master' of https://github.com/aquality-automation/aqual…
7f03255 Merge branch 'master' of https://github.com/aquality-automation/aqual…
1dc49f6 Merge branch 'master' of https://github.com/aquality-automation/aqual…
05d1e6f Merge branch 'master' of https://github.com/aquality-automation/aqual…
b9a2d26 added and updated ConditionalWait
c33607e refactored ConditionalWaitTests
Nikikuzi 322bff5 refactored conditionalwait tests - added Timer class to simplify meas…
0f58d54 removed unused variable
7b46ab4 made changes related with comments to PR
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
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
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
138 changes: 138 additions & 0 deletions 138 src/main/java/aquality/selenium/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,138 @@ | ||
| package aquality.selenium.waitings; | ||
| | ||
| import aquality.selenium.browser.Browser; | ||
| import aquality.selenium.browser.BrowserManager; | ||
| import aquality.selenium.configuration.Configuration; | ||
| import aquality.selenium.configuration.ITimeoutConfiguration; | ||
| import aquality.selenium.localization.LocalizationManager; | ||
| 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.concurrent.TimeoutException; | ||
| import java.util.function.BooleanSupplier; | ||
| import java.util.function.Function; | ||
| | ||
| public final class ConditionalWait { | ||
| | ||
| private ConditionalWait() { | ||
| throw new IllegalStateException("All methods are static in this 'ConditionalWait' class, class instance is not required"); | ||
| } | ||
| | ||
| /** | ||
| * Wait for some condition within timeout. Method does not use WebDriverWait | ||
| * Default values for timeouts used from configuration settings file | ||
| * @param condition condition with boolean result (predicate) | ||
| * @param message Part of error message in case of Timeout exception | ||
| * @return true if the condition has been met during the timeout | ||
| */ | ||
| public static boolean waitForTrue(BooleanSupplier condition, String message) | ||
| { | ||
| try | ||
| { | ||
| waitForTrue(condition, getTimeoutConfiguration().getCondition(), getTimeoutConfiguration().getPollingInterval(), message); | ||
| return true; | ||
| } | ||
| catch (TimeoutException e) { | ||
| return false; | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * 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 | ||
| * @throws TimeoutException will be thrown in case if timeout is over but condition was not met | ||
| */ | ||
| public static void waitForTrue(BooleanSupplier condition, long timeoutInSeconds, long pollingIntervalInMilliseconds, String message) throws TimeoutException { | ||
| if (condition == null) | ||
| { | ||
| throw new IllegalArgumentException(getLocalizationManager().getValue("loc.wait.condition.cant.be.null")); | ||
| } | ||
| | ||
| double startTime = getCurrentTime(); | ||
| while (true) | ||
| { | ||
| if (condition.getAsBoolean()) | ||
| { | ||
| return; | ||
| } | ||
| | ||
| double currentTime = getCurrentTime(); | ||
| if ((currentTime - startTime) > timeoutInSeconds) | ||
| { | ||
| String exceptionMessage = String.format(getLocalizationManager().getValue("loc.wait.timeout.condition"), timeoutInSeconds, message); | ||
| throw new TimeoutException(exceptionMessage); | ||
| } | ||
| | ||
| try { | ||
| Thread.sleep(pollingIntervalInMilliseconds); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
| } | ||
| | ||
| /** | ||
| * Waits for function will be true or return some except false. | ||
| * Default timeout condition from settings is using. | ||
| * StaleElementReferenceException will be handled by default | ||
| * @param condition Function for waiting {@link Function} | ||
| * @param message the message that will be added to an error in case if the condition is not matched during the timeout | ||
| * @param <T> Type of object which is waiting | ||
| * @return Object which waiting for or null - is exceptions occurred | ||
| */ | ||
| public static <T> T waitFor(ExpectedCondition<T> condition, String message) { | ||
| return waitFor(condition, | ||
| getTimeoutConfiguration().getCondition(), | ||
| getTimeoutConfiguration().getPollingInterval(), | ||
| message, | ||
| Collections.singleton(StaleElementReferenceException.class)); | ||
| } | ||
| | ||
| /** | ||
| * 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 <T> Type of object which is waiting | ||
| * @return Object which waiting for or null - is exceptions occured | ||
| */ | ||
| public static <T> T waitFor(ExpectedCondition<T> condition, long timeOutInSeconds, long pollingIntervalInMilliseconds, String message, Collection<Class<? extends Throwable>> exceptionsToIgnore) { | ||
| getBrowser().setImplicitWaitTimeout(0L); | ||
| WebDriverWait wait = new WebDriverWait(getBrowser().getDriver(), timeOutInSeconds); | ||
| wait.pollingEvery(Duration.ofMillis(pollingIntervalInMilliseconds)); | ||
| wait.withMessage(message); | ||
| wait.ignoreAll(exceptionsToIgnore); | ||
| | ||
| try { | ||
| return wait.until(condition); | ||
| } finally { | ||
| getBrowser().setImplicitWaitTimeout(getTimeoutConfiguration().getImplicit()); | ||
| } | ||
| } | ||
| | ||
| private static Browser getBrowser(){ | ||
| return BrowserManager.getBrowser(); | ||
| } | ||
| | ||
| private static ITimeoutConfiguration getTimeoutConfiguration(){ | ||
| return Configuration.getInstance().getTimeoutConfiguration(); | ||
| } | ||
| | ||
| private static LocalizationManager getLocalizationManager(){ | ||
| return LocalizationManager.getInstance(); | ||
| } | ||
| | ||
| private static double getCurrentTime(){ | ||
| return System.nanoTime()/Math.pow(10,9); | ||
| } | ||
| } | ||
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.