在 Appium (Android) 中获取所有子笔记仅比父对象低一级 [英] Get all child notes only one level lower from parent object in Appium (Android)
问题描述
我编写了一个自动化测试软件,现在遇到一个问题.在我实际的原生应用中,我有一个像这样的 ListView:
I write an automation testing software and have one problem now. In my actual native app I have a ListView like this here:
- 列表视图
- 线性布局
- 线性布局
- 线性布局
- 线性布局
- 线性布局
...等等.
我需要和想要的是从 ListView 中降低所有一层 LinearLayout(迭代的,可见和不可见元素):
What I need and want is to get all one level lower LinearLayout (iterative ones, visible and non visible elements) from ListView:
ListView : {LinearLayout, LinearLayout, ...}
ListView : {LinearLayout, LinearLayout, ...}
使用该代码,我可以从 ListView 中获取所有子项,但对我来说太多了:
With that code I get all childs from ListView, but its to much for me:
listViewChildElements = "[...]android.widget.ListView[1]//android.widget.LinearLayout"; app.getDriver().findElements(By.xpath(listViewChildElements));
在另一篇文章中,我看到一些带有计数"的内容,但我不明白.
In another post I see something with 'count', but I don't understand it.
感谢您的帮助,很高兴认识您.
Thanks for your help and nice to meet you.
在@har07 的帮助下,我编写了一个函数,不是最好的或最好的解决方案,也不是最终的,而是正确的第一步:
With the help of @har07 I write one function, not best or finest solution and not finally, but the 1st step in the right lane:
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(); }
推荐答案
如果我理解正确的话,你想使用单斜杠 (
/
) 而不是双斜杠来从一个父元素:If I understand the problem correctly, you want to use single slash (
/
) instead of double to get direct child element from a parent element :listViewChildElements = "[...]android.widget.ListView[1]/android.widget.LinearLayout"; ^ notice that only one slash being used above
有了这个改变,XPath 现在应该返回
LinearLayout
,它是ListView[1]
的直接子元素.With that change, the XPath should now return
LinearLayout
that is direct child of theListView[1]
.这篇关于在 Appium (Android) 中获取所有子笔记仅比父对象低一级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 线性布局
- 线性布局