首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Espresso元素

Espresso元素
EN

Stack Overflow用户
提问于 2016-09-06 20:47:18
回答 3查看 3.8K关注 0票数 8

在Espresso中有没有一种方法来计算具有特定id的元素?

我可以做onView(withId(R.id.my_id)),但是我被卡住了。

我有一个注入元素(而不是ListView)的LinearLayout,我想测试有多少个元素,以检查它们是否符合预期的行为。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-09-07 21:49:08

这是我想出来的匹配器:

代码语言:javascript
复制
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);
            }
        };
    }

其用法是:

代码语言:javascript
复制
onView(isRoot()).check(matches(withViewCount(withId(R.id.anything), 5)));
票数 4
EN

Stack Overflow用户

发布于 2021-11-12 13:05:29

Be_Negative的答案有一个Kotlin版本:

声明:

代码语言:javascript
复制
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计数:

代码语言:javascript
复制
 onView(withId(R.id.radioGroup)).check(
     matches(
         withViewCount(instanceOf(RadioButton::class.java), 3)
     )
 )
票数 1
EN

Stack Overflow用户

发布于 2020-03-06 18:54:11

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

代码语言:javascript
复制
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")
        }

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39349395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档