Skip to content

Commit 5f7deba

Browse files
asolntsevAutomatedTester
authored andcommitted
[java] remove @Nullable from return value for ExpectedConditions that never return null (#17149)
mark `wait.until` return value as non-nullable ... and <V> type of Function both nullable and non-nullable. Expression `wait.until(condition)` never returns null. It either returns an Object or true, or throws TimeoutException. While `Function<T, V> isTrue` can return null. Fixes #17122 P.S. This is a crazy trick to satisfy Kotlin compiler: ```java <V extends @nullable Object> @nonnull V until(Function<? super F, ? extends V> isTrue); ``` Thanks to `<V extends @nullable Object>`, Kotlin compiler accepts both nullable and non-nullable functions: * `(Function<? super T, V> isTrue)` * `(Function<? super T, @nullable V> isTrue)`
1 parent c672969 commit 5f7deba

3 files changed

Lines changed: 25 additions & 22 deletions

File tree

java/src/org/openqa/selenium/support/ui/ExpectedConditions.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ public String toString() {
247247
*/
248248
public static ExpectedCondition<@Nullable List<WebElement>> visibilityOfAllElementsLocatedBy(
249249
final By locator) {
250-
return new ExpectedCondition<@Nullable List<WebElement>>() {
251-
private int indexOfInvisibleElement;
250+
return new ExpectedCondition<>() {
251+
private int indexOfInvisibleElement = -1;
252252
private @Nullable WebElement invisibleElement;
253253

254254
@Override
@@ -304,8 +304,8 @@ public String toString() {
304304
*/
305305
public static ExpectedCondition<@Nullable List<WebElement>> visibilityOfAllElements(
306306
final List<WebElement> elements) {
307-
return new ExpectedCondition<@Nullable List<WebElement>>() {
308-
private int indexOfInvisibleElement;
307+
return new ExpectedCondition<>() {
308+
private int indexOfInvisibleElement = -1;
309309
private @Nullable WebElement invisibleElement;
310310

311311
@Override
@@ -345,7 +345,7 @@ public String toString() {
345345
* @return the (same) WebElement once it is visible
346346
*/
347347
public static ExpectedCondition<@Nullable WebElement> visibilityOf(final WebElement element) {
348-
return new ExpectedCondition<@Nullable WebElement>() {
348+
return new ExpectedCondition<>() {
349349
@Override
350350
public @Nullable WebElement apply(WebDriver driver) {
351351
return elementIfVisible(element);
@@ -373,7 +373,7 @@ public String toString() {
373373
*/
374374
public static ExpectedCondition<@Nullable List<WebElement>> presenceOfAllElementsLocatedBy(
375375
final By locator) {
376-
return new ExpectedCondition<@Nullable List<WebElement>>() {
376+
return new ExpectedCondition<>() {
377377
@Override
378378
public @Nullable List<WebElement> apply(WebDriver driver) {
379379
List<WebElement> elements = driver.findElements(locator);
@@ -596,7 +596,7 @@ public String toString() {
596596
*/
597597
public static ExpectedCondition<@Nullable WebDriver> frameToBeAvailableAndSwitchToIt(
598598
final By locator) {
599-
return new ExpectedCondition<@Nullable WebDriver>() {
599+
return new ExpectedCondition<>() {
600600
private @Nullable NotFoundException error;
601601

602602
@Override
@@ -660,7 +660,7 @@ public String toString() {
660660
*/
661661
public static ExpectedCondition<@Nullable WebDriver> frameToBeAvailableAndSwitchToIt(
662662
final WebElement frame) {
663-
return new ExpectedCondition<@Nullable WebDriver>() {
663+
return new ExpectedCondition<>() {
664664
private @Nullable NoSuchFrameException error;
665665

666666
@Override
@@ -850,7 +850,7 @@ public String toString() {
850850
* @return the result of the provided condition
851851
*/
852852
public static <T> ExpectedCondition<@Nullable T> refreshed(final ExpectedCondition<T> condition) {
853-
return new ExpectedCondition<@Nullable T>() {
853+
return new ExpectedCondition<>() {
854854
@Override
855855
public @Nullable T apply(WebDriver driver) {
856856
try {
@@ -934,7 +934,7 @@ public String toString() {
934934
}
935935

936936
public static ExpectedCondition<@Nullable Alert> alertIsPresent() {
937-
return new ExpectedCondition<@Nullable Alert>() {
937+
return new ExpectedCondition<>() {
938938
@Override
939939
public @Nullable Alert apply(WebDriver driver) {
940940
try {
@@ -953,7 +953,7 @@ public String toString() {
953953

954954
public static ExpectedCondition<Boolean> numberOfWindowsToBe(final int expectedNumberOfWindows) {
955955
return new ExpectedCondition<>() {
956-
private int actualNumberOfWindows;
956+
private int actualNumberOfWindows = -1;
957957
private @Nullable WebDriverException error;
958958

959959
@Override
@@ -1136,7 +1136,7 @@ public String toString() {
11361136
*/
11371137
public static ExpectedCondition<@Nullable List<WebElement>> numberOfElementsToBeMoreThan(
11381138
final By locator, final Integer expectedNumber) {
1139-
return new ExpectedCondition<@Nullable List<WebElement>>() {
1139+
return new ExpectedCondition<>() {
11401140
private Integer actualNumber = 0;
11411141

11421142
@Override
@@ -1165,7 +1165,7 @@ public String toString() {
11651165
*/
11661166
public static ExpectedCondition<@Nullable List<WebElement>> numberOfElementsToBeLessThan(
11671167
final By locator, final Integer number) {
1168-
return new ExpectedCondition<@Nullable List<WebElement>>() {
1168+
return new ExpectedCondition<>() {
11691169
private Integer currentNumber = 0;
11701170

11711171
@Override
@@ -1193,7 +1193,7 @@ public String toString() {
11931193
*/
11941194
public static ExpectedCondition<@Nullable List<WebElement>> numberOfElementsToBe(
11951195
final By locator, final Integer expectedNumberOfElements) {
1196-
return new ExpectedCondition<@Nullable List<WebElement>>() {
1196+
return new ExpectedCondition<>() {
11971197
private Integer actualNumberOfElements = -1;
11981198

11991199
@Override
@@ -1414,7 +1414,7 @@ private static Optional<String> getAttributeOrCssValue(WebElement element, Strin
14141414
*/
14151415
public static ExpectedCondition<@Nullable List<WebElement>> visibilityOfNestedElementsLocatedBy(
14161416
final By parent, final By childLocator) {
1417-
return new ExpectedCondition<@Nullable List<WebElement>>() {
1417+
return new ExpectedCondition<>() {
14181418
private int indexOfInvisibleElement = -1;
14191419
private @Nullable WebElement invisibleChild;
14201420

@@ -1465,7 +1465,7 @@ public String toString() {
14651465
*/
14661466
public static ExpectedCondition<@Nullable List<WebElement>> visibilityOfNestedElementsLocatedBy(
14671467
final WebElement element, final By childLocator) {
1468-
return new ExpectedCondition<@Nullable List<WebElement>>() {
1468+
return new ExpectedCondition<>() {
14691469
private int indexOfInvisibleElement = -1;
14701470
private @Nullable WebElement invisibleChild;
14711471

@@ -1575,7 +1575,7 @@ public String toString() {
15751575
*/
15761576
public static ExpectedCondition<@Nullable List<WebElement>> presenceOfNestedElementsLocatedBy(
15771577
final By parent, final By childLocator) {
1578-
return new ExpectedCondition<@Nullable List<WebElement>>() {
1578+
return new ExpectedCondition<>() {
15791579

15801580
@Override
15811581
public @Nullable List<WebElement> apply(WebDriver driver) {
@@ -1610,7 +1610,7 @@ public static ExpectedCondition<Boolean> invisibilityOfAllElements(final WebElem
16101610
public static ExpectedCondition<Boolean> invisibilityOfAllElements(
16111611
final List<WebElement> elements) {
16121612
return new ExpectedCondition<>() {
1613-
private int indexOfVisibleElement;
1613+
private int indexOfVisibleElement = -1;
16141614
private @Nullable WebElement visibleElement;
16151615

16161616
@Override
@@ -1808,7 +1808,7 @@ public String toString() {
18081808
* @return object once JavaScript executes without errors
18091809
*/
18101810
public static ExpectedCondition<@Nullable Object> jsReturnsValue(final String javaScript) {
1811-
return new ExpectedCondition<@Nullable Object>() {
1811+
return new ExpectedCondition<>() {
18121812
private @Nullable WebDriverException error;
18131813

18141814
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.List;
3030
import java.util.function.Function;
3131
import java.util.function.Supplier;
32+
import org.jspecify.annotations.NonNull;
3233
import org.jspecify.annotations.Nullable;
3334
import org.openqa.selenium.TimeoutException;
3435
import org.openqa.selenium.WebDriverException;
@@ -200,7 +201,7 @@ public FluentWait<T> ignoring(
200201
* @throws TimeoutException If the timeout expires.
201202
*/
202203
@Override
203-
public <V> V until(Function<? super T, @Nullable V> isTrue) {
204+
public <V extends @Nullable Object> @NonNull V until(Function<? super T, ? extends V> isTrue) {
204205
Instant end = clock.instant().plus(timeout);
205206

206207
Throwable lastException;

java/src/org/openqa/selenium/support/ui/Wait.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.openqa.selenium.support.ui;
1919

2020
import java.util.function.Function;
21+
import org.jspecify.annotations.NonNull;
22+
import org.jspecify.annotations.Nullable;
2123

2224
/**
2325
* A generic interface for waiting until a condition is true or not null. The condition may take a
@@ -36,9 +38,9 @@ public interface Wait<F> {
3638
* implementor may throw whatever is idiomatic for a given test infrastructure (e.g. JUnit4 would
3739
* throw {@link AssertionError}).
3840
*
39-
* @param <T> the return type of the method, which must not be Void
41+
* @param <V> the return type of the method, which must not be Void
4042
* @param isTrue the parameter to pass to the {@link ExpectedCondition}
4143
* @return truthy value from the isTrue condition
4244
*/
43-
<T> T until(Function<? super F, T> isTrue);
45+
<V extends @Nullable Object> @NonNull V until(Function<? super F, ? extends V> isTrue);
4446
}

0 commit comments

Comments
 (0)