【发布时间】:2015-06-10 14:04:19
【问题描述】:
如何使用 Espresso 从设计库中单击 NavigationView 中的 menuItem?它在整个菜单可见的较大设备上运行良好,但在横向或较小设备中,我的菜单底部不在屏幕上,我无法找到滚动/滑动到这些菜单项的方法,因此我可以点击它们。
调用:
onView(withText(R.string.contact_us)).perform(scrollTo()).check(matches(isDisplayed())).perform(click());
生成以下内容:
原因:java.lang.RuntimeException: Action will not be executed 因为目标视图与以下一项或多项不匹配 约束:(视图具有有效的可见性=可见并且是后代 一个:(可从类分配:类 android.widget.ScrollView 或者是 可从类分配:类 android.widget.HorizontalScrollView)) 目标视图:“NavigationMenuItemView{id=-1, visibility=VISIBLE, 宽度=888,高度=144,有焦点=假,有焦点=假, 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=联系我们, input-type=0, ime-target=false, has-links=false}"
NavigationView 继承自 FrameLayout 并正在执行画布切换以滚动。 https://developer.android.com/reference/android/support/design/widget/NavigationView.html
我尝试编写一个 customViewAction 以使用以下操作缓慢滚动屏幕并检查 menuItem 是否可见,如果不继续滚动,但我无法让它工作。
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());
有人知道如何在 Espresso 上测试 navigationView 吗?我很确定我的 customViewAction 背后的逻辑会起作用,但我无法让它起作用。
【问题讨论】: