简体   繁体   中英

select item in NavigationView with Espresso

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

How to select an item in the NavigationView.

In your package utils:

public class NavigationViewTestHelper {

    public static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

In your test after opening your NavigationView.

ViewInteraction navigationMenuItemView = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.design_navigation_view),
                                childAtPosition(
                                        withId(R.id.*your_navigation_view_id*),
                                        0)),
                        1), // Position of the element in the Navigation view.
                        isDisplayed()));

navigationMenuItemView.perform(click());

Using MiguelHincapieC response, This is what worked for me:

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());

Hope it helps:).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM