@@ -399,7 +399,7 @@ public static ExpectedCondition<Boolean> textToBePresentInElement(
399399
400400 return new ExpectedCondition <>() {
401401 private @ Nullable String elementText ;
402- private @ Nullable StaleElementReferenceException error ;
402+ private @ Nullable WebDriverException error ;
403403
404404 @ Override
405405 public Boolean apply (WebDriver driver ) {
@@ -408,7 +408,7 @@ public Boolean apply(WebDriver driver) {
408408 try {
409409 elementText = element .getText ();
410410 return elementText .contains (text );
411- } catch (StaleElementReferenceException e ) {
411+ } catch (StaleElementReferenceException | NoSuchElementException e ) {
412412 error = e ;
413413 return false ;
414414 }
@@ -481,7 +481,7 @@ public static ExpectedCondition<Boolean> textToBePresentInElementValue(
481481
482482 return new ExpectedCondition <>() {
483483 private @ Nullable String actualValue ;
484- private @ Nullable StaleElementReferenceException error ;
484+ private @ Nullable WebDriverException error ;
485485
486486 @ Override
487487 public Boolean apply (WebDriver driver ) {
@@ -490,7 +490,7 @@ public Boolean apply(WebDriver driver) {
490490 try {
491491 actualValue = element .getAttribute ("value" );
492492 return actualValue != null && actualValue .contains (expectedValue );
493- } catch (StaleElementReferenceException e ) {
493+ } catch (StaleElementReferenceException | NoSuchElementException e ) {
494494 error = e ;
495495 return false ;
496496 }
@@ -825,7 +825,7 @@ public Boolean apply(WebDriver ignored) {
825825 // Calling any method forces a staleness check
826826 element .isEnabled ();
827827 return false ;
828- } catch (StaleElementReferenceException expected ) {
828+ } catch (StaleElementReferenceException | NoSuchElementException expected ) {
829829 return true ;
830830 }
831831 }
@@ -855,7 +855,7 @@ public String toString() {
855855 public @ Nullable T apply (WebDriver driver ) {
856856 try {
857857 return condition .apply (driver );
858- } catch (StaleElementReferenceException e ) {
858+ } catch (StaleElementReferenceException | NoSuchElementException e ) {
859859 return null ;
860860 }
861861 }
@@ -1677,15 +1677,21 @@ private static boolean isInvisible(final WebElement element) {
16771677 */
16781678 public static ExpectedCondition <Boolean > or (final ExpectedCondition <?>... conditions ) {
16791679 return new ExpectedCondition <>() {
1680+ @ Nullable final Object [] results = new Object [conditions .length ];
1681+
16801682 @ Override
16811683 public Boolean apply (WebDriver driver ) {
1682- for (ExpectedCondition <?> condition : conditions ) {
1684+ for (int i = 0 ; i != conditions .length ; ++i ) {
1685+ ExpectedCondition <?> condition = conditions [i ];
1686+ results [i ] = null ;
16831687 try {
16841688 Object result = condition .apply (driver );
16851689 if (Boolean .TRUE .equals (result ) || result != null && !(result instanceof Boolean )) {
16861690 return true ;
16871691 }
1688- } catch (StaleElementReferenceException ignore ) {
1692+ results [i ] = result ;
1693+ } catch (RuntimeException e ) {
1694+ results [i ] = e ;
16891695 }
16901696 }
16911697 return false ;
@@ -1696,10 +1702,23 @@ public String toString() {
16961702 StringBuilder message =
16971703 new StringBuilder ("at least one condition to be valid:" ).append (lineSeparator ());
16981704 for (int i = 0 ; i < conditions .length ; i ++) {
1699- message .append (i + 1 ).append (". " ).append (conditions [i ]).append (lineSeparator ());
1705+ message
1706+ .append (i + 1 )
1707+ .append (". " )
1708+ .append (conditions [i ])
1709+ .append (resultAsString (results [i ]))
1710+ .append (lineSeparator ());
17001711 }
17011712 return message .toString ();
17021713 }
1714+
1715+ private Object resultAsString (@ Nullable Object result ) {
1716+ return Boolean .FALSE .equals (result )
1717+ ? ""
1718+ : result instanceof WebDriverException
1719+ ? " (caused by: " + shortDescription ((WebDriverException ) result ) + ")"
1720+ : " (actual: " + result + ")" ;
1721+ }
17031722 };
17041723 }
17051724
0 commit comments