-
-
Notifications
You must be signed in to change notification settings - Fork 768
feat: add pollDelay mechanism into AppiumFluentWait #2116
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
mykola-mokhnach
merged 11 commits into
appium:master
from
AlessandroMiccoli:feature/ISSUE-2111
Feb 19, 2024
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0003145
feat: add pollDelay mechanism into AppiumFluentWait
AlessandroMiccoli 8b8a9dd
docs: add javadoc for withPollDelay
AlessandroMiccoli f6b861a
refactor: improved until and nested method of AppiumFluentWait
AlessandroMiccoli d3db110
refactor: change order of imports in AppiumFluentWait
AlessandroMiccoli 6e47723
refactor: add whitespace after if
AlessandroMiccoli a42bc24
refactor: amend message generation in AppiumFluentWait
AlessandroMiccoli d6effd2
refactor: change method name from sleepUninterruptible to sleepInterr…
AlessandroMiccoli 928ec44
refactor: amend type to var in AppiumFluentWait
AlessandroMiccoli 1894a47
refactor: amend getIntervalWithPollingStrategy in AppiumFluentWait
AlessandroMiccoli d091447
refactor: amend millis to ms in timeoutMessage
AlessandroMiccoli f47dc63
Merge branch 'master' into feature/ISSUE-2111
AlessandroMiccoli 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,12 +29,16 @@ | |
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
public class AppiumFluentWait<T> extends FluentWait<T> { | ||
private Function<IterationInfo, Duration> pollingStrategy = null; | ||
|
||
private static final Duration DEFAULT_POLL_DELAY_DURATION = Duration.ZERO; | ||
private Duration pollDelay = DEFAULT_POLL_DELAY_DURATION; | ||
|
||
public static class IterationInfo { | ||
/** | ||
* The current iteration number. | ||
|
@@ -98,6 +102,18 @@ public AppiumFluentWait(T input, Clock clock, Sleeper sleeper) { | |
super(input, clock, sleeper); | ||
} | ||
|
||
/** | ||
* Sets how long to wait before starting to evaluate condition to be true. | ||
* The default pollDelay is {@link #DEFAULT_POLL_DELAY_DURATION}. | ||
* | ||
* @param pollDelay The pollDelay duration. | ||
* @return A self reference. | ||
*/ | ||
public AppiumFluentWait<T> withPollDelay(Duration pollDelay) { | ||
this.pollDelay = pollDelay; | ||
return this; | ||
} | ||
|
||
private <B> B getPrivateFieldValue(String fieldName, Class<B> fieldType) { | ||
return ReflectionHelpers.getPrivateFieldValue(FluentWait.class, this, fieldName, fieldType); | ||
} | ||
|
@@ -200,10 +216,19 @@ public AppiumFluentWait<T> withPollingStrategy(Function<IterationInfo, Duration> | |
*/ | ||
@Override | ||
public <V> V until(Function<? super T, V> isTrue) { | ||
final Instant start = getClock().instant(); | ||
final Instant end = getClock().instant().plus(getTimeout()); | ||
long iterationNumber = 1; | ||
final var start = getClock().instant(); | ||
// Adding pollDelay to end instant will allow to verify the condition for the expected timeout duration. | ||
final var end = start.plus(getTimeout()).plus(pollDelay); | ||
|
||
return performIteration(isTrue, start, end); | ||
} | ||
|
||
private <V> V performIteration(Function<? super T, V> isTrue, Instant start, Instant end) { | ||
var iterationNumber = 1; | ||
Throwable lastException; | ||
|
||
sleepInterruptibly(pollDelay); | ||
|
||
while (true) { | ||
try { | ||
V value = isTrue.apply(getInput()); | ||
|
@@ -222,32 +247,51 @@ public <V> V until(Function<? super T, V> isTrue) { | |
// Check the timeout after evaluating the function to ensure conditions | ||
// with a zero timeout can succeed. | ||
if (end.isBefore(getClock().instant())) { | ||
String message = getMessageSupplier() != null ? getMessageSupplier().get() : null; | ||
|
||
String timeoutMessage = String.format( | ||
"Expected condition failed: %s (tried for %d second(s) with %s interval)", | ||
message == null ? "waiting for " + isTrue : message, | ||
getTimeout().getSeconds(), getInterval()); | ||
throw timeoutException(timeoutMessage, lastException); | ||
handleTimeoutException(lastException, isTrue); | ||
} | ||
|
||
try { | ||
Duration interval = getInterval(); | ||
if (pollingStrategy != null) { | ||
final IterationInfo info = new IterationInfo(iterationNumber, | ||
Duration.between(start, getClock().instant()), getTimeout(), | ||
interval); | ||
interval = pollingStrategy.apply(info); | ||
} | ||
getSleeper().sleep(interval); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new WebDriverException(e); | ||
} | ||
var interval = getIntervalWithPollingStrategy(start, iterationNumber); | ||
sleepInterruptibly(interval); | ||
|
||
++iterationNumber; | ||
} | ||
} | ||
|
||
private <V> void handleTimeoutException(Throwable lastException, Function<? super T, V> isTrue) { | ||
var message = Optional.ofNullable(getMessageSupplier()) | ||
.map(Supplier::get) | ||
.orElseGet(() -> "waiting for " + isTrue); | ||
|
||
var timeoutMessage = String.format( | ||
"Expected condition failed: %s (tried for %s millis with an interval of %s millis)", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. millis -> ms There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
message, | ||
getTimeout().toMillis(), | ||
getInterval().toMillis() | ||
); | ||
|
||
throw timeoutException(timeoutMessage, lastException); | ||
} | ||
|
||
private Duration getIntervalWithPollingStrategy(Instant start, long iterationNumber) { | ||
var interval = getInterval(); | ||
return Optional.ofNullable(pollingStrategy) | ||
.map(strategy -> strategy.apply(new IterationInfo( | ||
iterationNumber, | ||
Duration.between(start, getClock().instant()), getTimeout(), interval))) | ||
.orElse(interval); | ||
} | ||
|
||
private void sleepInterruptibly(Duration duration) { | ||
try { | ||
if (!duration.isZero() && !duration.isNegative()) { | ||
getSleeper().sleep(duration); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw new WebDriverException(e); | ||
} | ||
} | ||
|
||
protected Throwable propagateIfNotIgnored(Throwable e) { | ||
for (Class<? extends Throwable> ignoredException : getIgnoredExceptions()) { | ||
if (ignoredException.isInstance(e)) { | ||
|
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.