How do you click on a menuItem in a NavigationView from the design library using Espresso? It works fine on a larger devices where the whole menu is visible but in landscape or smaller devices the bottom of my menu is off the screen and I can not figure out a way to scroll/swipe to these menuItems so I can click on them.
Calling:
onView(withText(R.string.contact_us)).perform(scrollTo()).check(matches(isDisplayed())).perform(click());
Generates the following:
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: (view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView)) Target view: "NavigationMenuItemView{id=-1, visibility=VISIBLE, width=888, height=144, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=1653.0, text=Contact Us, input-type=0, ime-target=false, has-links=false}"
NavigationView inherits from FrameLayout and are doing a canvas shift to scroll. https://developer.android.com/reference/android/support/design/widget/NavigationView.html
I tried to write a customViewAction to slowly scroll the screen using the below action and check if the menuItem is visible and if not keep scrolling, but I could not get it to work.
CoordinatesProvider coordinatesProvider = new CoordinatesProvider() {
public float[] calculateCoordinates(View view) {
float[] xy = GeneralLocation.BOTTOM_CENTER.calculateCoordinates(view);
xy[0] += 0.0F * (float)view.getWidth();
xy[1] += -0.083F * (float)view.getHeight();
return xy;
}
};
onView(withId(R.id.navigation_view)).perform(new GeneralSwipeAction(Swipe.SLOW,
coordinatesProvider, GeneralLocation.TOP_CENTER, Press.FINGER));
onView(withText(R.string.contact_us)).check(matches(isDisplayed())).perform(click());
Has anyone figured out how to test navigationView on Espresso yet? I am pretty sure my logic behind the customViewAction would work but I can not get it to work.
I figured it out, I used a custom viewAction
I do not love this solution, it is not the cleanest since for some reason I have to swipe again after it says its visible but it works.
To use it you call it with the following: