如何使用 Espresso 在不知道其位置但数据的情况下检查 RecyclerView 项目是否存在

How to check if RecyclerView item exists without knowing its position, but data, using Espresso

我有 CardViewRecyclerView child。 RecyclerView 中的每个项目都是一个单词及其翻译。我想测试删除 RecyclerView 中的特定项目。独立于项目的位置对我来说很重要,因为我在不同的条件下进行测试(零或大量项目可能有不同的排序......位置对我来说没有意义)。因此我写了 Matcher 来根据 ViewHolder.

中的数据查找视图
public static Matcher<RecyclerView.ViewHolder> withWordAndTranslation(String word, String translation) {
checkNotNull(word);
checkNotNull(translation);
return new BoundedMatcher<RecyclerView.ViewHolder, WordsViewHolder>(WordsViewHolder.class) {
  @Override
  protected boolean matchesSafely(WordsViewHolder holder) {
    return holder.wordTxt.getText().toString().equalsIgnoreCase(word)
        && holder.translationTxt.getText().toString().equalsIgnoreCase(translation);
  }

  @Override
  public void describeTo(Description description) {
    description.appendText("view holder with word, translation: " + word + ", " + translation);
  }
};
}

到目前为止,还不错。假设将其 TextViews 中带有 "test_word" 和 "test_translation" 的项目添加到 RecyclerView,我得到适当的视图并使用点击对其采取行动:

   onView(allOf(withId(R.id.wordsRecyclerView), withParent(withId(R.id.cardView)), isDisplayed()))
    .perform(actionOnHolderItem(withWordAndTranslation("test_word", "test_translation"), click()));

仅供参考,它给出了这个项目选择。我点击删除按钮删除选中的项目:

onView(allOf(withId(R.id.action_delete), isDisplayed())).perform(click());

我真的很想检查项目 ("test_word, test_translation) 是否仍然存在于 RecyclerView 上。但我不知道如何。任何 ideas/solutions?

Espresso 文档建议您 get the Adapter and iterate over it. 拿示例代码,把AdapterView换成RecyclerView。