android - 如何使用 Android Espresso 单击 RecyclerView 中的每个项目

标签 android android-testing android-espresso

有帖子介绍如何在您输入的任何位置单击 RecyclerView 中的某个项目,但是有没有办法单击所有 View ,特别是考虑到我可能不知道会有多少 View 在回收者 View 中。

我想像 in this link 这样获取 RecyclerView 中的项目数但它涉及从 Activity 中获取实际的 RecyclerView,这有时是不可能的。

我还看到了这样一段代码:

public static class RecyclerViewItemCountAssertion implements ViewAssertion { private final int expectedCount;

    public RecyclerViewItemCountAssertion(int expectedCount) {
        this.expectedCount = expectedCount;
    }

    @Override
    public void check(View view, NoMatchingViewException noViewFoundException) {
        if (noViewFoundException != null) {
            throw noViewFoundException;
        }

        RecyclerView recyclerView = (RecyclerView) view;
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        assertThat(adapter.getItemCount(), is(expectedCount));
    }
}

但我不确定如何操作以获得计数

最佳答案

所以在你的测试中很简单:

final View viewById = activityTestRule.getActivity().findViewById(R.id.your_recycler_view_id);
final RecyclerView recyclerView = (RecyclerView) viewById;
final RecyclerView.Adapter adapter = recyclerView.getAdapter();
final int itemsCount = adapter.getItemCount();

因此您可以从 0 迭代到 itemsCount-1

您可以使用此类在位置获取回收器 View 元素(在您的情况下为 0 到 itemsCount-1)

public class RecyclerViewMatcher {
private final int recyclerViewId;

public RecyclerViewMatcher(int recyclerViewId) {
    this.recyclerViewId = recyclerViewId;
}

public Matcher<View> atPosition(final int position) {
    return atPositionOnView(position, -1);
}

public Matcher<View> atPositionOnView(final int position, final int targetViewId) {

    return new TypeSafeMatcher<View>() {
        Resources resources = null;
        View childView;

        public void describeTo(Description description) {
            String idDescription = Integer.toString(recyclerViewId);
            if (this.resources != null) {
                try {
                    idDescription = this.resources.getResourceName(recyclerViewId);
                } catch (Resources.NotFoundException var4) {
                    idDescription = String.format("%s (resource name not found)", recyclerViewId);
                }
            }

            description.appendText("with id: " + idDescription);
        }

        public boolean matchesSafely(View view) {

            this.resources = view.getResources();

            if (childView == null) {
                RecyclerView recyclerView =
                        (RecyclerView) view.getRootView().findViewById(recyclerViewId);
                if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                    childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
                } else {
                    return false;
                }
            }

            if (targetViewId == -1) {
                return view == childView;
            } else {
                View targetView = childView.findViewById(targetViewId);
                return view == targetView;
            }
        }
    };
}}

并像这样使用这个类:

onView(new RecyclerViewMatcher(R.id.your_recycler_view_id)
            .atPositionOnView(0, R.id.your_item_body))
            .perform(click());

因此,您最终可以点击回收站 View 中的所有项目。我希望它会有所帮助。干杯。

关于android - 如何使用 Android Espresso 单击 RecyclerView 中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44533181/

相关文章:

android - 使用android模拟器相机报错100

android - 测试运行失败 : Instrumentation run failed due to 'java.lang.ClassNotFoundException'

selenium - 如何增加 session 超时(默认为 30 分钟)

android-espresso - 安卓。 Espresso : android. content.res.Resources$NotFoundException:字符串资源 ID

安卓 Espresso : Flaky tests due to random ViewNotFoundException

android - onChildView 和 hasSiblings 与 Espresso

android - task.getResult() 抛出 java.lang.ClassCastException

java - Smack XMPP android 连接时崩溃

java - 向 TextView 添加一个字母

AndroidJUnitRunner 单元测试执行起来很慢