I found a real solution to this issue. By using the hierarchyviewer I found that the toolbar looks like this:
This means we could match the hamburger icon (not back button) like this:
onView(withContentDescription("Open navigation")).perform(click());
But a better solution to me was to find out that the hamburger icon is the only ImageButton and a direct child view of the v7 Toolbar. So I wrote a helper method to match it:
public static Matcher<View> androidHomeMatcher() {
return allOf(
withParent(withClassName(is(Toolbar.class.getName()))),
withClassName(anyOf(
is(ImageButton.class.getName()),
is(AppCompatImageButton.class.getName())
)));
}
@Test
public void clickHamburgerIcon() throws Exception {
onView(androidHomeMatcher()).perform(click());
// ...
}
This solution is better because it should match the view no matter which locale you use in your test. :-)
EDIT: Note that Toolbar might be android.support.v7.widget.Toolbar or android.widget.Toolbar - depending on your use case!
EDIT: The support lib version 24.2.0 uses AppCompatImageButton instead of ImageButton, so I added it, too.
EDIT: You have to import the correct methods to get this to work. Here are the imports used:
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is;