Reputation: 6490
I am confused that Espresso seems to be a little inconsistent when handling failures.
What I'm trying to achieve is a retry check for a certain string to be displayed. A button click kicks off an async operation that should complete within some time frame, and I could successfully put a try catch block around a check whether some element exists or not:
try {
onView(withId(fieldIdToBeAvailable))
.inRoot(isDialog())
.check(matches(isDisplayed()));
}
catch (NoMatchingViewException nmve) {
// ok, retry this or fail
retry();
}
which would throw an NoMatchingViewException
and retry this with some sort of loop, but Espresso does not throw an exception if I try to check for a matching text like:
onView(withId(R.id.sync_status))
.check(matches(withText("Logged in")));
The only thing that happens is an android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError:
How would someone write a retry concept around that? When I put a failureHandler in place I cannot throw out of it (so I would catch the exception instead of the evaluation of the AssertionFailed error) since handle
does not allow a throw:
onView(withId(R.id.sync_status)).withFailureHandler(new FailureHandler()
{
@Override
public void handle(Throwable error, Matcher<View> viewMatcher)
{
// Compiler error: Unhandled exception
throw new Exception("Failed, try again");
}
}).check(matches(withText("Logged in")));
Upvotes: 0
Views: 1097
Reputation: 1413
I suggest to use idling resources to handle async ops https://developer.android.com/training/testing/espresso/idling-resource.html
Anyway, Exception
is a checked exception so it should be wrapped with try-catch or method should define that exception with throws
keyword.
To 'fix' your compiler error you may use RuntimeException
or Error
Upvotes: 1