使用espresso在Web视图中查找多个元素

Jen*_*nny 6 xpath android webview android-webview android-espresso

我正在测试一个混合应用程序,其中每个视图都有一个Web视图。
在这些Web视图之一中,我具有具有相同属性的元素列表。它们具有相同的xpath定位器,如下所示:

//h4[contains(@data-role, 'product-name')]
Run Code Online (Sandbox Code Playgroud)

我想创建这些元素的列表,并对其进行遍历,计数,获取它们的属性。

在文档中,我发现了两种类似的方法:

findElement(locator, value)
Run Code Online (Sandbox Code Playgroud)

findMultipleElements(locator, value)
Run Code Online (Sandbox Code Playgroud)

尽管我完全不清楚如何使用它。我试图找到例子,但没有成功。

有人可以帮我吗?

Rai*_*333 -1

你能尝试这样的事情吗?因为您应该关心的实际上是 ElementReference,并且您可以使用简单的 for/foreach 语句迭代从 findMultipleElements 返回的 lsit:

yourList = findMultipleElements(locator, value);
yourList.size(); //this will get you the count of found elements with that locator
for(Atom<ElementReference> item : yourList ){
    item.getAttribute...
    //and whatever you want
}
Run Code Online (Sandbox Code Playgroud)