我似乎无法弄清楚如何测试BottomBar导航

时间:2019-11-01 14:27:56

标签: android testing kotlin navigation android-espresso

我正在使用材质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的新手,所以我绝对不知道如何解决此问题。

1 个答案:

答案 0 :(得分:0)

Espresso不适用于动画,因为它们引入了视觉状态延迟。您需要在设备上禁用动画。首先,启用开发人员选项:

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

窗口动画比例

过渡动画比例

动画师持续时间量表

相关问题