2014-12-11 2 views
4

항목의 여러 인스턴스가 있고 그 항목을 계산하려는 경우가 있습니다. 내보기 정규 표현은 다음과 같다 : 뷰 정규으로어댑터보기에없는 동일한 ID를 가진 항목의 수를 얻는 방법

public static ViewInteraction onTestPanelView(){ 
     return onView(allOf(withId(R.id.myId), isDisplayed())); 
    } 

, 나는 다음과 같은 오류 얻을 : 나는 같은 ID를 가진 항목의 여러 인스턴스를 가지고 있기 때문에

com.google.android.apps.common.testing.ui.espresso.AmbiguousViewMatcherException: '(with id: is <2131427517> and is displayed on the screen to the user)' matches multiple views in the hierarchy. Problem views are marked with '****MATCHES****' below.

이 올바른지를 (R.를 id.myId). 내 기준과 일치하는 조회수를 반환하는 메소드를 작성하려고합니다. 어댑터보기가 아닙니다.

답변

0

이전 질문이지만 잠재적으로 누군가 유용 할 것입니다.

here에서 당신은 그것을 찾을 수 있습니다 : 당신이 아래

In the vast majority of cases, the onView method takes a hamcrest matcher that is expected to match one — and only one — view within the current view hierarchy. Matchers are powerful and will be familiar to those who have used them with Mockito or Junit. If you are not familiar with hamcrest matchers, we suggest you start with a quick look at this presentation.

정확히 하나 개의보기를 찾는 예제와 설명을 찾을 수 있습니다. 나는 그것을하기 위해 나의 테스트를 조정했다. 그러나 상위 컨테이너를 찾고 일부 인스턴스의 조회수를 계산하는 것과 같은 임시 해결책이있을 수 있습니다.

suggestion은 도움이되지 않습니다. 안드로이드 에스프레소에 대한

+0

실제로 ViewAction을 작성하면 가능합니다. viewAction을 작성하고, 뷰 매처를 전달하고, 일치하는 횟수를 셀 수 있습니다! – user846316

0

당신이 정규로 onView()에 배치 상태를 포장하고 정규 내부 카운터를 넣을 수 있습니다. 매처가 항목과 일치 할 때마다 카운터가 증가해야합니다.

int counter = 0; 

public static Matcher<View> withIdAndDisplayed(final int id) { 
    Checks.checkNotNull(id); 
    return new TypeSafeMatcher<View>() { 
     @Override 
     public void describeTo(Description description) { 
      description.appendText("with item id: " + id); 
     } 

     @Override 
     public boolean matchesSafely(View view) { 
      if ((view.getId() == id) && (view.getGlobalVisibleRect(new Rect()) 
        && withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE).matches(view))){ 
       counter++; 
       return true; 
      } 
      return false; 
     } 
    }; 
} 

public static ViewInteraction onTestPanelView(){ 
    return onView(withIdAndDisplayed(R.id.myId)); 
} 
관련 문제