【问题标题】:How to get a list of elements in espresso?如何获取浓缩咖啡中的元素列表?
【发布时间】:2015-09-17 05:12:24
【问题描述】:

我正在尝试使用 espresso 框架自动化一个 android 本机应用程序,但我找不到获取元素列表的方法。 (例如我需要检查页面上的所有复选框) 在硒中我可以这样做:

elements = self.driver.find_elements_by_xpath("//xpath")
for element in elements:
    //do stuff

【问题讨论】:

    标签: java android selenium android-espresso


    【解决方案1】:

    据我了解,在浓缩咖啡中一次只能对一个元素执行一项操作。我想我无法通过 id 或类名或其他方式获取元素列表。所以如果我们有RecyclerView 的容器,我们可以在循环中对它的子元素执行操作。像这样:

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

    我们可以得到孩子的数量:

     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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      • 1970-01-01
      相关资源
      最近更新 更多