使用Espresso检查视图是否被其他兄弟视图隐藏

5
我想检查一个视图是否被另一个视图隐藏。我尝试使用经典的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/>

测试代码:

onView(
    withId(R.id.view_a)
).check(
    matches(
        isDisplayed()
    )
)

正如我之前所说的,即使视图B完全覆盖视图A,此测试也不会失败。

我该如何使用Espresso测试用户实际可见视图A?例如,当使用translateX/Y移动视图B或以其他方式隐藏它时。


https://developer.android.com/reference/android/support/test/espresso/matcher/ViewMatchers.html#isDisplayingAtLeast(int) - ephemient
你找到解决方案了吗?我也卡在这个问题上了。 - Venkataramanan
2个回答

2

如果您想检查视图是否可见但不一定显示在屏幕上,可以使用withEffectiveVisibility(Visibility)

注意:请保留HTML标签。
onView(matcher).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));

1
这只是检查可见性属性。它无法检测到视图因另一个视图显示在其上而被隐藏的情况。 - Michael

0

尝试使用isCompletelyDisplayed匹配器:

onView(withId(R.id.my_view_A_id))
.check(matches(isCompletelyDisplayed()));

1
isCompletelyDisplayed 调用 isDisplyaedAtLeast(100)。两者只能考虑到父视图裁剪的区域(“视图的区域没有被任何父视图遮挡,因此对用户可见。”),因为它们使用 ViewGroup.getChildVisibleRect,该方法仅适用于父视图和相关子视图。不幸的是,这不能考虑到几个兄弟姐妹(例如在 FrameLyout 中)可能会重叠。 - Josef Adamcik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接