Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test the home button on the Action Bar with Espresso?

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> ...

like image 845
Jesús Castro Avatar asked Dec 17 '14 14:12

Jesús Castro


2 Answers

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.

like image 186
Christopher Avatar answered Oct 12 '22 11:10

Christopher


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

like image 22
Peter Fortuin Avatar answered Oct 12 '22 11:10

Peter Fortuin