I've just started using Espresso, before that I have tried Robotium. I need to test LoginActivity. The logic is:
- User enters correct credentials;
- User sees "Logging in.." string;
- User waits for string to disappear;
- User is in MainActivity and sees "You're logged in"
testLogin source:
public void testLogin() throws Exception{
onView(withId(R.id.login_email)).perform(typeText(LOGIN_EMAIL));
onView(withId(R.id.login_password)).perform(typeText(LOGIN_PASSWORD));
onView(withId(R.id.login_loginBtn)).perform(click());
onView(withText(R.string.loading_logging_in)).check(matches(isDisplayed()));
onView(withText("You're logged in")).check(matches(isDisplayed()));
}
The problem was that espresso didn't wait for "You"re logged in" string to appear, it was trying to find it while logging still was in process.
logcat:
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: is "You're logged in"
I've tried using Thread.sleep(10000), but it terminates the run on Thread.sleep(10000) and gives me error:
Test failed to run to completion. Reason: 'Instrumentation run failed due to 'Process crashed.''. Check device logcat for details Test running failed: Instrumentation run failed due to 'Process crashed.'
logcat:
@@@ @@@ @@@was killed cancelService in HttpReRegistrationService
Before that, I used the waitForActivity(MainActivity.class) method. Is there any workaround to make this work?