Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 780
Thomas Cirksena Avatar asked Feb 17 '16 10:02

Thomas Cirksena


1 Answers

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!

like image 72
sunlover3 Avatar answered Nov 15 '22 15:11

sunlover3