1

I am trying to use atPosition in the way I see it used in various answers on StackOverflow:

Espresso.onView(withId(R.id.rv_feeds))
  .check(matches(atPosition(0, hasDescendant(withText("Test text")))));

Android Studio finds all imports needed except for atPosition. Am I perhaps missing a needed gradle import?

    testImplementation "androidx.test.ext:junit-ktx:1.1.3"
    testImplementation "androidx.test:core-ktx:1.4.0"
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation "androidx.test:core:1.4.0"
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.test.espresso:espresso-contrib:3.4.0"
    androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
    androidTestUtil 'androidx.test:orchestrator:1.4.0'

Thanks.

Casey Perkins
  • 1,853
  • 2
  • 23
  • 41

2 Answers2

1

Yup. You're missing:

androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
Okihita
  • 167
  • 1
  • 11
0

Seems like atPosition is an extension method written by someone, not sure if is the right one but I found one such example code at this stackoverflow answer and works well: https://stackoverflow.com/a/34795431/1241783

In case the link goes down, here's the code snippet:

public static Matcher<View> atPosition(final int position, @NonNull final Matcher<View> itemMatcher) {
    checkNotNull(itemMatcher);
    return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has item at position " + position + ": ");
            itemMatcher.describeTo(description);
        }

        @Override
        protected boolean matchesSafely(final RecyclerView view) {
            RecyclerView.ViewHolder viewHolder = view.findViewHolderForAdapterPosition(position);
            if (viewHolder == null) {
                // has no item on such position
                return false;
            }
                return itemMatcher.matches(viewHolder.itemView);
            }
        }
    };
}
Bruce
  • 2,357
  • 5
  • 29
  • 50