我有自定义布局的微调器:
<?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"
androidrientation="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
}
}
Best Answer-推荐答案 strong>
我也使用自定义匹配器进行了归档。但我使用了一个不那么冗长的匹配器,也许它可以帮助某人:
onView(withId(R.id.spinner)).perform(click());
onData(allOf(is(instanceOf(YourCustomClass.class)), withMyValue("Open"))).perform(click());
public static <T> Matcher<T> withMyValue(final String name) {
return new BaseMatcher<T>() {
@Override
public boolean matches(Object item) {
return item.toString().equals(name);
}
@Override
public void describeTo(Description description) {
}
};
}
那么你必须在你的自定义类上重写 toString() 方法。
关于安卓 Espresso : How to match text on custom spinner selected item layout?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/34581576/
|