我想检查一个 View 是否没有被另一个 View 隐藏。我没有成功使用经典的 isDisplayed 断言对此进行测试。

在我的情况下,我在相同的布局(FrameLayout)中有一个 View A 和一个 View B。我想测试 View A 是否对用户可见。
但我知道,这个测试应该失败,因为 View ​​B 与 View 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()
    )
)

正如我之前所说,这个测试不会失败,即使 View B 完全在 View A 上方。

如何使用 espresso 来测试我的 View A 是否对用户真正可见?例如,当 View B 使用 translateX/Y 移动或以任何其他方式隐藏时。

最佳答案

在您的情况下,您是否正在检查 View 是否可见但不一定显示在屏幕上。为此,您可以使用 withEffectiveVisibility(Visibility)

onView(matcher).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));

关于android - 使用 Espresso 检查一个 View 是否被另一个同级 View 隐藏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47578074/

10-12 00:31
查看更多