在使用浓缩咖啡进行测试时,如何在Android上的NavigationView中单击菜单项?

在使用浓缩咖啡进行测试时,如何在Android上的NavigationView中单击菜单项?,android,android-espresso,Android,Android Espresso,如何使用Espresso在设计库的NavigationView中单击菜单项?它可以在较大的设备上正常工作,整个菜单可见,但在横向或较小的设备上,我的菜单底部不在屏幕上,我无法找到滚动/滑动到这些菜单项的方法,以便单击它们 电话: onView(withText(R.string.contact_us)).perform(scrollTo()).check(matches(isDisplayed())).perform(click()); 生成以下内容: onView(isRoot()).per

如何使用Espresso在设计库的NavigationView中单击菜单项?它可以在较大的设备上正常工作,整个菜单可见,但在横向或较小的设备上,我的菜单底部不在屏幕上,我无法找到滚动/滑动到这些菜单项的方法,以便单击它们

电话:

onView(withText(R.string.contact_us)).perform(scrollTo()).check(matches(isDisplayed())).perform(click());
生成以下内容:

onView(isRoot()).perform(CustomViewActions.swipeForTextToBeVisible(getActivity().getString(R.string.contact_us),2000, getActivity().findViewById(R.id.navigation_view)));
原因:java.lang.RuntimeException:将不执行操作 因为目标视图与以下一项或多项不匹配 约束:(视图具有有效可见性=可见且为子体) 是可从类中赋值的:类android.widget.ScrollView还是 可从类分配:类android.widget.HorizontalScrollView) 目标视图:“NavigationMenuItemView{id=-1,可见性=可见, 宽度=888,高度=144,焦距=false,焦距=false, 窗口焦点为真,可点击为假,已启用为真, 聚焦=假,聚焦=假,布局请求=假, 已选择=false,请求根布局=false, 输入连接=错误,x=0.0,y=1653.0,文本=联系我们, 输入类型=0,输入法目标=false,有链接=false}”

NavigationView继承自FrameLayout,正在进行画布移动以滚动。

我尝试编写一个customViewAction,使用下面的操作缓慢滚动屏幕,并检查菜单项是否可见,如果没有,则继续滚动,但我无法使其工作

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());

有人知道如何在浓缩咖啡上测试navigationView了吗?我很确定customViewAction背后的逻辑会起作用,但我无法让它起作用。

我想出来了,我使用了自定义viewAction

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());
public static ViewAction swipeForTextToBeVisible(final String text, final long millis, final View navigationView) {
        return new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isRoot();
            }

            @Override
            public String getDescription() {
                return "swipe for a specific view with text <" + text + "> during " + millis + " millis.";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
                final long startTime = System.currentTimeMillis();
                final long endTime = startTime + millis;
                final Matcher<View> viewMatcher = withText(text);

                do {
                    boolean returnAfterOneMoreSwipe = false; //not sure why I have to do one more swipe but it works

                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                        // found view with required ID
                        if (viewMatcher.matches(child) && child.getVisibility() == View.VISIBLE) {
                            returnAfterOneMoreSwipe = true;
                        }
                    }

                    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;
                        }
                    };

                    //Below code is c/p from perform in android/support/test/espresso/action/GeneralSwipeAction.class
                    float[] startCoordinates = coordinatesProvider.calculateCoordinates(navigationView);
                    float[] endCoordinates = GeneralLocation.TOP_CENTER.calculateCoordinates(navigationView);
                    float[] precision = Press.FINGER.describePrecision();
                    Swiper.Status status = Swiper.Status.FAILURE;

                    for(int tries = 0; tries < 3 && status != Swiper.Status.SUCCESS; ++tries) {
                        try {
                            status = Swipe.SLOW.sendSwipe(uiController, startCoordinates, endCoordinates, precision);
                        } catch (RuntimeException var9) {
                            throw (new PerformException.Builder()).withActionDescription(this.getDescription()).withViewDescription(HumanReadables.describe(navigationView)).withCause(var9).build();
                        }

                        int duration = ViewConfiguration.getPressedStateDuration();
                        if(duration > 0) {
                            uiController.loopMainThreadForAtLeast((long)duration);
                        }
                    }
                    //End c/p from android/support/test/espresso/action/GeneralSwipeAction.class

                    if (returnAfterOneMoreSwipe) {
                        return;
                    }

                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                // timeout happens
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(new TimeoutException())
                        .build();
            }
        };
    }

您需要使用意式浓缩咖啡:

import android.support.test.espresso.contrib.DrawerActions;
要打开菜单,可以执行以下操作:

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
您可以向下滚动至菜单项并检查是否显示:

onView(withId(R.id.my_menu_item)).perform(scrollTo()).check(matches(isDisplayed()));
并单击菜单项:

onView(withId(R.id.my_menu_item)).perform(click());

即使菜单项不可见(在列表中的某个位置),这对我来说也是有效的


onView(带id(R.id.nav_视图))。执行(NavigationViewActions.navigateTo(R.id.nav_登录))

就像一个单行魅力!非常感谢。