毎回忘れてググる羽目になるので自分用メモ。
ListView,GridView
Espresso.onData(anything())
.inAdapterView(withId(R.id.gridView))
.atPosition(index)
.onChildView(withId(R.id.img_child))
.check(matches(isDisplayed()))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
.perform(click())
※ListView/GridViewがTOP要素なら、atPositionに指定したビューが見えるまで自動でスクロールしてくれるが、ScrollView内に含めるような実装方法にしていると、自分でスクロールさせておく必要がある。
RecyclerView
check
onView(withId(R.id.list))
.perform(RecyclerViewActions.scrollToPosition(index))
.check(matches(atPositionOnView(index,
withText("タイトル"), R.id.text_title_name)))
.check(matches(atPositionOnView(index ,
withText("¥300"), R.id.text_price)))
.check(matches(atPositionOnView(index,
withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE),
R.id.text_purchased)));
public static Matcher<View> atPositionOnView(final int position, final Matcher<View> itemMatcher,
@NonNull final int targetViewId) {
return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
@Override
public void describeTo(Description description) {
description.appendText("has view id " + itemMatcher + " at position " + position);
}
@Override
public boolean matchesSafely(final RecyclerView recyclerView) {
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
View targetView = viewHolder.itemView.findViewById(targetViewId);
return itemMatcher.matches(targetView);
}
};
}
perform
onView(withId(R.id.list)).perform(
RecyclerViewActions.actionOnItemAtPosition(index + 1, click()));