当前位置: 首页 > 知识库问答 >
问题:

Espresso测试NestedScrollView-“执行”滚动到“on view”时出错,其ID为:“

仲君昊
2023-03-14

我需要向下滚动我的NestedScrollView以便用Espresso测试我的xml文件,但我得到错误消息:“错误执行'scroll to'on view',ID:”

现在我得到了前面提到的错误,并发现了这个帖子:试图滚动到视图,但视图没有显示

我的NestedScrollView中没有填充-为了测试目的,我甚至尝试从XML中完全删除填充,但没有什么不同。

这是我的测试(到目前为止,它不应该做任何事情,而是向下滚动):

    @Test
    public void testScrollDownAbilityOfDetailsScrollView(){
        goToSpecificItemOnStream(streamItemWithOneImage);

        onView(withId(R.id.end_of_details))
                .perform(ScrollToAction.betterScrollTo());

    }
    public final class ScrollToAction implements ViewAction {

    private static final String TAG = ScrollToAction.class.getSimpleName();

    @SuppressWarnings("unchecked")
    @Override
    public Matcher<View> getConstraints() {
        return allOf(withEffectiveVisibility(Visibility.VISIBLE), isDescendantOfA(anyOf(
                isAssignableFrom(ScrollView.class),    isAssignableFrom(HorizontalScrollView.class), isAssignableFrom(NestedScrollView.class))));
    }

    @Override
    public void perform(UiController uiController, View view) {
        if (isDisplayingAtLeast(80).matches(view)) {
            Log.i(TAG, "View is already displayed. Returning.");
            return;
        }
        Rect rect = new Rect();
        view.getDrawingRect(rect);
        if (!view.requestRectangleOnScreen(rect, true /* immediate */)) {
            Log.w(TAG, "Scrolling to view was requested, but none of the parents      scrolled.");
        }
        uiController.loopMainThreadUntilIdle();
        if (!isDisplayingAtLeast(80).matches(view)) {
            throw new PerformException.Builder()
                    .withActionDescription(this.getDescription())
                    .withViewDescription(HumanReadables.describe(view))
                    .withCause(new RuntimeException(
                            "Scrolling to view was attempted, but the view is not displayed"))
                    .build();
        }
    }
    public static ViewAction betterScrollTo() {
        return ViewActions.actionWithAssertions(new ScrollToAction());
    }

    @Override
    public String getDescription() {
        return "scroll to";
    }}
       <?xml version="1.0" encoding="utf-8"?>
       <android.support.v4.widget.NestedScrollView    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrollView"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">



            <FrameLayout
                android:id="@+id/fl"
                android:background="#FBFBFB"
                android:layout_margin="0dp"
                android:layout_width="match_parent"
                android:layout_height="350dp">

                <android.support.v4.view.ViewPager
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

                <ImageView
                    android:id="@+id/location"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/text_margin"
                    android:layout_gravity="left|top"
                    android:background="@null"
                    android:src="@drawable/ic_location_white"
                    android:paddingLeft="-8dp" />


                <TextView
                    android:id="@+id/textViewDistance"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@id/location"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_margin="@dimen/text_margin"
                    android:layout_gravity="left|top"
                    android:shadowColor="#262424"
                    android:shadowDx="1"
                    android:shadowDy="1"
                    android:shadowRadius="2"
                    android:textColor="#FBFBFB"
                    android:textSize="22dp"
                    android:singleLine="false"
                    android:paddingLeft="24dp" />


                <TextView
                    android:id="@+id/textViewPrice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:layout_margin="@dimen/text_margin"
                    android:layout_gravity="right|top"
                    android:shadowColor="#262424"
                    android:shadowDx="1"
                    android:shadowDy="1"
                    android:shadowRadius="2"
                    android:textColor="#FBFBFB"

                    android:textSize="22dp"/>

                <me.relex.circleindicator.CircleIndicator
                    android:id="@+id/indicator"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_gravity="bottom"
                    android:shadowColor="#262424"
                    android:shadowDx="1"
                    android:shadowDy="1"
                    android:shadowRadius="1"/>



            </FrameLayout>


            <LinearLayout

                android:layout_below="@id/fl"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                >

                <TextView
                    android:id="@+id/textViewTitle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:layout_margin="@dimen/text_margin"
                    android:textColor="@color/colorCheckTomBlack"
                    android:textStyle="bold"
                    android:textSize="20dp" />

                <TextView
                    android:id="@+id/textViewDescription"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/text_margin"
                    android:layout_marginRight="@dimen/text_margin"
                    android:gravity="left"
                    android:textColor="@color/colorCheckTomBlack"
                    android:textSize="18dp"
                    android:layout_weight="0.56" />

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="65dp"
                    android:paddingTop="30dp">


                    <ImageButton
                        android:id="@+id/buttonWatchlist"
                        android:src="@drawable/ic_checktom"
                        android:background="@null"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="left"
                        android:layout_marginLeft="55dp"
                        android:layout_marginStart="55dp"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:onClick="launchWatchlistActivity"
                        android:paddingTop="2dp"/>


                    <ImageButton
                        android:id="@+id/buttonMessage"
                        android:src="@drawable/ic_messages"
                        android:background="@null"
                        android:scaleX="1.2"
                        android:scaleY="1.2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true"
                        android:onClick="launchMessageActivity"
                        android:paddingTop="7dp"/>


                    <ImageButton
                        android:id="@+id/buttonShare"
                        android:src="@drawable/ic_share"
                        android:background="@null"
                        android:scaleX="1.5"
                        android:scaleY="1.5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="54dp"
                        android:layout_marginEnd="54dp"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        android:onClick="launchShareActivity"/>



                </RelativeLayout>


                <RelativeLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="2dp">


                    <TextView
                        android:id="@+id/textViewWatchlist"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Watchhtml" target="_blank">list"
                        android:layout_marginLeft="41dp"
                        android:layout_marginStart="41dp"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true" />


                    <TextView
                        android:id="@+id/textViewMessage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Message"
                        android:layout_gravity="center_horizontal"
                        android:layout_alignParentTop="true"
                        android:layout_centerHorizontal="true" />

                    <TextView
                        android:id="@+id/textViewShare"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Share"
                        android:layout_gravity="center_horizontal"
                        android:layout_alignParentTop="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true"
                        android:layout_marginRight="52dp"
                        android:layout_marginEnd="52dp" />


                </RelativeLayout>
                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="_________________________________________"
                    android:paddingTop="25dp"/>


                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/imageView"
                        android:layout_centerHorizontal="true"
                        android:id="@+id/circleView"
                        android:scaleX="0.4"
                        android:scaleY="0.4"
                        android:layout_marginTop="-20dp"
                        android:layout_marginBottom="-60dp"
                        />       

                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center">     
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center">     
                    <TextView
                        android:id="@+id/textViewSellerName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="18dp"/>     
                        </LinearLayout>        
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center">     
                    <TextView
                        android:id="@+id/textViewSellerDestination"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingBottom="20dp" />     
                        <TextView
                            android:id="@+id/end_of_details"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                        </LinearLayout>        
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </android.support.v4.widget.NestedScrollView>

