Android 检查一个视图是否未被另一个使用Espresso的同级视图隐藏
我想检查一个视图是否未被另一个视图隐藏。我没有成功地使用经典的Android 检查一个视图是否未被另一个使用Espresso的同级视图隐藏,android,android-espresso,Android,Android Espresso,我想检查一个视图是否未被另一个视图隐藏。我没有成功地使用经典的isDisplayed断言测试这一点 在我的例子中,视图a和视图B位于同一布局(FrameLayout)内。我想测试视图A是否对用户可见。 但是我知道,这个测试应该失败,因为视图B与视图A完全重叠 布局示例: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.c
isDisplayed
断言测试这一点
在我的例子中,视图a和视图B位于同一布局(FrameLayout)内。我想测试视图A是否对用户可见。
但是我知道,这个测试应该失败,因为视图B与视图A完全重叠
布局示例:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<View android:id="@+id/view_a"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<View android:id="@+id/view_b"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout/>
正如我之前所说,即使视图B完全超过视图A,此测试也不会失败
如何使用espresso测试我的视图A是否对用户可见?例如,当使用
translateX/Y
移动视图B或以任何其他方式隐藏视图B时。在您的情况下,检查视图是否可见,但不一定显示在屏幕上。为此,您可以将与effectivevisibility(Visibility)
一起使用
尝试使用IsCompletlyDisplayed matcher:
onView(withId(R.id.my_view_A_id))
.check(matches(isCompletelyDisplayed()));
已完全显示
调用isdisplyedatellast(100)
。两者都只能说明父视图裁剪的区域(“视图的区域不会被任何父视图遮挡,因此对用户可见”),因为它们使用的是ViewGroup.getChildVisibleRect
,该视图仅适用于所讨论的父视图和子视图。不幸的是,这不能解释几个兄弟(例如在FrameLyout
)可能相互重叠。这只是检查可见性属性。它不会检测某个视图是否由于另一个视图显示在其顶部而被隐藏。
onView(matcher).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
onView(withId(R.id.my_view_A_id))
.check(matches(isCompletelyDisplayed()));