Search code examples
androidlistviewandroid-espressolistadapterandroid-espresso-recorder

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view


I am using Espresso to test a list view that appears when I am searching for an item (like an autocomplete). The list view does not appear until the user has typed in something into the SearchView. i.e. I set the ListView to View.VISIBLE only when the user has typed something into the SearchView

I am getting this error when I try to click on text in a list view. android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id:'. Using onData did not work.

Adding an artificial delay works, but I am unsure if this is bad practice since it seems to defeat the purpose of methods such as onData etc.

What I've tried:

My Code

This code works, but I would prefer not to have to introduce an artificial delay.

public pickSuggestion(int index){

    /** artificial delay to allow list to appear. 
    This works but I shouldn't have to do this right? **/

    SystemClock.sleep(1000);

    onData(anything())
        .inAdapterView(withId(R.id.list))
        .atPosition(index)
        .onChildView(withId(R.id.mTextView))
        .perform(click());
}

Solution

  • Adding an artificial delay works, but I am unsure if this is bad practice since it seems to defeat the purpose of methods such as onData etc.

    Your error is provided with Espresso limitation. This framework need to run on UI thread and it 'waits' until it would be idle. It doesn't wait for loading adapter data, but waits for get idling resource

    Check: http://dev.jimdo.com/2014/05/09/wait-for-it-a-deep-dive-into-espresso-s-idling-resources/

    IdlingResource Reference: https://developer.android.com/reference/android/support/test/espresso/IdlingResource.html

    IdlingResource Documentation: https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html

    CountingIdlingResource: https://developer.android.com/reference/android/support/test/espresso/idling/CountingIdlingResource.html

    Code like SystemClock.sleep(1000) or Thread.sleep(1000) is a bad practice because better devices don't need this amount of time and older ones need more, so your code maybe flaky, rather than fast and flexible.

    The solution is to create your own Espresso IdlingResource to tell Espresso when it can perform tests without losing data and time.

    Hope this will help.