如何使用Espresso计算RecyclerView项目

使用浓咖啡和Hamcrest,

如何计算recyclerView中可用的物品编号?

例如:我想检查是否有5个项目显示在一个特定的RecyclerView(如果需要滚动)。

Solutions Collecting From Web of "如何使用Espresso计算RecyclerView项目"

这里有一个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)); } } 

然后使用这个断言

 onView(withId(R.id.recyclerView)).check(new RecyclerViewItemCountAssertion(5)); 

我已经开始编写一个库,使用espresso和uiautomator进行testing。 这包括RecyclerView动作和断言的工具。 https://github.com/nenick/espresso-macchiato请参阅示例EspRecyclerView与方法assertItemCountIs(int)

为了完成nenick的答案,并提供一点点更灵活的解决scheme,以testing如果项目cout更大,但less…

 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); } } 

用法:

 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)); // ... 

在@ 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); } } 

用法:

 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)); // ... 

我使用下面的方法来获取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; } 

用法 –

 int itemsCount = getCountFromRecyclerView(R.id.RecyclerViewId); 

然后执行断言来检查itemsCount是否如预期的那样

基于@Sivakumar Kamichetty回答:

  1. variables“COUNT”从内部类访问,需要声明为final。
  2. 不必要的行: COUNT = 0;
  3. COUNTvariables传递给一个元素数组。
  4. variablesresult是不必要的。

不好,但工作:

 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]; }