Prabin Timsina
Prabin Timsina

Reputation: 2182

Espresso Android matching textviews inside parent

I have a simple linear layout with two textviews inside it. The linear layout has an unique id but the textviews don't have. How do I verify one of the texts is from those textviews?

I tried the following code:

 onView(allOf(NavigationDrawerComponent.topSectionWrapper,
                hasSibling(withClassName(Matchers.equalTo(TextView.class.getSimpleName())))))
                .check(matches(withText(Data.fullUserName)));

Unfortunately,its not working for me.I am getting the following error:

   android.support.test.espresso.NoMatchingViewException: No views in
    hierarchy found matching

Upvotes: 4

Views: 3592

Answers (2)

Ali Kazi
Ali Kazi

Reputation: 1636

Sometimes you may want to scroll the RecyclerView until you find the right child view (if the view is not visible, it is probably not yet created). In that case using scrollTo is not enough and Android Studio will not recommend a better choice automatically. You have to do this:

 onView(withId(R.id.recycler_view))
 .perform(RecyclerViewActions.scrollTo(hasDescendant(withText("Available"))));

Upvotes: 0

Prabin Timsina
Prabin Timsina

Reputation: 2182

Use hasDescendant()

onView(withId(R.id.recycler_view)).check(matches(atPosition(0, hasDescendant(withText("Available")))));

Upvotes: 4

Related Questions