Android.support.test.espresso.PerformException:执行“滚动到'on view'时ID:com.checktom.checktom:id/end_of_details”时出错。在Android.support.test.espresso.performException$builder.build(PerformException.java:83)在Android.support.test.espresso.base.defaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:80)在Android.support.test.espresso.base.defaultFailureHandler.handle(DefaultFailureHandler.java:56)在在Android.support.test.espresso.viewInteraction.perform(ViewInteraction.java:87)在com.checktom.checktom.applicationtest.testScrolldownabilityofdetailsScrollView(applicationTest.java:279)在java.lang.reflect.Method.invoke(原生方法)在java.lang.reflect.Method.invoke(Method.java:372)在V在org.junit.internal.runners.statements.invokeMethod.evaluate(invokeMethod.java:17)在org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:26)在android.support.test.statement.uithreadStatement.evaluate(uithreadStatement.java:55)在android.support.test.rule.activityStatement.evaluate(parentRunner.java:325)在org.junit.runners.blockJunit4ClassRunner.runchild(blockJunit4ClassRunner.java:78)在org.junit.runners.blockJunit4ClassRunner.runchild(blockJunit4ClassRunner.java:57)在org.junit.runners.parentRunner$3.run(parentRunner.java:57)在org.junit.runners.parentRunner$3.run(.Evalu在org.junit.runners.parentrunner.run(parentrunner.java:363)在org.junit.runners.suite.runchild(suite.java:128)在org.junit.runners.suite.runchild(suite.java:27)在org.junit.runners.parentrunner$3.run(parentrunner.java:290)在org.junit.runners.parentrunner$1在org.junit.runners.parentrunner.junit.runners.parentrunner$2.在org.junit.runners.parentrunner.run(parentrunner.java:268)在org.junit.runner.junitcore.run(junitcore.java:363),在org.junit.runner.junitcore.run(junitcore.java:137),在org.junit.runner.junitcore.run(junitcore.java:115),在Instrumentation.java:1933)由:java.lang.RuntimeException引起:滚动到尝试查看,但该视图未显示在com.checktom.checktom.scrolltoaction.perform(Scrolltoaction.java:52),android.support.test.espresso.viewinteraction$1.run oper.java:145),android.app.activitythread.main(activitythread.java:6938),java.lang.reflect.Method.invoke(原生方法),java.lang.reflect.Method.invoke(Method.java:372),com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1199),测试运行到完成。

