[英]How to count RecyclerView items with Espresso
Using Espresso and Hamcrest,使用 Espresso 和 Hamcrest,
How can I count items number available in a recyclerView?如何计算 recyclerView 中可用的项目数?
Exemple: I would like check if 5 items are displaying in a specific RecyclerView (scrolling if necessary).示例:我想检查 5 个项目是否显示在特定的 RecyclerView 中(必要时滚动)。
Here an example ViewAssertion to check RecyclerView item count 这里有一个示例ViewAssertion来检查RecyclerView项目计数
public class RecyclerViewItemCountAssertion implements ViewAssertion {
private final int expectedCount;
public RecyclerViewItemCountAssertion(int expectedCount) {
this.expectedCount = expectedCount;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
assertThat(adapter.getItemCount(), is(expectedCount));
}
}
and then use this assertion 然后使用这个断言
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));
I have started to write an library which should make testing more simple with espresso and uiautomator. 我已经开始编写一个库,应该使用espresso和uiautomator使测试更加简单。 This includes tooling for RecyclerView action and assertions. 这包括RecyclerView操作和断言的工具。 https://github.com/nenick/espresso-macchiato See for example EspRecyclerView with the method assertItemCountIs(int) https://github.com/nenick/espresso-macchiato例如,使用方法assertItemCountIs(int)查看EspRecyclerView
Adding a bit of syntax sugar to the @Stephane's answer . 在@Stephane的答案中添加一些语法糖。
public class RecyclerViewItemCountAssertion implements ViewAssertion {
private final Matcher<Integer> matcher;
public static RecyclerViewItemCountAssertion withItemCount(int expectedCount) {
return withItemCount(is(expectedCount));
}
public static RecyclerViewItemCountAssertion withItemCount(Matcher<Integer> matcher) {
return new RecyclerViewItemCountAssertion(matcher);
}
private RecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
this.matcher = matcher;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
assertThat(adapter.getItemCount(), matcher);
}
}
Usage: 用法:
import static your.package.RecyclerViewItemCountAssertion.withItemCount;
onView(withId(R.id.recyclerView)).check(withItemCount(5));
onView(withId(R.id.recyclerView)).check(withItemCount(greaterThan(5)));
onView(withId(R.id.recyclerView)).check(withItemCount(lessThan(5)));
// ...
To complete nenick answer and provide and little bit more flexible solution to also test if item cout is greaterThan, lessThan ... 完成nenick答案并提供更灵活的解决方案,以测试项目cout是否更大,更少...
public class RecyclerViewItemCountAssertion implements ViewAssertion {
private final Matcher<Integer> matcher;
public RecyclerViewItemCountAssertion(int expectedCount) {
this.matcher = is(expectedCount);
}
public RecyclerViewItemCountAssertion(Matcher<Integer> matcher) {
this.matcher = matcher;
}
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.Adapter adapter = recyclerView.getAdapter();
assertThat(adapter.getItemCount(), matcher);
}
}
Usage: 用法:
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5));
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(greaterThan(5));
onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(lessThan(5));
// ...
Based on @Sivakumar Kamichetty answer: 基于@Sivakumar Kamichetty回答:
COUNT = 0;
不必要的行: COUNT = 0;
COUNT
variable to one element array. 将COUNT
变量传递给一个元素数组。 result
is unnecessary. 变量result
是不必要的。 Not nice, but works: 不好,但有效:
public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) {
final int[] COUNT = {0};
Matcher matcher = new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
COUNT[0] = ((RecyclerView) item).getAdapter().getItemCount();
return true;
}
@Override
public void describeTo(Description description) {}
};
onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher));
return COUNT[0];
}
I used the below method to get the count of RecyclerView 我用下面的方法来获取RecyclerView的数量
public static int getCountFromRecyclerView(@IdRes int RecyclerViewId) {
int COUNT = 0;
Matcher matcher = new TypeSafeMatcher<View>() {
@Override
protected boolean matchesSafely(View item) {
COUNT = ((RecyclerView) item).getAdapter().getItemCount();
return true;
}
@Override
public void describeTo(Description description) {
}
};
onView(allOf(withId(RecyclerViewId),isDisplayed())).check(matches(matcher));
int result = COUNT;
COUNT = 0;
return result;
}
Usage - 用法 -
int itemsCount = getCountFromRecyclerView(R.id.RecyclerViewId);
Then perform assertions to check if the itemsCount is as expected 然后执行断言以检查itemsCount是否符合预期
You can create a custom BoundedMatcher
: 您可以创建自定义BoundedMatcher
:
object RecyclerViewMatchers {
@JvmStatic
fun hasItemCount(itemCount: Int): Matcher<View> {
return object : BoundedMatcher<View, RecyclerView>(
RecyclerView::class.java) {
override fun describeTo(description: Description) {
description.appendText("has $itemCount items")
}
override fun matchesSafely(view: RecyclerView): Boolean {
return view.adapter.itemCount == itemCount
}
}
}
}
And then use it like this: 然后像这样使用它:
onView(withId(R.id.recycler_view)).check(matches((hasItemCount(5))))
Validated answer works but we can solve this problem with one line and without adapter awareness : 验证的答案有效但我们可以用一行解决这个问题而没有适配器意识:
onView(withId(R.id.your_recycler_view_id)).check(matches(hasChildCount(2)))
Replace your_recycler_view_id
with your id and 2
with the number to assert. 将your_recycler_view_id
替换为您的id,将2
替换为要断言的数字。
count with ActivityScenarioRule使用 ActivityScenarioRule 计数
@get: Rule
val activityScenarioRule = ActivityScenarioRule(ShowListActivity::class.java)
@Test
fun testItemCount(){
activityScenarioRule.scenario.onActivity { activityScenarioRule ->
val recyclerView = activityScenarioRule.findViewById<RecyclerView(R.id.movieListRecyclerView)
val itemCount = recyclerView.adapter?.itemCount?:0
....
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.