Android: Espresso doesn't wait until fragment or activity is shown so every test failed

Thomas Cirksena picture Thomas Cirksena · Feb 17, 2016 · Viewed 10.1k times · Source

I know there are a lot of questions around this but i can't find anything to help me :(

I tried to use Espresso for generate UI-Test for an android app. After beating dependency issues (because some libraries are included two times in different versions as dependency from other libraries) I'm still not able to create working tests...

I know about IdlingResource but as I read, espresso waits out of the box till the main-thread and AsyncTaskPool is idle before running any tests.

For getting in touch with it I crated a simple app containing two activities, a splashscreen and a mainactivity. The splashscreen contains something like this for waiting for three sec before starting mainActivity

 new Handler().postDelayed(new Runnable() {
   @Override
   public void run() {
     finish();
     Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
     startActivity(intent);
   }
 }, 3000);

I also tried to use AsyncTask for this but nothing seems to work correctly because every test fails.

The test only checks if a TextView containing a defined text is shown on the view by:

onView(withText("DummyText")).check(matches(notNullValue()));

I thought it wouldn't be that difficult to create a simple test...

Because I've to test against api>=16 I need espresso. Just for me I checked it by using uiAutomator (I know it's for api >=18) but there are that smart logic to wait for something to be shown.....and that works perfectly...

Answer

sunlover3 picture sunlover3 · Feb 18, 2016

What is your activityRule? Because if it's SplashScreenActivity, the test will start immediately and the onView will fail if the view is not on splash screen. Try to open directly the MainActivity, in order to test that view. I am using Espresso for some time and I know that the @Test methods start when the activity defined as ActivityTestRule has finished loading.

Hope this helped.

Good luck!