android - 使用 Espresso 检查一个 View 是否被另一个同级 View 隐藏

标签 android android-espresso

我想检查一个 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/

相关文章:

java - 通过打开/关闭 300 多个复选框来改善用户体验

android - Espresso-web 导入导致 duplicateFileException

Android Espresso 测试 : Class not found: . ..空测试套件

android - 使用 Espresso 测试应用程序时出现 "Native crash"

android - 如何使用express编写recyclerview拖放的测试用例

android - 如何在选择新选项卡或在 Android 中使用滑动选项卡滑动到新 fragment 之前执行验证?

android - 景观维度被忽略

android - 为什么 Espresso 在测试完成后离开应用程序?如何阻止它这样做

java - Android 服务 ("startService & stopService") 和播放/停止按钮合二为一

java - ListView/ScrollViews 下的大位图会减慢滚动速度