I have enabled the home button to return to the previous view. Simply, doing this:
getActionBar().setDisplayHomeAsUpEnabled(true);
I'm using the last version of com.android.support:appcompat-v7:21.0.2
. However, when I use the below code it doesn't work throwing a Exception.
Espresso.onView(ViewMatchers.withId(android.R.id.home)).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withId(R.id.home)).perform(ViewActions.click());
Exception:
com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: is <2131296261> ...
I did the following workaround:
private String getString(int resId){
return getInstrumentation().getTargetContext().getString(resId);
}
public void testUI() {
onView(withContentDescription(getString(R.string.navigation_drawer_open))).perform(click());
onView(withContentDescription(getString(R.string.navigation_drawer_close))).perform(click());
}
Basically I use the content description attribute instead of the view id.