其他分享
首页 > 其他分享> > Android Espresso:如何在自定义微调器选择的项目布局上匹配文本?

Android Espresso:如何在自定义微调器选择的项目布局上匹配文本?

作者:互联网

我有带有自定义布局的微调器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <TextView
        android:id="@+id/tv_text"
        android:textColor="#FFFFFF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

我需要检查所选项目的R.id.tv_text中的值是否与特定文本匹配.
是否可以在不实现自定义Matcher类的情况下做到这一点?

我的简单测试:

@SmallTest
public class CategoryTest {

    public static final String TEXT_TO_MATCH = "Spinner item text";

    @Test
    public void testCreate() {
        ...
        onView(withId(R.id.spn_tt)).perform(click()); // open spinner
        onView(allOf(withId(R.id.tv_text), withText(TEXT_TO_MATCH))).perform(click()); // find item by text and click
        onView(withId(R.id.spn_tt)).check(matches(withTextInSpinnerSelectedView(TEXT_TO_MATCH))); // check if right item selected by text
    }

}

解决方法:

除了创建在Spinner选定项目的任何TextView(或其继承人)中查找特定文本的自定义匹配器之外,我没有找到更简单的方法.我有一个自定义匹配器:https://gist.github.com/igorokr/5f3db37f0b9eb8b2feae

标签:android-espresso,android-spinner,android-testing,android
来源: https://codeday.me/bug/20191027/1945063.html