espresso 如何获得满足 `ViewMatchers` 列表的视图

How can espresso get a view that satisfies a list of `ViewMatchers`

我阅读了 Espresso 的 documentation 方法 onView(),它适用于单个视图。

有人知道我怎样才能:

  1. 获取所有满足ViewMatcher

  2. 的视图
  3. 获取满足ViewMatchers

  4. 列表的视图

例如,我想知道 recyclerView!

中有多少项

Get a view that satisfies a list of ViewMatchers

这可以通过 Hamcrest 匹配器完成 allOf

import static org.hamcrest.CoreMatchers.allOf;

onView(allOf(withId(R.id.exampleView), 
             withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
        .check(matches(isCompletelyDisplayed()))
        .check(matches(withHint(R.string.exampleViewHint)));

Get all views that satisfy a ViewMatcher

好吧,也许这是朝着正确方向的开始:下面是一个示例,说明如何查找所有 Android 个带有给定值标记的视图。

发件人:https://gist.github.com/orip/5566666 and

package com.onavo.android.common.ui;

import android.view.View;
import android.view.ViewGroup;

import java.util.LinkedList;
import java.util.List;

/**
 * Based on  by by Shlomi Schwartz
 * License: MIT
 */
public class ViewGroupUtils {
    public static List<View> getViewsByTag(View root, String tag) {
        List<View> result = new LinkedList<View>();

        if (root instanceof ViewGroup) {
            final int childCount = ((ViewGroup) root).getChildCount();
            for (int i = 0; i < childCount; i++) {
                result.addAll(getViewsByTag(((ViewGroup) root).getChildAt(i), tag));
            }
        }

        final Object rootTag = root.getTag();
        // handle null tags, code from Guava's Objects.equal
        if (tag == rootTag || (tag != null && tag.equals(rootTag))) {
            result.add(root);
        }

        return result;
    }
}