Android espresso test NavigationView

I am writing some espressos for an application that I recently made. I am using NavigationView inside DrawerLayout as a sliding menu.

I managed to open the box this way:

onView(withId(R.id.drawer_layout)).perform(open()); 

Now this works, so I'm trying to click on a menu item in the NavigationView.

 onView(withId(R.id.nav_register)).. 

can't find a representation. I tried a few things, but I can not find a way to get the menuitem idea. So the elements in the code are assigned:

  <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" /> 

and activity_main_drawer.xml

 <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_menu_home" android:title="Home" /> <item android:id="@+id/nav_register" android:icon="@drawable/ic_menu_register" android:title="Registreer" /> <item android:id="@+id/nav_login" android:icon="@drawable/ic_menu_login" android:title="Log in" /> <item android:id="@+id/nav_play" android:icon="@drawable/ic_menu_play" android:title="Speel sessie" /> <item android:id="@+id/nav_logout" android:icon="@drawable/ic_menu_logout" android:title="Log uit" /> </group> 

I read something about NavigationViewMenuItem is a private member and not available. Can someone help me?

Congratulations! Shenno

+6
source share
2 answers

In fact, the view representing the menu item does not know the identifier of the menu item in Android. As a result, the withId() method does not work in Espresso with menu items. I suggest you use the withText method:

 onView(withText("the item title")). 
+3
source

If you want to stick with an identifier, you can use:

 onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id. nav_register)); 
0
source

All Articles