在 Espresso 中,我可以检测视图是否包含文本 * 或 * 其他文本
In Espresso can I detect if view contains text *or* other text
我有一个回收站视图,其中应包含顺序未知的 3 个项目。
我知道如何按位置获取回收物品以及如何检查文本
onView(withId(...)).check(matches(atPosition(0, hasDescendant(withText(A)))));
但是我不知道怎么说withText(A)
或者withText(B)
我用谷歌搜索,但看不到任何类似 OR hamcrest 匹配器的东西
您可以使用 Matchers.anyOf()
。例如:
onView(anyOf(withText("X"), withText("Y"))).check(matches(isDisplayed()))
创建您自己的自定义 matcher
:
class MultiTextMatcher extends BoundedMatcher<View, TextView> {
static MultiTextMatcher withTexts(String[] texts) {
return new MultiTextMatcher(texts);
}
private final String[] texts;
private MultiTextMatcher(String[] texts) {
super(TextView.class);
this.texts = texts;
}
@Override protected boolean matchesSafely(TextView item) {
for(String text: texts) {
if(item.getText().toString().equals(text)) {
return true;
}
}
return false;
}
@Override public void describeTo(Description description) {
description.appendText("with texts:").appendValue(texts.toString());
}
}
然后,按如下方式应用您的自定义 matcher
:
onView(withId(...)).check(matches(atPosition(0, hasDescendant(withTexts({"A", "B"})))));
我有一个回收站视图,其中应包含顺序未知的 3 个项目。
我知道如何按位置获取回收物品以及如何检查文本
onView(withId(...)).check(matches(atPosition(0, hasDescendant(withText(A)))));
但是我不知道怎么说withText(A)
或者withText(B)
我用谷歌搜索,但看不到任何类似 OR hamcrest 匹配器的东西
您可以使用 Matchers.anyOf()
。例如:
onView(anyOf(withText("X"), withText("Y"))).check(matches(isDisplayed()))
创建您自己的自定义 matcher
:
class MultiTextMatcher extends BoundedMatcher<View, TextView> {
static MultiTextMatcher withTexts(String[] texts) {
return new MultiTextMatcher(texts);
}
private final String[] texts;
private MultiTextMatcher(String[] texts) {
super(TextView.class);
this.texts = texts;
}
@Override protected boolean matchesSafely(TextView item) {
for(String text: texts) {
if(item.getText().toString().equals(text)) {
return true;
}
}
return false;
}
@Override public void describeTo(Description description) {
description.appendText("with texts:").appendValue(texts.toString());
}
}
然后,按如下方式应用您的自定义 matcher
:
onView(withId(...)).check(matches(atPosition(0, hasDescendant(withTexts({"A", "B"})))));