浓缩咖啡计数元素

8
有没有办法在Espresso中计算具有特定ID的元素?
我可以使用 onView(withId(R.id.my_id)),但是之后我就卡住了。
我有一个LinearLayout,我要插入元素(不是ListView),我想测试有多少个这些元素来检查它们是否符合预期行为。
3个回答

4

这是我想出来的匹配器:

public static Matcher<View> withViewCount(final Matcher<View> viewMatcher, final int expectedCount) {
        return new TypeSafeMatcher<View>() {
            int actualCount = -1;

            @Override
            public void describeTo(Description description) {
                if (actualCount >= 0) {
                    description.appendText("With expected number of items: " + expectedCount);
                    description.appendText("\n With matcher: ");
                    viewMatcher.describeTo(description);
                    description.appendText("\n But got: " + actualCount);
                }
            }

            @Override
            protected boolean matchesSafely(View root) {
                actualCount = 0;
                Iterable<View> iterable = TreeIterables.breadthFirstViewTraversal(root);
                actualCount = Iterables.size(Iterables.filter(iterable, withMatcherPredicate(viewMatcher)));
                return actualCount == expectedCount;
            }
        };
    }

    private static Predicate<View> withMatcherPredicate(final Matcher<View> matcher) {
        return new Predicate<View>() {
            @Override
            public boolean apply(@Nullable View view) {
                return matcher.matches(view);
            }
        };
    }

用法如下:

onView(isRoot()).check(matches(withViewCount(withId(R.id.anything), 5)));

2
我在升级到Espresso 3.0.0后遇到了Iterables的问题。导入从android.support.test.espresso.core.deps.guava.collect.Iterables;更改为android.support.test.espresso.core.internal.deps.guava.collect.Iterables;,这样做失去了此代码所需的.size。 - Zeek Aran
使用 Iterables.filter(iterable, withMatcherPredicate(viewMatcher)).count() 怎么样? - Sushant
1
最终还是使用了这个:= Lists.newArrayList(Iterables.filter(iterable, withMatcherPredicate(viewMatcher))).size 还有,来自2020年的问候! - Zeek Aran

2

这里有一个关于 Be_Negative 回答的Java和Kotlin版本:

声明:

fun withViewCount(viewMatcher: Matcher<View>, expectedCount: Int): Matcher<View?> {
return object : TypeSafeMatcher<View?>() {
    private var actualCount = -1
    override fun describeTo(description: Description) {
        when {
            actualCount >= 0 -> description.also {
                it.appendText("Expected items count: $expectedCount, but got: $actualCount")
            }
        }
    }

    override fun matchesSafely(root: View?): Boolean {
        actualCount = TreeIterables.breadthFirstViewTraversal(root).count {
            viewMatcher.matches(it)
        }
        return expectedCount == actualCount
    }
}

例如在测试中使用,检查RadioGroup中的RadioButtons数量:

 onView(withId(R.id.radioGroup)).check(
     matches(
         withViewCount(instanceOf(RadioButton::class.java), 3)
     )
 )

0

这可以通过自定义匹配器来实现。您可以在 Kotlin 中按照以下方式定义一个:

fun withChildViewCount(count: Int, childMatcher: Matcher<View>): Matcher<View> {

    return object : BoundedMatcher<View, ViewGroup>(ViewGroup::class.java) {
        override fun matchesSafely(viewGroup: ViewGroup): Boolean {
            val matchCount = viewGroup.children
                    .filter { childMatcher.matches(it) }
                    .count()
            return matchCount == count
        }

        override fun describeTo(description: Description) {
            description.appendText("with child count $count")
        }

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接