Skip to content

Commit 4c93795

Browse files
committed
Deprecate our Clock class in favour of the one in java.date
Also deprecate all constructors and methods that take our Clock.
1 parent 2cbcd08 commit 4c93795

File tree

14 files changed

+288
-105
lines changed

14 files changed

+288
-105
lines changed

java/client/src/org/openqa/selenium/lift/WebDriverTestContext.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.openqa.selenium.support.ui.Clock;
2929
import org.openqa.selenium.support.ui.ExpectedCondition;
3030
import org.openqa.selenium.support.ui.Sleeper;
31-
import org.openqa.selenium.support.ui.SystemClock;
3231
import org.openqa.selenium.support.ui.Wait;
3332
import org.openqa.selenium.support.ui.WebDriverWait;
3433

@@ -40,14 +39,22 @@
4039
public class WebDriverTestContext implements TestContext {
4140

4241
private WebDriver driver;
43-
private final Clock clock;
42+
private final java.time.Clock clock;
4443
private final Sleeper sleeper;
4544

4645
public WebDriverTestContext(WebDriver driver) {
47-
this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER);
46+
this(driver, java.time.Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER);
4847
}
4948

49+
/**
50+
* @deprecated Use {@link #WebDriverTestContext(WebDriver, java.time.Clock, Sleeper)}.
51+
*/
52+
@Deprecated
5053
WebDriverTestContext(WebDriver driver, Clock clock, Sleeper sleeper) {
54+
this(driver, clock.asJreClock(), sleeper);
55+
}
56+
57+
WebDriverTestContext(WebDriver driver, java.time.Clock clock, Sleeper sleeper) {
5158
this.driver = driver;
5259
this.clock = clock;
5360
this.sleeper = sleeper;
@@ -132,7 +139,11 @@ public void waitFor(final Finder<WebElement, WebDriver> finder, final long timeo
132139
? defaultSleepTimeoutMillis : timeoutMillis / 2;
133140

134141
Wait<WebDriver> wait =
135-
new WebDriverWait(driver, clock, sleeper, millisToSeconds(timeoutMillis),
142+
new WebDriverWait(
143+
driver,
144+
clock,
145+
sleeper,
146+
millisToSeconds(timeoutMillis),
136147
sleepTimeout) {
137148
@Override
138149
protected RuntimeException timeoutException(String message, Throwable lastException) {

java/client/src/org/openqa/selenium/support/pagefactory/AjaxElementLocator.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
import org.openqa.selenium.NoSuchElementException;
2121
import org.openqa.selenium.SearchContext;
2222
import org.openqa.selenium.WebElement;
23-
import org.openqa.selenium.support.ui.Clock;
2423
import org.openqa.selenium.support.ui.SlowLoadableComponent;
25-
import org.openqa.selenium.support.ui.SystemClock;
2624

2725
import java.lang.reflect.Field;
26+
import java.time.Clock;
2827
import java.util.ArrayList;
2928
import java.util.List;
3029

@@ -48,12 +47,30 @@ public class AjaxElementLocator extends DefaultElementLocator {
4847
* @param timeOutInSeconds How long to wait for the element to appear. Measured in seconds.
4948
* @param annotations AbstractAnnotations class implementation
5049
*/
51-
public AjaxElementLocator(SearchContext context, int timeOutInSeconds, AbstractAnnotations annotations) {
52-
this(new SystemClock(), context, timeOutInSeconds, annotations);
50+
public AjaxElementLocator(
51+
SearchContext context,
52+
int timeOutInSeconds,
53+
AbstractAnnotations annotations) {
54+
this(Clock.systemDefaultZone(), context, timeOutInSeconds, annotations);
5355
}
5456

55-
public AjaxElementLocator(Clock clock, SearchContext context, int timeOutInSeconds,
56-
AbstractAnnotations annotations) {
57+
/**
58+
* @deprecated Use {@link #AjaxElementLocator(Clock, SearchContext, int, AbstractAnnotations)}.
59+
*/
60+
@Deprecated
61+
public AjaxElementLocator(
62+
org.openqa.selenium.support.ui.Clock clock,
63+
SearchContext context,
64+
int timeOutInSeconds,
65+
AbstractAnnotations annotations) {
66+
this(clock.asJreClock(), context, timeOutInSeconds, annotations);
67+
}
68+
69+
public AjaxElementLocator(
70+
Clock clock,
71+
SearchContext context,
72+
int timeOutInSeconds,
73+
AbstractAnnotations annotations) {
5774
super(context, annotations);
5875
this.timeOutInSeconds = timeOutInSeconds;
5976
this.clock = clock;
@@ -67,10 +84,26 @@ public AjaxElementLocator(Clock clock, SearchContext context, int timeOutInSecon
6784
* @param timeOutInSeconds How long to wait for the element to appear. Measured in seconds.
6885
*/
6986
public AjaxElementLocator(SearchContext searchContext, Field field, int timeOutInSeconds) {
70-
this(new SystemClock(), searchContext, field, timeOutInSeconds);
87+
this(Clock.systemDefaultZone(), searchContext, field, timeOutInSeconds);
7188
}
7289

73-
public AjaxElementLocator(Clock clock, SearchContext searchContext, Field field, int timeOutInSeconds) {
90+
/**
91+
* @deprecated Use {@link #AjaxElementLocator(Clock, SearchContext, int, AbstractAnnotations)}.
92+
*/
93+
@Deprecated
94+
public AjaxElementLocator(
95+
org.openqa.selenium.support.ui.Clock clock,
96+
SearchContext searchContext,
97+
Field field,
98+
int timeOutInSeconds) {
99+
this(clock.asJreClock(), searchContext, timeOutInSeconds, new Annotations(field));
100+
}
101+
102+
public AjaxElementLocator(
103+
Clock clock,
104+
SearchContext searchContext,
105+
Field field,
106+
int timeOutInSeconds) {
74107
this(clock, searchContext, timeOutInSeconds, new Annotations(field));
75108
}
76109

@@ -136,6 +169,11 @@ private class SlowLoadingElement extends SlowLoadableComponent<SlowLoadingElemen
136169
private NoSuchElementException lastException;
137170
private WebElement element;
138171

172+
@Deprecated
173+
public SlowLoadingElement(org.openqa.selenium.support.ui.Clock clock, int timeOutInSeconds) {
174+
super(clock, timeOutInSeconds);
175+
}
176+
139177
public SlowLoadingElement(Clock clock, int timeOutInSeconds) {
140178
super(clock, timeOutInSeconds);
141179
}
@@ -177,6 +215,11 @@ private class SlowLoadingElementList extends SlowLoadableComponent<SlowLoadingEl
177215
private NoSuchElementException lastException;
178216
private List<WebElement> elements;
179217

218+
@Deprecated
219+
public SlowLoadingElementList(org.openqa.selenium.support.ui.Clock clock, int timeOutInSeconds) {
220+
this(clock.asJreClock(), timeOutInSeconds);
221+
}
222+
180223
public SlowLoadingElementList(Clock clock, int timeOutInSeconds) {
181224
super(clock, timeOutInSeconds);
182225
}

java/client/src/org/openqa/selenium/support/ui/Clock.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,15 @@
1717

1818
package org.openqa.selenium.support.ui;
1919

20+
import java.time.Instant;
21+
import java.time.ZoneId;
22+
2023
/**
21-
* A simple encapsulation to allowing timing
24+
* A simple encapsulation to allowing timing.
25+
*
26+
* @deprecated Use {@link java.time.Clock}.
2227
*/
28+
@Deprecated
2329
public interface Clock {
2430

2531
/**
@@ -45,4 +51,27 @@ public interface Clock {
4551
*/
4652
boolean isNowBefore(long endInMillis);
4753

54+
default java.time.Clock asJreClock() {
55+
class JreClock extends java.time.Clock {
56+
57+
@Override
58+
public ZoneId getZone() {
59+
return ZoneId.systemDefault();
60+
}
61+
62+
@Override
63+
public java.time.Clock withZone(ZoneId zone) {
64+
throw new UnsupportedOperationException(
65+
"Please use a complete instance of java.time.Clock");
66+
}
67+
68+
@Override
69+
public Instant instant() {
70+
return Instant.ofEpochMilli(now());
71+
}
72+
}
73+
74+
return new JreClock();
75+
}
76+
4877
}

java/client/src/org/openqa/selenium/support/ui/FluentWait.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.openqa.selenium.support.ui;
1919

20-
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static java.util.Objects.requireNonNull;
2121
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2222

2323
import com.google.common.base.Throwables;
@@ -26,6 +26,7 @@
2626
import org.openqa.selenium.TimeoutException;
2727
import org.openqa.selenium.WebDriverException;
2828

29+
import java.time.Instant;
2930
import java.time.temporal.ChronoUnit;
3031
import java.util.ArrayList;
3132
import java.util.Collection;
@@ -79,7 +80,7 @@ public class FluentWait<T> implements Wait<T> {
7980
private static final java.time.Duration DEFAULT_WAIT_DURATION = java.time.Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT);
8081

8182
private final T input;
82-
private final Clock clock;
83+
private final java.time.Clock clock;
8384
private final Sleeper sleeper;
8485

8586
private java.time.Duration timeout = DEFAULT_WAIT_DURATION;
@@ -100,10 +101,20 @@ public FluentWait(T input) {
100101
* @param clock The clock to use when measuring the timeout.
101102
* @param sleeper Used to put the thread to sleep between evaluation loops.
102103
*/
104+
@Deprecated
103105
public FluentWait(T input, Clock clock, Sleeper sleeper) {
104-
this.input = checkNotNull(input);
105-
this.clock = checkNotNull(clock);
106-
this.sleeper = checkNotNull(sleeper);
106+
this(input, clock.asJreClock(), sleeper);
107+
}
108+
109+
/**
110+
* @param input The input value to pass to the evaluated conditions.
111+
* @param clock The clock to use when measuring the timeout.
112+
* @param sleeper Used to put the thread to sleep between evaluation loops.
113+
*/
114+
public FluentWait(T input, java.time.Clock clock, Sleeper sleeper) {
115+
this.input = requireNonNull(input);
116+
this.clock = requireNonNull(clock);
117+
this.sleeper = requireNonNull(sleeper);
107118
}
108119

109120
/**
@@ -241,7 +252,8 @@ public FluentWait<T> ignoring(Class<? extends Throwable> firstType,
241252
*/
242253
@Override
243254
public <V> V until(Function<? super T, V> isTrue) {
244-
long end = clock.laterBy(timeout.toMillis());
255+
Instant end = clock.instant().plus(timeout);
256+
245257
Throwable lastException;
246258
while (true) {
247259
try {
@@ -260,7 +272,7 @@ public <V> V until(Function<? super T, V> isTrue) {
260272

261273
// Check the timeout after evaluating the function to ensure conditions
262274
// with a zero timeout can succeed.
263-
if (!clock.isNowBefore(end)) {
275+
if (end.isBefore(clock.instant())) {
264276
String message = messageSupplier != null ?
265277
messageSupplier.get() : null;
266278

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.openqa.selenium.support.ui;
2+
3+
import org.openqa.selenium.Beta;
4+
5+
import java.time.Clock;
6+
import java.time.Instant;
7+
import java.time.ZoneId;
8+
import java.util.Objects;
9+
10+
@Beta
11+
class JreBackedClock extends java.time.Clock {
12+
13+
private final Clock delegate;
14+
15+
public JreBackedClock(Clock delegate) {
16+
this.delegate = Objects.requireNonNull(delegate);
17+
}
18+
19+
@Override
20+
public ZoneId getZone() {
21+
return null;
22+
}
23+
24+
@Override
25+
public Clock withZone(ZoneId zone) {
26+
return null;
27+
}
28+
29+
@Override
30+
public Instant instant() {
31+
return null;
32+
}
33+
}

java/client/src/org/openqa/selenium/support/ui/SlowLoadableComponent.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
package org.openqa.selenium.support.ui;
1919

20-
import static java.util.concurrent.TimeUnit.SECONDS;
20+
import java.time.Duration;
21+
import java.time.Instant;
2122

2223

2324
/**
@@ -33,12 +34,20 @@
3334
*/
3435
public abstract class SlowLoadableComponent<T extends LoadableComponent<T>>
3536
extends LoadableComponent<T> {
36-
private final Clock clock;
37-
private final long timeOutInSeconds;
37+
private final java.time.Clock clock;
38+
private final Duration timeOutInSeconds;
3839

40+
/**
41+
* @deprecated Use {@link #SlowLoadableComponent(java.time.Clock, int)}.
42+
*/
43+
@Deprecated
3944
public SlowLoadableComponent(Clock clock, int timeOutInSeconds) {
45+
this(clock.asJreClock(), timeOutInSeconds);
46+
}
47+
48+
public SlowLoadableComponent(java.time.Clock clock, int timeOutInSeconds) {
4049
this.clock = clock;
41-
this.timeOutInSeconds = timeOutInSeconds;
50+
this.timeOutInSeconds = Duration.ofSeconds(timeOutInSeconds);
4251
}
4352

4453
@Override
@@ -51,9 +60,9 @@ public T get() {
5160
load();
5261
}
5362

54-
long end = clock.laterBy(SECONDS.toMillis(timeOutInSeconds));
63+
Instant end = clock.instant().plus(timeOutInSeconds);
5564

56-
while (clock.isNowBefore(end)) {
65+
while (clock.instant().isBefore(end)) {
5766
try {
5867
isLoaded();
5968
return (T) this;

java/client/src/org/openqa/selenium/support/ui/SystemClock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package org.openqa.selenium.support.ui;
1919

20+
/**
21+
* @deprecated Use {@link java.time.Clock#systemDefaultZone()}.
22+
*/
23+
@Deprecated
2024
public class SystemClock implements Clock {
2125

2226
public long laterBy(long durationInMillis) {

0 commit comments

Comments
 (0)