LoginSignup
7
5

More than 3 years have passed since last update.

[Espresso]AdapterViewの子要素の確認方法まとめ

Last updated at Posted at 2016-06-24

毎回忘れてググる羽目になるので自分用メモ。

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()));
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5