Skip to content

Commit

Permalink
Fix ExpectedConditions.or behavior when one of the conditions throws.
Browse files Browse the repository at this point in the history
Throwing an exception should be treated as "false". This regressed in 80a91b4 when the blanket try-catch was deleted.
  • Loading branch information
juangj committed Mar 22, 2017
1 parent 5b04ef6 commit 9a70926
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ private static boolean isInvisible(final WebElement element) {
/**
* An expectation with the logical or condition of the given list of conditions.
*
* Each condition is checked until at leas one of them returns true or not null
* Each condition is checked until at least one of them returns true or not null.
*
* @param conditions ExpectedCondition is a list of alternative conditions
* @return true once one of conditions is satisfied
Expand All @@ -1421,7 +1421,9 @@ public static ExpectedCondition<Boolean> or(final ExpectedCondition<?>... condit
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
RuntimeException lastException = null;
for (ExpectedCondition<?> condition : conditions) {
try {
Object result = condition.apply(driver);
if (result != null) {
if (result instanceof Boolean) {
Expand All @@ -1432,6 +1434,12 @@ public Boolean apply(WebDriver driver) {
return true;
}
}
} catch (RuntimeException e) {
lastException = e;
}
}
if (lastException != null) {
throw lastException;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,28 @@ public void waitForOneOfExpectedConditionsToHavePositiveResultWhenSecondPositive
attributeToBe(mockElement, attributeName, attributeName))));
}

@Test
public void waitForOneOfExpectedConditionsToHavePositiveResultWhenOneThrows() {
String attributeName = "test";
when(mockElement.getAttribute(attributeName)).thenReturn(attributeName);
when(mockElement.getCssValue(attributeName)).thenReturn(attributeName);
when(mockElement.getText()).thenThrow(new NoSuchElementException(""));

assertTrue(wait.until(or(textToBePresentInElement(mockElement, attributeName),
attributeToBe(mockElement, attributeName, attributeName))));
}

@Test(expected = NoSuchElementException.class)
public void waitForOneOfExpectedConditionsToHavePositiveResultWhenAllThrow() {
String attributeName = "test";
when(mockElement.getAttribute(attributeName)).thenThrow(new NoSuchElementException(""));
when(mockElement.getCssValue(attributeName)).thenThrow(new NoSuchElementException(""));
when(mockElement.getText()).thenThrow(new NoSuchElementException(""));

wait.until(or(textToBePresentInElement(mockElement, attributeName),
attributeToBe(mockElement, attributeName, attributeName)));
}


@Test(expected = TimeoutException.class)
public void waitingForAllExpectedConditionsToHavePositiveResultWhenAllFailed() {
Expand Down

0 comments on commit 9a70926

Please sign in to comment.