I have this piece of code
private void setupDrawerLayout() {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
final int itemId = menuItem.getItemId();
switch (itemId) {
case R.id.drawer_my_mixes:
replaceFragment(MyMixesFragment.newInstance());
drawerLayout.closeDrawers();
break;
and so on...
I want to make espresso to click R.id.drawer_settings which will basically open a fragment called SettingsFragment. How do I do this? I tried this with no success
private ViewInteraction navigateToSettings(){
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());
return onView(withText("Settings")).perform(click());
}
The correct way would be to use the onData function of Espresso.
onData(allOf(is(instanceOf(NavigationMenuItem.class)), ... )).perform(click());
But NavigationMenuItem isn't accessible since it is a private nested class.
An issue is pending https://code.google.com/p/android/issues/detail?id=187701 to find a solution on it. Go add a star to it ;-)
openActionBarOverflowOrOptionsMenu() does not open NavigationView.
For me the following works:
//Open NavigationView
Matcher<View> isImageButton = withClassName(is(ImageButton.class.getName()));
Matcher<View> isInToolbar = isDescendantOfA(withId(R.id.toolbar));
onView(allOf(isInToolbar, isImageButton)).perform(click());
//Click on item in NavigationView
onView(withText(R.string.logout)).perform(click());
This solution works for me temporarily until a better solution like suggested by user40797 is working...
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