首页 > 解决方案 > 我似乎无法弄清楚如何测试 BottomBar Navigation

问题描述

我正在使用材料 BottomNavigationView 并且正在尝试测试导航:

测试:

    @Test //fails
    fun navigationMenu_contractClicked_openContractFragment() {
        Espresso.onView(ViewMatchers.withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.contractFragment))
        Espresso.onView(ViewMatchers.withId(R.id.constraint_contract_overview))
    }

XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/raisingBlack"
            app:itemBackground="@color/raisingBlack"
            app:itemIconTint="@color/white"
            app:itemTextColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/bottom_nav_menu" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:defaultNavHost="true"
            app:layout_constraintBottom_toTopOf="@id/nav_view"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:navGraph="@navigation/mobile_navigation" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

但我得到了这个错误:

androidx.test.espresso.PerformException:在“目标设备上启用动画或过渡”视图上执行“单击带有 id 的菜单项”时出错。

由于我对 android 和 kotlin 很陌生,所以我完全不知道该怎么做才能解决这个问题。

标签: androidtestingkotlinnavigationandroid-espresso

解决方案


由于它们引入的视觉状态延迟,Espresso 不适用于动画。您需要禁用设备上的动画。首先,启用开发者选项:

打开设置应用程序。滚动到底部并选择关于手机。滚动到底部并点击内部版本号 7 次。返回上一个屏幕以在底部附近找到开发人员选项。从设置应用程序访问开发人员选项,然后在绘图部分下,将以下所有选项切换为动画关闭:

窗口动画比例

过渡动画比例

动画师持续时间比例


推荐阅读