Skip to content Skip to sidebar Skip to footer

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 int

Solution 1:

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.

Solution 2:

Got the same error message Error performing 'load adapter data' on view, none of the answers from these posts worked for me.

Testing RecyclerView if it has data with EspressoEspresso onData Error performing 'load adapter data' on viewEspresso. Error performing 'load adapter data'

I ended up using the Espresso UI test recorder in Android Studio. Go to Run from the top drop down menu, then click Record Espresso Test. It will ask you to pick a device to run on, once the app is launched, manually do the ui tests you like to perform and add assertions when needed. Hit OK when done. It will generate a UI test file for you.

The code generated for clicking on an item on a RecyclerView looks like this. The heavy lifting here is the Matcher method childAtPosition() At the beginning of the test, it sleeps for 10 seconds to make sure everything are loaded, usually it doesn't take 10 seconds, you can reduce it to probably 2 seconds. An alternative is to use Espresso IdlingResource like @piotrek1543 has suggested, but that requires to add interventions(mixing test specific codes) in the production code to accommodate the testing.

@LargeTest@RunWith(AndroidJUnit4.class)publicclassMainActivityTest {

    @Rulepublic ActivityTestRule<MainActivity> mActivityTestRule = newActivityTestRule<>(MainActivity.class);

    @TestpublicvoidmainActivityTest() {
        // Added a sleep statement to match the app's execution delay.// The recommended way to handle such scenarios is to use Espresso idling resources:// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.htmltry {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteractionrecyclerView= onView(
                allOf(withId(R.id.recycler_view_list),
                        childAtPosition(
                                withClassName(is("android.support.constraint.ConstraintLayout")),
                                0)));
        recyclerView.perform(actionOnItemAtPosition(0, click()));
    }

    privatestatic Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, finalint position) {

        returnnewTypeSafeMatcher<View>() {
            @OverridepublicvoiddescribeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @OverridepublicbooleanmatchesSafely(View view) {
                ViewParentparent= view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

Note: The generated code by the Espresso UI test recorder is not perfect either, it works for the most of the time but not 100%

Post a Comment for "Android.support.test.espresso.performexception: Error Performing 'load Adapter Data' On View"