I'm just getting started with Espresso for testing Android apps, but I'm having some trouble. I have an Activity with a button that replaces a fragment in the usual way:
public void onClick(View v) {
final FragmentTransaction t = getFragmentManager().beginTransaction();
t.setCustomAnimations(R.animator.fragment_slide_in_up, 0);
t.replace(R.id.fragment_container,
LogInFragment.newInstance(), LogInFragment.TAG)
.addToBackStack(LogInFragment.TAG);
t.commit();
}
In my test, I click the button that sends in the new fragment, and then just check that the new fragment is visible.
onView(withId(R.id.login_btn))
.perform(click());
Thread.sleep(500);
onView(withId(R.id.email_input))
.check(matches(isDisplayed()));
If I remove the Thread.sleep()
from the test, the test fails, even if I remove the setCustomAnimations()
from the Activity code.
All animations are turned off on my phone, as per the instructions. My understanding is that Espresso knows about the UI thread status and will wait until everything is ready. But if I do a onView(withId(R.id.widgetOnNewFragment))
it blows up every time. I need to add a Thread.sleep(500)
every time I show a new fragment.
Am I missing something? I think I should not have to add dozens of Thread.sleep() to my test code.