【问题标题】:How to let Espresso click a specific RecyclerView item?如何让 Espresso 单击特定的 RecyclerView 项目?
【发布时间】:2016-11-01 18:12:03
【问题描述】:

我正在尝试使用 Espresso 为我使用 RecyclerView 的 Android 应用程序编写仪器测试。这是主要布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>

...和项目视图布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</RelativeLayout>

这意味着屏幕上有几个TextViews,它们都有label id。我正在尝试单击特定标签。我试着让 Espresso 测试记录器生成代码:

onView(allOf(withId(R.id.grid), isDisplayed()))
    .perform(actionOnItemAtPosition(5, click()));

我宁愿做这样的事情:

// Not working
onView(allOf(withId(R.id.grid), isDisplayed()))
    .perform(actionOnItem(withText("Foobar"), click()));

读数

我阅读了很多关于该主题的内容,但仍然无法弄清楚如何根据其内容测试RecyclerView 项目不是基于其位置索引。

【问题讨论】:

    标签: android android-recyclerview android-espresso android-espresso-recorder


    【解决方案1】:

    您可以实现您的自定义 RecyclerView 匹配器。假设您有RecyclerView,其中每个元素都有您要匹配的主题:

    public static Matcher<RecyclerView.ViewHolder> withItemSubject(final String subject) {
        Checks.checkNotNull(subject);
        return new BoundedMatcher<RecyclerView.ViewHolder, MyCustomViewHolder>(
                MyCustomViewHolder.class) {
    
            @Override
            protected boolean matchesSafely(MyCustomViewHolder viewHolder) {
                TextView subjectTextView = (TextView)viewHolder.itemView.findViewById(R.id.subject_text_view_id);
    
                return ((subject.equals(subjectTextView.getText().toString())
                        && (subjectTextView.getVisibility() == View.VISIBLE)));
            }
    
            @Override
            public void describeTo(Description description) {
                description.appendText("item with subject: " + subject);
            }
        };
    }
    

    及用法:

    onView(withId(R.id.my_recycler_view_id)
        .perform(RecyclerViewActions.actionOnHolderItem(withItemSubject("My subject"), click()));
    

    基本上你可以匹配任何你想要的东西。在此示例中,我们使用了主题 TextView,但它可以是 RecyclerView 项目内的任何元素。

    要澄清的另一件事是检查可见性(subjectTextView.getVisibility() == View.VISIBLE)。我们需要它,因为有时RecyclerView 中的其他视图可以具有相同的主题,但它会与View.GONE 一起使用。通过这种方式,我们避免了自定义匹配器的多次匹配,并且只针对实际显示我们主题的项目。

    【讨论】:

    • 有谁知道appendText的作用是什么?
    • 通过这种方式,您可以自定义错误描述以防失败。
    【解决方案2】:

    actionOnItem() 匹配 ViewHolder 的 itemView。在这种情况下,TextView 被包裹在 RelativeLayout 中,因此您需要更新 Matcher 来解决它。

    onView(allOf(withId(R.id.grid), isDisplayed()))
        .perform(actionOnItem(withChild(withText("Foobar")), click()));
    

    你也可以使用hasDescendant()来包裹你的匹配器是一个更复杂的嵌套情况。

    【讨论】:

    • 不管视图嵌套多深,hasDescendant() 是否会自动遍历层级?还是我必须使用hasDescendant(hasDescendant(...))
    • 是的,hasDescendant() 将从 onView() 部分中的根处理整个视图层次结构。在 recyclerview 操作的情况下,根是 itemView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多