在 Appium 中获取父对象仅低一级的所有子笔记 (Android)

Get all child notes only one level lower from parent object in Appium (Android)

我写了一个自动化测试软件,现在有一个问题。 在我实际的 本机应用程序 中,我有一个像这样的 ListView:

...等等。

我需要和想要的是从 ListView 中降低所有一级 LinearLayout(迭代的,可见和不可见的元素) :

列表视图:{线性布局、线性布局、...}

使用该代码,我可以从 ListView 获取所有子项,但这对我来说太过分了:

listViewChildElements       = "[...]android.widget.ListView[1]//android.widget.LinearLayout";
app.getDriver().findElements(By.xpath(listViewChildElements));

在另一个post中,我看到了一些带有'count'的东西,但我不明白。

感谢您的帮助,很高兴认识您。


在@har07 的帮助下,我编写了一个函数,不是最好或最好的解决方案,也不是最终的,而是正确通道中的第一步:

    public static List<String> getAllViewElements(App app){
    int i = 0;

    List<String> writtenElementsByText = new ArrayList<String>();

    while(true){
        if(!writtenElementsByText.isEmpty()){
            String recentLastItem = writtenElementsByText.get(writtenElementsByText.size()-1);
            if(recentLastItem.equals("Alle Themen"))
                break;
            else
                app.getDriver().scrollTo(recentLastItem);
        }

        List<WebElement> tmp = app.getDriver().findElements(By.xpath(listViewChildElements));


        if(writtenElementsByText.isEmpty())
            for(WebElement e: tmp){
                writtenElementsByText.add(getTextFromElement(e));
            }
        else
            for(WebElement e: tmp){
                String activualElemenByText = getTextFromElement(e);
                if(!writtenElementsByText.contains(activualElemenByText))
                    writtenElementsByText.add(activualElemenByText);
            }
    }

    System.out.println(writtenElementsByText.size());
    for(String e: writtenElementsByText){
        System.out.println("\t-\t"+e);
    }
    return writtenElementsByText;
}

public static String getTextFromElement(WebElement element){
    return element.findElement(By.id(listViewChildElementTextID)).getText();
}

希望以下代码对您有所帮助:

 WebElement listViewElement = driver.findElement(<your ListView locator>);
 List<WebElement> listLinearLayoutChildren = listViewElement.findElements(By.className("android.widget.LinearLayout")); //All the linear layouts under ListView in one list
 listLinearLayoutChildren.get(index).<action>; //index of Nth linearlayout starting from 0 and <action> you want to perform

如果我理解正确,您想使用单斜杠 (/) 而不是双斜杠从父元素获取直接子元素:

listViewChildElements = "[...]android.widget.ListView[1]/android.widget.LinearLayout";
                                                        ^
                                                        notice that only one slash being 
                                                        used above

有了这个更改,XPath 现在应该 return LinearLayoutListView[1].

的直接子项

为每个 linearLayout 设置标签是最简单的方法之一。 现在通过 view.findViewWithTag(index)

访问线性布局