Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions java/src/org/openqa/selenium/support/ui/ExpectedConditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ public static ExpectedCondition<Boolean> textToBePresentInElement(

return new ExpectedCondition<>() {
private @Nullable String elementText;
private @Nullable StaleElementReferenceException error;
private @Nullable WebDriverException error;

@Override
public Boolean apply(WebDriver driver) {
Expand All @@ -408,7 +408,7 @@ public Boolean apply(WebDriver driver) {
try {
elementText = element.getText();
return elementText.contains(text);
} catch (StaleElementReferenceException e) {
} catch (StaleElementReferenceException | NoSuchElementException e) {
error = e;
return false;
}
Expand Down Expand Up @@ -481,7 +481,7 @@ public static ExpectedCondition<Boolean> textToBePresentInElementValue(

return new ExpectedCondition<>() {
private @Nullable String actualValue;
private @Nullable StaleElementReferenceException error;
private @Nullable WebDriverException error;

@Override
public Boolean apply(WebDriver driver) {
Expand All @@ -490,7 +490,7 @@ public Boolean apply(WebDriver driver) {
try {
actualValue = element.getAttribute("value");
return actualValue != null && actualValue.contains(expectedValue);
} catch (StaleElementReferenceException e) {
} catch (StaleElementReferenceException | NoSuchElementException e) {
error = e;
return false;
}
Expand Down Expand Up @@ -825,7 +825,7 @@ public Boolean apply(WebDriver ignored) {
// Calling any method forces a staleness check
element.isEnabled();
return false;
} catch (StaleElementReferenceException expected) {
} catch (StaleElementReferenceException | NoSuchElementException expected) {
return true;
}
}
Expand Down Expand Up @@ -855,7 +855,7 @@ public String toString() {
public @Nullable T apply(WebDriver driver) {
try {
return condition.apply(driver);
} catch (StaleElementReferenceException e) {
} catch (StaleElementReferenceException | NoSuchElementException e) {
return null;
}
}
Expand Down Expand Up @@ -1677,15 +1677,21 @@ private static boolean isInvisible(final WebElement element) {
*/
public static ExpectedCondition<Boolean> or(final ExpectedCondition<?>... conditions) {
return new ExpectedCondition<>() {
@Nullable final Object[] results = new Object[conditions.length];

@Override
public Boolean apply(WebDriver driver) {
for (ExpectedCondition<?> condition : conditions) {
for (int i = 0; i != conditions.length; ++i) {
ExpectedCondition<?> condition = conditions[i];
results[i] = null;
try {
Object result = condition.apply(driver);
if (Boolean.TRUE.equals(result) || result != null && !(result instanceof Boolean)) {
return true;
}
} catch (StaleElementReferenceException ignore) {
results[i] = result;
} catch (RuntimeException e) {
results[i] = e;
}
}
return false;
Expand All @@ -1696,10 +1702,23 @@ public String toString() {
StringBuilder message =
new StringBuilder("at least one condition to be valid:").append(lineSeparator());
for (int i = 0; i < conditions.length; i++) {
message.append(i + 1).append(". ").append(conditions[i]).append(lineSeparator());
message
.append(i + 1)
.append(". ")
.append(conditions[i])
.append(resultAsString(results[i]))
.append(lineSeparator());
}
return message.toString();
}

private Object resultAsString(@Nullable Object result) {
return Boolean.FALSE.equals(result)
? ""
: result instanceof WebDriverException
? " (caused by: " + shortDescription((WebDriverException) result) + ")"
: " (actual: " + result + ")";
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,7 @@ void whenOneThrows() {
.thenReturn("16pt")
.thenReturn("17pt")
.thenReturn("18pt");
when(mockElement.getText())
.thenThrow(new StaleElementReferenceException("Element disappeared"));
when(mockElement.getText()).thenThrow(new NoSuchElementException("Element disappeared"));

assertThat(
wait.until(
Expand All @@ -981,7 +980,7 @@ void whenOneThrows() {
void whenAllThrow() {
String attributeName = "test";
when(mockElement.getAttribute(attributeName))
.thenThrow(new StaleElementReferenceException("Disappeared 1"));
.thenThrow(new NoSuchElementException("Disappeared 1"));
when(mockElement.getCssValue(attributeName))
.thenThrow(new StaleElementReferenceException("Disappeared 2"));
when(mockElement.getText()).thenThrow(new StaleElementReferenceException("Disappeared 3"));
Expand All @@ -999,7 +998,8 @@ void whenAllThrow() {
+ "1. element to have text \"test\", but..."
+ " org.openqa.selenium.StaleElementReferenceException: Disappeared 3."
+ lineSeparator()
+ "2. attribute or CSS value \"test\"=\"test\". Current value: \"null\".")
+ "2. attribute or CSS value \"test\"=\"test\". Current value: \"null\". (caused"
+ " by: org.openqa.selenium.NoSuchElementException: Disappeared 1)")
.hasMessageContaining("tried for 1.1 seconds with 250 milliseconds interval");
}
}
Expand Down