くま's Tech系Blog

基本的には技術で学んだことを書き留めようと思います。雑談もやるかもね!

ナビゲーションドローワーのUIテスト

EspressoでUIテストを実施していて、ナビゲーションドローワーがある場合に少しつまずいたので、残したいと思います。

dependencyの追加

ナビゲーションドローワーのアクションはespresso-contribのライブラリに入っているので追加しました。 その時、バージョン違いでコンフリクトが起こったので、重複しているものは除外します。

build.gradle(app)

androidTestImplementation ('com.android.support.test.espresso:espresso-contrib:3.0.2') {
        exclude group: 'com.android.support', module: 'support-annotations'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude group: 'com.android.support', module: 'recyclerview-v7'
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.android.support', module: 'support-compat'
        exclude group: 'com.android.support', module: 'support-core-utils'
    }

これでボタンクリックと同じようにナビゲーションドローワーを開くアクションが使えるようになります。

テストの記載

NavigatinViewは下記になります。

activity_main.xml

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

MainActivityTest

    @Test
    fun navigationMenu_updatetime_exist() {
        onView(ViewMatchers.withId(R.id.drawer_layout)).perform(DrawerActions.open())
        Thread.sleep(1500)
        onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.nav_slideshow))
        Thread.sleep(1500)
        onView(ViewMatchers.withId(R.id.updateTimeLabel)).check(matches(not(doesNotExist())))
    }

ナビゲーションドローワーを開くアクションはDrawerActions.open()になります。 開いたら、NavigationViewActions.navigateTo(R.id.nav_slideshow)の部分で、開いたドローワーに存在するメニューをタップしています。

実際にテストしてみて、思ったより画面の操作は担保できるなあと感じました。