2016-09-22 6 views
5

Espresso로 XML 파일을 테스트하기 위해 NestedScrollView를 아래로 스크롤해야하지만 오류 메시지가 표시됩니다. '보기의'id : "Espresso NestedScrollView - "id '가있는'보기 '에서'스크롤 '오류 발생 :

다른 게시물에 비슷한 문제가있는 것으로 보입니다. 이에서

내가 따랐다 설명 : Android espresso NestedScrollView, how to scroll to bottom

지금 나는 forementioned 오류가 발생하고,이 게시물을 발견 : 내 NestedScrollView에 패딩이없는 Scrolling to view was attempted, but the view is not displayed

를 - 난 alltogether는 XML에서 패딩을 제거하려 테스트 목적으로 사용되었지만 차이는 없습니다.

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

사용자 정의 :

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

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

    } 

그것은 Action 클래스로 스크롤 만든 사용자 정의를 사용

는 (지금까지이 아무것도 할 수 있지만 아래로 스크롤 안됩니다) 내 테스트입니다 일반 scrollTo 메서드는 ScrollView 및 HorizontalScrollView에 하드 코드되지만 NestedScrollView에는 하드 코딩되지 않기 때문에 만든 ScrollToAction 클래스가 있습니다.

android.support.test.espresso : 테스트가 실패하면

 <?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="Watchlist" 
         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> 

, 이것은 내가 얻을 전체 출력 :

이것은 내가 테스트를 시도하고있는 XML 파일입니다. PerformException : 'id : com.checktom.checktom : id/end_of_details'의 '보기'에서 '스크롤'을 수행하는 중 오류가 발생했습니다. android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError (DefaultFailureHandler.java:80) android.support.test.espresso.PerformException에서 을 android.support에 에서 수행합니다. test.espresso.base.DefaultFailureHandler.handle (DefaultFailureHandler.java:56) android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread (ViewInteraction.java:184)에서 android.support.test.espresso.ViewInteraction.doPerform ViewInteraction.java:115) android.support.test.espresso.ViewInteraction.perform (ViewInteraction.java:87) at com.checktom.checktom.ApplicationTest.testScrollDownAbilityOfDetailsScrollView (ApplicationTest.java:279) at java.lang. Invoke (네이티브 메소드) at java.lang.reflect.Method.invoke (Method.java:372) at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall (FrameworkMethod.java:50) at org.junit.internal.runners.model .ReflectiveCallable.run (ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively (FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate (InvokeMethod.java : 17) at org.junit.internal.runners.statements.RunBefores.evaluate (RunBefores.java:26) android.support.test.internal.statement.UiThreadStatement.evaluate (UiThreadStatement.java:55) at android .support.test.rule.ActivityTestRule $ ActivityStatement.evaluate (ActivityTestRule.java:270) at org.junit.rules.RunRules.evaluate (RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf (ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.자바 : 78) org.junit.runners.BlockJUnit4ClassRunner.runChild (BlockJUnit4ClassRunner.java:57) 에서 org.junit.runners.ParentRunner $ 3.run (ParentRunner.java:290에서) org.junit.runners.ParentRunner에서 $ 1.schedule (ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren (ParentRunner.java:288) at org.junit.runners.ParentRunner.access $ 000 (ParentRunner.java:58) at org .junit.runners.ParentRunner $ 2.evaluate (ParentRunner.java:268) at org.junit.runners.ParentRunner.run (ParentRunner.java:363) at org.junit.runners.Suite.runChild (Suite.java : 128) at org.junit.runners.Suite.runChild (Suite.java:27) at org.junit.runners.ParentRunner $ 3.run (ParentRunner.java:290) at org.junit.runners.ParentRunner $ 1.schedule (ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren (ParentRunner.java:288) at org.junit.runners.ParentRunner.access $ 000 ParentRunner.java:58) at org.junit.runners.ParentRunner $ 2.evaluate (ParentRunner.java:268) at org.junit.runners.ParentRunner.run (ParentRunner.java:363) at org.junit.runner .JUnitCore.run (JUnitCore.java:137) at org.junit.runner.JUnitCore.run (JUnitCore.java:115) android.support.test.internal.runner.TestExecutor.execute (TestExecutor.java:59)) android.support.test.runner.AndroidJUnitRunner.onStart (AndroidJUnitRunner.java:262) android.app.Instrumentation $ InstrumentationThread.run (Instrumentation.ja) va : 1933) 발생 원인 : java.lang.RuntimeException :보기로 스크롤하려고 시도했지만보기가 표시되지 않음 at com.checktom.checktom.ScrollToAction.perform (ScrollToAction.java:52) android.support. test.espresso.ViewInteraction $ 1.run (ViewInteraction.java:144) at java.util.concurrent.Executors $ RunnableAdapter.call (Executors.java:422) at java.util.concurrent.FutureTask.run (FutureTask.java : 237) android.os.Handler.handleCallback (Handler.java:739) at android.os.Handler.dispatchMessage (Handler.java:95) at android.os.Looper.loop (Looper.java:145)) android.app.ActivityThread.main (ActivityThread.java:6938) at java.lang.reflect.Method.invoke (기본 메소드) at java.lang.reflect.Method.invoke (Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1404) at com.android.internal.os. ZygoteInit.main (ZygoteInit.java:1199) 테스트가 완료되었습니다.

내 ScrollToAction 클래스의 perform 메서드에서 런타임 예외가 발생한다고 알 수 있지만 해결 방법을 아직 찾지 못했습니다.

내가 링크 된 첫 번째 게시물에서는 새로운 BetterScrollTo 메서드가 매력처럼 작동하는 것처럼 보였습니다.

+1

시도 이 http://stackoverflow.com/questions/35272953/espresso-scrolling-not-working-when-nestedscrollview-or-recyclerview-is-in-coor –

+1

그 게시물 중 하나입니다, 나는 이미 따라했습니다. 하지만 고마워. :) 그것은 두 가지를 제안한다.첫째, ScrollToAction 클래스를 직접 작성해야하며, CoordinatorLayout 내부의 NestedScrollView가 문제를 일으킬 수 있지만 NestedScrollView는 CoordinatorLayout 내부에 있지 않아야합니다. 감사합니다. – Silas

+0

편집 : 부모님을 통해 NestedScrollView를 살펴본 후 사실 CoordinatorLayout 내부에 있음을 알게되었습니다. CoordinatorLayout의 직접적인 자식은 아니기 때문에 게시물에 제안 된 getParent() 메서드는 저에게 효과적이지 않습니다. – Silas

답변

0

당신이 중첩 된 스크롤에 대한 코드를 얻기 위해 새로운 에스프레소 테스트 레코더 사용할 수 있습니다 - 체크 아웃 Espresso Test Recorder

1

나는이 한 : nestedScrollTo()가 어디

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

을 :

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