Skip to content

Commit 9a70926

Browse files
committed
Fix ExpectedConditions.or behavior when one of the conditions throws.
Throwing an exception should be treated as "false". This regressed in 80a91b4 when the blanket try-catch was deleted.
1 parent 5b04ef6 commit 9a70926

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ private static boolean isInvisible(final WebElement element) {
14121412
/**
14131413
* An expectation with the logical or condition of the given list of conditions.
14141414
*
1415-
* Each condition is checked until at leas one of them returns true or not null
1415+
* Each condition is checked until at least one of them returns true or not null.
14161416
*
14171417
* @param conditions ExpectedCondition is a list of alternative conditions
14181418
* @return true once one of conditions is satisfied
@@ -1421,7 +1421,9 @@ public static ExpectedCondition<Boolean> or(final ExpectedCondition<?>... condit
14211421
return new ExpectedCondition<Boolean>() {
14221422
@Override
14231423
public Boolean apply(WebDriver driver) {
1424+
RuntimeException lastException = null;
14241425
for (ExpectedCondition<?> condition : conditions) {
1426+
try {
14251427
Object result = condition.apply(driver);
14261428
if (result != null) {
14271429
if (result instanceof Boolean) {
@@ -1432,6 +1434,12 @@ public Boolean apply(WebDriver driver) {
14321434
return true;
14331435
}
14341436
}
1437+
} catch (RuntimeException e) {
1438+
lastException = e;
1439+
}
1440+
}
1441+
if (lastException != null) {
1442+
throw lastException;
14351443
}
14361444
return false;
14371445
}

java/client/test/org/openqa/selenium/support/ui/ExpectedConditionsTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,28 @@ public void waitForOneOfExpectedConditionsToHavePositiveResultWhenSecondPositive
640640
attributeToBe(mockElement, attributeName, attributeName))));
641641
}
642642

643+
@Test
644+
public void waitForOneOfExpectedConditionsToHavePositiveResultWhenOneThrows() {
645+
String attributeName = "test";
646+
when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);
647+
when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);
648+
when(mockElement.getText()).thenThrow(new NoSuchElementException(""));
649+
650+
assertTrue(wait.until(or(textToBePresentInElement(mockElement, attributeName),
651+
attributeToBe(mockElement, attributeName, attributeName))));
652+
}
653+
654+
@Test(expected = NoSuchElementException.class)
655+
public void waitForOneOfExpectedConditionsToHavePositiveResultWhenAllThrow() {
656+
String attributeName = "test";
657+
when(mockElement.getAttribute(attributeName)).thenThrow(new NoSuchElementException(""));
658+
when(mockElement.getCssValue(attributeName)).thenThrow(new NoSuchElementException(""));
659+
when(mockElement.getText()).thenThrow(new NoSuchElementException(""));
660+
661+
wait.until(or(textToBePresentInElement(mockElement, attributeName),
662+
attributeToBe(mockElement, attributeName, attributeName)));
663+
}
664+
643665

644666
@Test(expected = TimeoutException.class)
645667
public void waitingForAllExpectedConditionsToHavePositiveResultWhenAllFailed() {

0 commit comments

Comments
 (0)