Espresso 在自定义视图类中找不到视图 [英] Espresso can't find a View inside a custom view class

查看:44
本文介绍了Espresso 在自定义视图类中找不到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义 Viewpublic class Foo extends RelativeLayout{...} 其中有一个 EditText .Foo 类有一个膨胀的 XML 布局,它的 EditText 也有一个 id 但它无法找到 EditText

I have a custom View class public class Foo extends RelativeLayout{...} which have an EditText in it. The Foo class have a XML layout inflated and its EditText also have an id but its not able to find the EditText

我正在使用此 Espresso 代码:

I'm using this Espresso code:

onView(allOf(withId(R.id.edittext), withParent(withId(R.id.name_container)))).check(matches(isDisplayed())).perform(click());

我得到的错误信息是:

android.support.test.espresso.NoMatchingViewException:没有视图找到匹配的层次结构:(id:dk.aura.app.staging:id/edittext并具有父匹配:id:dk.aura.app.staging:id/name_container)

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: (with id: dk.aura.app.staging:id/edittext and has parent matching: with id: dk.aura.app.staging:id/name_container)

推荐答案

通过坚持,我找到了自己问题的答案.

With persistence I found an answer to my own question.

似乎在访问另一个视图中的视图(嵌套视图)时,您必须检查它是否 isDescendantOfA() 而不是 withParent()

It seems that when accessing View inside another View(nested views), you have to check for if it it isDescendantOfA() instead of withParent()

所以这是正确的方法:onView(allOf(withId(R.id.edittext), isDescendantOfA(withId(R.id.name_container)))).check(matches(isDisplayed()));

这篇关于Espresso 在自定义视图类中找不到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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