android–Espresso计数元素

android–Espresso计数元素,第1张

概述有没有办法在Espresso中计算具有特定ID的元素?我可以做onView(withId(R.id.my_id)),但后来我被卡住了.我有一个LinearLayout,我注入元素(不是ListView),我想测试多少或那些是检查它们是否符合预期的行为.解决方法:这是我想出的匹配器:publicstaticMatcher<View>withViewCount(

有没有办法在Espresso中计算具有特定ID的元素?

我可以做onVIEw(withID(R.ID.my_ID)),但后来我被卡住了.

我有一个linearLayout,我注入元素(不是ListVIEw),我想测试多少或那些是检查它们是否符合预期的行为.

解决方法:

这是我想出的匹配器:

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)));
总结

以上是内存溢出为你收集整理的android – Espresso计数元素全部内容,希望文章能够帮你解决android – Espresso计数元素所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1118376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存