【问题标题】:I can't seem to figure out how to test BottomBar Navigation我似乎无法弄清楚如何测试 BottomBar Navigation
【发布时间】:2019-11-01 14:27:56
【问题描述】:

我正在使用材质 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 很陌生,我完全不知道如何解决这个问题。

【问题讨论】:

    标签: android testing kotlin navigation android-espresso


    【解决方案1】:

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

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

    窗口动画比例

    过渡动画比例

    动画持续时间比例

    【讨论】:

      猜你喜欢
      • 2021-06-04
      • 2011-07-08
      • 2014-05-19
      • 2020-11-14
      • 2011-11-28
      • 2014-07-28
      • 2016-03-12
      • 2023-02-11
      • 2012-05-01
      相关资源
      最近更新 更多