使用espresso在Web视图中查找多个元素 [英] Find multiple elements in web view with espresso

查看:55
本文介绍了使用espresso在Web视图中查找多个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm testing a hybrid app, where each view has a web view.
In one of these web views I have a list of elements with the same attribute. They have the same xpath locator that is something like:

//h4[contains(@data-role, 'product-name')]

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

I want to create a list of these elements and iterate through them, count them, get their attributes.

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

In the documentation, I found two similar methods:

findElement(locator, value)

findMultipleElements(locator, value)

尽管我完全不清楚如何使用它.我尝试查找有关示例,但没有成功.

Though it's totally unclear to me how to use it. I tried to find examples on it but with no success.

有人可以帮我吗?

推荐答案

这是我找到的解决方案.

Here is the solution that I have found.

@kaqqao是正确的,因为 findMultipleItems 调用返回的 Atom< List< ElementReference>> 不能与 onWebView()一起使用,因为您只有 withElement()接受 Atom< ElementReference> 或仅接受 ElementReference

@kaqqao is right that findMultipleItems call returns Atom<List<ElementReference>> that is not usable with onWebView() because there you have only withElement() that accepts either Atom<ElementReference> or just ElementReference

您可以执行的操作是查找多个项目,并从Atom中获取结果.如果您在Web.java中检查espresso的 doEval 方法的源,这就是它的内部工作方式.

What you can do though is perform your action that find multiple items and just get results from your Atom. This is how it works internally if you check the source of doEval method inside Web.java for espresso.

val elements = with(AtomAction(findMultipleElements(
                Locator.XPATH,
                "YOUR_COMPLEX_XPATH"
        ), null, null)) {
            onView(ViewMatchers.isAssignableFrom(WebView::class.java)).perform(this)
            this.get()
        }

此代码将为您提供 List< ElementMatcher> .

然后将其运行为

elements.forEach {          
  onWebView().forceJavascriptEnabled().withElement(it).perform(webClick())
}

这篇关于使用espresso在Web视图中查找多个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