我有一个自定义的View类公共类Foo扩展了RelativeLayout {…},里面有一个EditText. Foo类有一个XML布局膨胀,其EditText也有一个id,但它无法找到EditText
我正在使用这个Espresso代码:
onView(allOf(withId(R.id.edittext), withParent(withId(R.id.name_container)))).check(matches(isDisplayed())).perform(click());
我得到的错误信息是:
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)
解决方法:
凭着坚持不懈,我找到了自己问题的答案.
似乎在访问另一个View(嵌套视图)中的View时,你必须检查它是否是isDescendantOfA()而不是withParent()
所以这是正确的方法:onView(allOf(withId(R.id.edittext),isDescendantOfA(withId(R.id.name_container)))).check(matches(isDisplayed()));