Skip to content

Commit

Permalink
Deprecate internal Duration in favor of java.time.Duration
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stewart <simon.m.stewart@gmail.com>
  • Loading branch information
valfirst authored and shs96c committed Feb 19, 2018
1 parent 7b763e2 commit 49fe105
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
* Gives the context for a test, holds page state, and interacts with the {@link WebDriver}.
Expand Down Expand Up @@ -138,7 +137,7 @@ public Boolean apply(WebDriver driver) {
}
};

final long defaultSleepTimeoutMillis = FluentWait.FIVE_HUNDRED_MILLIS.in(TimeUnit.MILLISECONDS);
final long defaultSleepTimeoutMillis = 500;
final long sleepTimeout = (timeoutMillis > defaultSleepTimeoutMillis)
? defaultSleepTimeoutMillis : timeoutMillis / 2;

Expand Down
9 changes: 9 additions & 0 deletions java/client/src/org/openqa/selenium/support/ui/Duration.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@

/**
* Represents an immutable duration of time.
*
* @deprecated use {@link java.time.Duration}
*/
@Deprecated
public class Duration {

private final long time;
private final TimeUnit unit;

/**
* @deprecated use {@link java.time.Duration}
*
* @param time The amount of time.
* @param unit The unit of time.
*/
@Deprecated
public Duration(long time, TimeUnit unit) {
checkArgument(time >= 0, "Duration < 0: %d", time);
checkNotNull(unit);
Expand Down Expand Up @@ -66,9 +72,12 @@ public String toString() {
/**
* Converts this duration to the given unit of time.
*
* @deprecated use {@link java.time.Duration}
*
* @param unit The time unit to convert to.
* @return The value of this duration in the specified unit of time.
*/
@Deprecated
public long in(TimeUnit unit) {
return unit.convert(time, this.unit);
}
Expand Down
88 changes: 79 additions & 9 deletions java/client/src/org/openqa/selenium/support/ui/FluentWait.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;

import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
Expand All @@ -28,6 +27,8 @@
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriverException;

import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -68,14 +69,22 @@
*/
public class FluentWait<T> implements Wait<T> {

public static final Duration FIVE_HUNDRED_MILLIS = new Duration(500, MILLISECONDS);
protected final static long DEFAULT_SLEEP_TIMEOUT = 500;

/**
* @deprecated use {@link #DEFAULT_WAIT_DURATION}
*/
@Deprecated
public static final Duration FIVE_HUNDRED_MILLIS = new Duration(DEFAULT_SLEEP_TIMEOUT, MILLISECONDS);

private static final java.time.Duration DEFAULT_WAIT_DURATION = java.time.Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT);

private final T input;
private final Clock clock;
private final Sleeper sleeper;

private Duration timeout = FIVE_HUNDRED_MILLIS;
private Duration interval = FIVE_HUNDRED_MILLIS;
private java.time.Duration timeout = DEFAULT_WAIT_DURATION;
private java.time.Duration interval = DEFAULT_WAIT_DURATION;
private Supplier<String> messageSupplier = () -> null;

private List<Class<? extends Throwable>> ignoredExceptions = Lists.newLinkedList();
Expand All @@ -102,12 +111,27 @@ public FluentWait(T input, Clock clock, Sleeper sleeper) {
* Sets how long to wait for the evaluated condition to be true. The default timeout is
* {@link #FIVE_HUNDRED_MILLIS}.
*
* @deprecated use {@link #withTimeout(java.time.Duration)}
*
* @param duration The timeout duration.
* @param unit The unit of time.
* @return A self reference.
*/
@Deprecated
public FluentWait<T> withTimeout(long duration, TimeUnit unit) {
this.timeout = new Duration(duration, unit);
return withTimeout(java.time.Duration.of(duration, toChronoUnit(unit)));
}


/**
* Sets how long to wait for the evaluated condition to be true. The default timeout is
* {@link #DEFAULT_WAIT_DURATION}.
*
* @param timeout The timeout duration.
* @return A self reference.
*/
public FluentWait<T> withTimeout(java.time.Duration timeout) {
this.timeout = timeout;
return this;
}

Expand Down Expand Up @@ -140,12 +164,29 @@ public FluentWait<T> withMessage(Supplier<String> messageSupplier) {
* In reality, the interval may be greater as the cost of actually evaluating a condition function
* is not factored in. The default polling interval is {@link #FIVE_HUNDRED_MILLIS}.
*
* @deprecated use {@link #pollingEvery(java.time.Duration)}
*
* @param duration The timeout duration.
* @param unit The unit of time.
* @return A self reference.
*/
@Deprecated
public FluentWait<T> pollingEvery(long duration, TimeUnit unit) {
this.interval = new Duration(duration, unit);
return pollingEvery(java.time.Duration.of(duration, toChronoUnit(unit)));
}

/**
* Sets how often the condition should be evaluated.
*
* <p>
* In reality, the interval may be greater as the cost of actually evaluating a condition function
* is not factored in. The default polling interval is {@link #DEFAULT_WAIT_DURATION}.
*
* @param interval The timeout duration.
* @return A self reference.
*/
public FluentWait<T> pollingEvery(java.time.Duration interval) {
this.interval = interval;
return this;
}

Expand Down Expand Up @@ -201,7 +242,7 @@ public FluentWait<T> ignoring(Class<? extends Throwable> firstType,
*/
@Override
public <V> V until(Function<? super T, V> isTrue) {
long end = clock.laterBy(timeout.in(MILLISECONDS));
long end = clock.laterBy(timeout.toMillis());
Throwable lastException;
while (true) {
try {
Expand All @@ -225,9 +266,9 @@ public <V> V until(Function<? super T, V> isTrue) {
messageSupplier.get() : null;

String timeoutMessage = String.format(
"Expected condition failed: %s (tried for %d second(s) with %s interval)",
"Expected condition failed: %s (tried for %d second(s) with %d milliseconds interval)",
message == null ? "waiting for " + isTrue : message,
timeout.in(SECONDS), interval);
timeout.getSeconds(), interval.toMillis());
throw timeoutException(timeoutMessage, lastException);
}

Expand Down Expand Up @@ -262,4 +303,33 @@ private Throwable propagateIfNotIgnored(Throwable e) {
protected RuntimeException timeoutException(String message, Throwable lastException) {
throw new TimeoutException(message, lastException);
}

/**
* Converts the {@code TimeUnit} to the equivalent {@code ChronoUnit}.
*
* This is a backport from Java 9, see https://bugs.openjdk.java.net/browse/JDK-8141452.
*
* @param timeUnit the TimeUnit to convert
* @return the converted equivalent ChronoUnit
*/
private ChronoUnit toChronoUnit(TimeUnit timeUnit) {
switch (timeUnit) {
case NANOSECONDS:
return ChronoUnit.NANOS;
case MICROSECONDS:
return ChronoUnit.MICROS;
case MILLISECONDS:
return ChronoUnit.MILLIS;
case SECONDS:
return ChronoUnit.SECONDS;
case MINUTES:
return ChronoUnit.MINUTES;
case HOURS:
return ChronoUnit.HOURS;
case DAYS:
return ChronoUnit.DAYS;
default:
throw new IllegalArgumentException("No ChronoUnit equivalent for " + timeUnit);
}
}
}
26 changes: 26 additions & 0 deletions java/client/src/org/openqa/selenium/support/ui/Sleeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,42 @@
public interface Sleeper {

public static final Sleeper SYSTEM_SLEEPER = new Sleeper() {

/**
* Sleeps for the specified duration of time.
*
* @deprecated use {@link #sleep(java.time.Duration)}
*
* @param duration How long to sleep.
* @throws InterruptedException If the thread is interrupted while sleeping.
*/
@Deprecated
public void sleep(Duration duration) throws InterruptedException {
Thread.sleep(duration.in(TimeUnit.MILLISECONDS));
}

@Override
public void sleep(java.time.Duration duration) throws InterruptedException {
Thread.sleep(duration.toMillis());
}
};

/**
* Sleeps for the specified duration of time.
*
* @deprecated use {@link #sleep(java.time.Duration)}
*
* @param duration How long to sleep.
* @throws InterruptedException If the thread is interrupted while sleeping.
*/
@Deprecated
void sleep(Duration duration) throws InterruptedException;

/**
* Sleeps for the specified duration of time.
*
* @param duration How long to sleep.
* @throws InterruptedException If the thread is interrupted while sleeping.
*/
void sleep(java.time.Duration duration) throws InterruptedException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import org.openqa.selenium.internal.WrapsDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.util.concurrent.TimeUnit;
import java.time.Duration;
import java.time.temporal.ChronoUnit;

/**
* A specialization of {@link FluentWait} that uses WebDriver instances.
*/
public class WebDriverWait extends FluentWait<WebDriver> {
public final static long DEFAULT_SLEEP_TIMEOUT = 500;
private final WebDriver driver;

/**
Expand Down Expand Up @@ -70,8 +70,8 @@ public WebDriverWait(WebDriver driver, long timeOutInSeconds, long sleepInMillis
public WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds,
long sleepTimeOut) {
super(driver, clock, sleeper);
withTimeout(timeOutInSeconds, TimeUnit.SECONDS);
pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS);
withTimeout(Duration.ofSeconds(timeOutInSeconds));
pollingEvery(Duration.ofMillis(sleepTimeOut));
ignoring(NotFoundException.class);
this.driver = driver;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

@RunWith(JUnit4.class)
Expand All @@ -100,8 +101,8 @@ public void setUpMocks() {
MockitoAnnotations.initMocks(this);

wait = new FluentWait<>(mockDriver, mockClock, mockSleeper)
.withTimeout(1, TimeUnit.SECONDS)
.pollingEvery(250, TimeUnit.MILLISECONDS);
.withTimeout(Duration.ofSeconds(1))
.pollingEvery(Duration.ofMillis(250));
}

@Test
Expand Down Expand Up @@ -179,7 +180,7 @@ public void waitingForVisibilityOfElement_elementBecomesVisible() throws Interru
when(mockElement.isDisplayed()).thenReturn(false, false, true);

assertSame(mockElement, wait.until(visibilityOf(mockElement)));
verify(mockSleeper, times(2)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(2)).sleep(Duration.ofMillis(250));
}

@Test
Expand All @@ -195,7 +196,7 @@ public void waitingForVisibilityOfElement_elementNeverBecomesVisible()
} catch (TimeoutException expected) {
// Do nothing.
}
verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));
}

@Test
Expand All @@ -213,7 +214,7 @@ public void waitingForVisibilityOfElementInverse_elementDisappears() throws Inte
when(mockElement.isDisplayed()).thenReturn(true, true, false);

assertTrue(wait.until(not(visibilityOf(mockElement))));
verify(mockSleeper, times(2)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(2)).sleep(Duration.ofMillis(250));
}

@Test
Expand All @@ -229,7 +230,7 @@ public void waitingForVisibilityOfElementInverse_elementStaysVisible()
} catch (TimeoutException expected) {
// Do nothing.
}
verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));
}

@Test
Expand Down Expand Up @@ -260,7 +261,7 @@ public void invertingAConditionThatAlwaysReturnsTrueTimesout() throws Interrupte
} catch (TimeoutException expected) {
// Do nothing.
}
verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));
}

@Test
Expand All @@ -275,7 +276,7 @@ public void doubleNegatives_conditionThatReturnsFalseTimesOut() throws Interrupt
} catch (TimeoutException expected) {
// Do nothing.
}
verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));
}

@Test
Expand All @@ -290,7 +291,7 @@ public void doubleNegatives_conditionThatReturnsNullTimesOut() throws Interrupte
} catch (TimeoutException expected) {
// Do nothing.
}
verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
verify(mockSleeper, times(1)).sleep(Duration.ofMillis(250));
}

@Test
Expand Down
Loading

0 comments on commit 49fe105

Please sign in to comment.