问题描述
我正在尝试编写一个espresso函数以匹配espresso根据我的函数找到的第一个元素,即使找到了多个匹配项也是如此.
I'm trying to write an espresso function to match the first element espresso finds according to my function, even when multiple matching items are found.
例如:我有一个列表视图,其中包含包含项目价格的单元格.我希望能够将货币切换为加拿大元,并确认商品价格为加元.
Ex:I have a list view with cells which contain item price. I want to be able to switch the currency to Canadian dollars and verify item prices are in CAD.
我正在使用此功能:
onView(anyOf(withId(R.id.product_price), withText(endsWith("CAD"))))
.check(matches(
isDisplayed()));
这将引发AmbiguousViewMatcherException.
This throws the AmbiguousViewMatcherException.
在这种情况下,我不在乎有多少个单元格显示CAD,我只想验证它是否显示.一旦遇到符合参数的对象,有没有办法使浓缩咖啡通过此测试?
In this case, I don't care how many or few cells display CAD, I just want to verify it is displayed. Is there way to make espresso pass this test as soon as it encounters an object meeting the parameters?
推荐答案
您应该能够使用以下代码创建仅与第一项匹配的自定义匹配器:
You should be able to create a custom matcher that only matches on the first item with the following code:
private <T> Matcher<T> first(final Matcher<T> matcher) {
return new BaseMatcher<T>() {
boolean isFirst = true;
@Override
public boolean matches(final Object item) {
if (isFirst && matcher.matches(item)) {
isFirst = false;
return true;
}
return false;
}
@Override
public void describeTo(final Description description) {
description.appendText("should return first matching item");
}
};
}
这篇关于当多个处于层次结构中时,Espresso匹配找到的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!