How to get a list of items in espresso?

I am trying to automate my own Android application using the espresso framework and I cannot find a way to get a list of items. (for example, I need to check all the checkboxes on the page) in selenium I can do like this:

elements = self.driver.find_elements_by_xpath("//xpath")
for element in elements:
    //do stuff
+4
source share
1 answer

As I understand it, in espresso you can only perform one action for one item at a time. I think I cannot get a list of elements by name or class name or the like. therefore, if we have a container like RecyclerView, we can perform actions on it with children in a loop. Like this:

for (int i = 0; i <= num; i++) {
            onView(withId(R.id.result)).perform(RecyclerViewActions.actionOnItemAtPosition(i, click()));
        }

and we can get the number of children:

 public int getRecyclerViewChildCount(int matcher) {
        final int[] count = {0};
        onView(withId(matcher)).perform(new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isAssignableFrom(RecyclerView.class);
            }
            @Override
            public String getDescription() {
                return "getting child count";
            }
            @Override
            public void perform(UiController uiController, View view) {
                RecyclerView rv = (RecyclerView) view;
                count[0] = rv.getChildCount();
            }
        });
        return count[0];
    }

, , , , . , .

, , PerformException, :

try {
    int count = 50;// specify number of child elements in RecycleView you want to click
    for (int i = 0; i <= count; i++) {       
    onView(withId(R.id.result)).perform(RecyclerViewActions.actionOnItemAtPosition(i, click()));
            }
            }catch(PerformException e){}//if argument is greater then files in search result.
0

All Articles