Espresso - 检查 RecyclerView 项目是否正确排序

Espresso - Check RecyclerView items are ordered correctly

如何使用 Espresso 检查 RecyclerView 项目是否以正确的顺序显示?我正在尝试通过文本检查每个元素的标题来测试它。

当我尝试这段代码时,它可以点击元素,但不能继续,而不是执行点击尝试断言元素的文本

onView(withId(R.id.rv_metrics)).perform(actionOnItemAtPosition(0, click()));

当我尝试使用自定义匹配器时,我不断收到错误

Error performing 'load adapter data' on view 'with id: mypackage_name:id/rv_metrics'

我现在知道了 onData doesn't work for RecyclerView 但在此之前我曾尝试使用自定义匹配器来完成此任务。

 public static Matcher<Object> hasTitle(final String inputString) {
    return new BoundedMatcher<Object, Metric>(Metric.class) {
        @Override
        protected boolean matchesSafely(Metric metric) {

            return inputString.equals(metric.getMetric());

        }

        @Override
        public void describeTo(org.hamcrest.Description description) {
            description.appendText("with title: ");
        }
    };
}

我也尝试过类似的方法,但由于作为 actionOnItemAtPosition 方法的参数给出的类型,它显然不起作用,但我们是否有类似的方法可能会起作用?

onView(withId(R.id.rv_metrics)).check(actionOnItemAtPosition(0, ViewAssertions.matches(withText("Weight"))));

请问我在这里遗漏了什么? 非常感谢。

如前所述 hereRecyclerView 对象与 AdapterView 对象的工作方式不同,因此 onData() 不能用于与它们交互。

为了在 RecyclerView 的特定位置找到视图,您需要实现自定义 RecyclerViewMatcher,如下所示:

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)",
                                                      new Object[] { Integer.valueOf
                                                          (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;
                }

            }
        };
    }
}

然后以这种方式在您的测试用例中使用它:

@Test
void testCase() {
    onView(new RecyclerViewMatcher(R.id.rv_metrics)
        .atPositionOnView(0, R.id.txt_title))
        .check(matches(withText("Weight")))
        .perform(click());

    onView(new RecyclerViewMatcher(R.id.rv_metrics)
        .atPositionOnView(1, R.id.txt_title))
        .check(matches(withText("Height")))
        .perform(click());
}

如果你想在 RecyclerView 中的某个位置匹配匹配器,那么你可以尝试创建自定义 Matcher<View>:

public static Matcher<View> hasItemAtPosition(int position, Matcher<View> matcher) {
    return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {

        @Override public void describeTo(Description description) {
            description.appendText("has item: ");
            matcher.describeTo(description);
            description.appendText(" at position: " + position);
        }

        @Override protected boolean matchesSafely(RecyclerView view) {
            RecyclerView.Adapter adapter = view.getAdapter();
            int type = adapter.getItemViewType(position);
            RecyclerView.ViewHolder holder = adapter.createViewHolder(view, type);
            adapter.onBindViewHolder(holder, position);
            return matcher.matches(holder.itemView);
        }
    };
}

您可以使用它,例如:

onView(withId(R.id.rv_metrics)).check(matches(0, hasDescendant(withText("Weight")))))

我简化了一点 Mosius 的回答:

public static Matcher<View> hasItemAtPosition(final Matcher<View> matcher, final int position) {
    return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("has item at position " + position + ": ");
            matcher.describeTo(description);
        }
        @Override
        protected boolean matchesSafely(RecyclerView recyclerView) {
            RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
            return matcher.matches(viewHolder.itemView);
        }
    };
}

我们将 Matcher 传递给该函数,以便我们可以提供更多条件。用法示例:

onView(hasItemAtPosition(hasDescendant(withText("Item 1")), 0)).check(matches(isDisplayed()));
onView(hasItemAtPosition(hasDescendant(withText("Item 2")), 1)).check(matches(isDisplayed()));

原来的问题已经解决了,但我在这里发布了一个答案,因为发现 Barista 库在一个 单行代码中解决了这个问题.

assertDisplayedAtPosition(R.id.rv_metrics, 0, R.id.tv_title, "weight");

它是在 Espresso 之上制作的,可以找到它的文档 here

希望这可能对某人有所帮助。 :)

如果有人对 Kotlin 版本感兴趣,请看这里

fun hasItemAtPosition(position: Int, matcher: Matcher<View>) : Matcher<View> {
    return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {

        override fun describeTo(description: Description?) {
            description?.appendText("has item at position $position : ")
            matcher.describeTo(description)
        }

        override fun matchesSafely(item: RecyclerView?): Boolean {
            val viewHolder = item?.findViewHolderForAdapterPosition(position)
            return matcher.matches(viewHolder?.itemView)
        }
    }
}