How can I click a navigation item when the navigation box consists of a list of lines with an image and text view?
I used the Espresso test source example: git / testapp_test / src / main / java / com / google / android / apps / common / testing / ui / testapp / DrawerActionsTest.java
I extracted DrawerActions and DrawerMatchers from the contribution and put them locally in my test project.
Navigation Box Row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/list_selector">
<ImageView
android:id="@+id/icon"
etc ... />
<TextView
android:id="@+id/title"
etc.... />
</RelativeLayout>
Navigation Elements:
public class NavDrawerItem {
public String title;
public int icon;
....
@Override
public boolean equals( Object mob2) {
String otherName = ((NavDrawerItem) mob2).title;
return( title.equals( otherName));
}
}
The NavigationDrawerAdapter application fills the navigation box view.
The Espresso test source opens a box, closes it, opens it again ... but I can’t find a match for the first Import item. Thus, the test stops when you click.
The code:
public LearnerAppAutoTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testOpenAndCloseDrawer() {
openDrawer(R.id.drawer_layout);
closeDrawer(R.id.drawer_layout);
openDrawer(R.id.drawer_layout);
onView(withId(R.id.drawer_layout)).check(matches(isOpen()));
String rowContents = "Import";
onData( allOf( is( instanceOf( String.class)), is( rowContents))).perform(click());
onData( allOf( is( instanceOf( NavDrawerItem.class)), is( rowContents))).perform(click());
onData( allOf( instanceOf( NavDrawerItem.class), navDrawerItemHavingName( rowContents))).perform( click());
}
, , ... , NavDrawerItem.