我可以看出,我在ScrollToAction类的perform方法中遇到了运行时异常,但我还没有找到解决它的方法。

在我链接的第一篇文章中,新的BetterScrollTo方法似乎非常有效。

共有1个答案

农建弼
2023-03-14

我是这么做的:

onView(withId(R.id.viewToScroll)
                .perform(nestedScrollTo())
                .check(matches(isDisplayed()));

其中nestedscrollto()为:

public static ViewAction nestedScrollTo() {
    return new ViewAction() {

        @Override
        public Matcher<View> getConstraints() {
            return Matchers.allOf(
                    isDescendantOfA(isAssignableFrom(NestedScrollView.class)),
                    withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE));
        }

        @Override
        public String getDescription() {
            return "View is not NestedScrollView";
        }

        @Override
        public void perform(UiController uiController, View view) {
            try {
                NestedScrollView nestedScrollView = (NestedScrollView)
                        findFirstParentLayoutOfClass(view, NestedScrollView.class);
                if (nestedScrollView != null) {
                    nestedScrollView.scrollTo(0, view.getTop());
                } else {
                    throw new Exception("Unable to find NestedScrollView parent.");
                }
            } catch (Exception e) {
                throw new PerformException.Builder()
                        .withActionDescription(this.getDescription())
                        .withViewDescription(HumanReadables.describe(view))
                        .withCause(e)
                        .build();
            }
            uiController.loopMainThreadUntilIdle();
        }

    };
}

private static View findFirstParentLayoutOfClass(View view, Class<? extends View> parentClass) {
    ViewParent parent = new FrameLayout(view.getContext());
    ViewParent incrementView = null;
    int i = 0;
    while (parent != null && !(parent.getClass() == parentClass)) {
        if (i == 0) {
            parent = findParent(view);
        } else {
            parent = findParent(incrementView);
        }
        incrementView = parent;
        i++;
    }
    return (View) parent;
}

private static ViewParent findParent(View view) {
    return view.getParent();
}

private static ViewParent findParent(ViewParent view) {
    return view.getParent();
}
 类似资料:
  • 我有一个应用程序(使用注释的Spring 4 MVC Hibernate 4 MySQL Maven集成示例),使用基于注释的配置将Spring与Hibernate集成,但运行测试时出错! 上课时间到了 下面是即将到来的考验: 这是pom。xml文件:

  • 在我的Espresso测试执行和AlertDialog会被提示并等待用户响应。Espresso测试只有在我按下“Accept”按钮但我想测试代替用户执行此事件时才会继续。 我注意到我的线程选项卡上有以下状态

  • 首先,我已经关闭了所有的动画设置: 窗口动画比例 过渡动画比例 动画师持续时间刻度 最近,我需要为一个旧的Android项目(5年以上)添加Espresso测试用例,我没有足够的时间来挖掘所有的自定义视图。 浓缩咖啡盒: 然后它启动了应用程序,我确实看到了活动中的所有元素,但浓缩咖啡无限等待。

  • 当我试图从Spring Boot应用程序中调用JUnit测试用例时,我遇到了以下错误: 我在测试类中使用了这个注释。 @RunWith(SpringRunner.class) 在我从项目依赖项添加JUnit库后,它给出了以下错误: 我的Spring启动版本是2.0.4。编辑器是SpringToolSuite。有人能帮我解决这个问题吗? 谢谢