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.
It's possible to click on the button when you use the description from the resources.
onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click());
Works fine with appcompat-v7:23.1.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With