如何使用Espresso计算RecyclerView项目 [英] How to count RecyclerView items with Espresso

查看:114
本文介绍了如何使用Espresso计算RecyclerView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用浓缩咖啡和Hamcrest,

Using Espresso and Hamcrest,

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

How can I count items number available in a recyclerView?

示例:我想检查5个项目是否在特定的RecyclerView中显示(如有必要,请滚动).

Exemple: I would like check if 5 items are displaying in a specific RecyclerView (scrolling if necessary).

推荐答案

下面是一个示例ViewAssertion,用于检查RecyclerView项目计数

Here an example ViewAssertion to check RecyclerView item count

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的测试更加简单.这包括用于RecyclerView操作和断言的工具. https://github.com/nenick/espresso-macchiato 例如,参见带有方法assertItemCountIs的EspRecyclerView (int)

I have started to write an library which should make testing more simple with espresso and uiautomator. This includes tooling for RecyclerView action and assertions. https://github.com/nenick/espresso-macchiato See for example EspRecyclerView with the method assertItemCountIs(int)

这篇关于如何使用Espresso计算RecyclerView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