Issue
I read Espresso's documentation with method onView() which works on a single view.
Does someone know how can I:
Get all views that satisfy a
ViewMatcherGet a view that satisfies a list of
ViewMatchers
For example, I want to know how many items are in a recyclerView!
Solution
Get a view that satisfies a list of ViewMatchers
This can be done with the Hamcrest matcher 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
Well, maybe this is a start in the right direction: Below is an example of how to find all Android views tagged with a given value.
From: https://gist.github.com/orip/5566666 and http://stackoverflow.com/a/8831593/37020
package com.onavo.android.common.ui;
import android.view.View;
import android.view.ViewGroup;
import java.util.LinkedList;
import java.util.List;
/**
* Based on http://stackoverflow.com/a/8831593/37020 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;
}
}
Answered By - Mark Han
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.