浓咖啡AmbiguousViewMatcherException

问题描述:

我在尝试编写代码时交互ListView项目时遇到AmbiguousViewMatcherException异常。情景如下。浓咖啡AmbiguousViewMatcherException

我有两个观点

  1. 的TextView
  2. buttonView

我已经在列表中几乎250行的列表视图。所有按钮都有文字“预订”或“取消”。他们正在洗牌。我想直接点击Espresso点击列表中的第一个“预订”按钮。我已经尝试了很多场景,但仍无法解决此问题。请有人帮助我。

以下是我的代码现在

onView(withId(R.id.List)) 
       .check(matches(withAdaptedData(withItemContent("Book it")))); 

///////////////////////////////// ///////////////////////

private static Matcher<View> withAdaptedData(final Matcher<Object> dataMatcher) { 
     return new TypeSafeMatcher<View>() { 
      @Override 
      public void describeTo(Description description) { 
       description.appendText("with class name: "); 
       dataMatcher.describeTo(description); 
      } 

      @Override 
      public boolean matchesSafely(View view) { 
       if (!(view instanceof AdapterView)) { 
        return false; 
       } 
       @SuppressWarnings("rawtypes") 
       Adapter adapter = ((AdapterView) view).getAdapter(); 
       for (int i = 0; i < adapter.getCount(); i++) { 
        if (dataMatcher.matches(adapter.getItem(i))) { 
         return true; 
        } 
       } 
       return false; 
      } 
     }; 
    } 

//////////////////// ////////////////////////////////////////

android.support。 test.espresso.AmbiguousViewMatcherException:'与id: com.bottegasol。 com.migym.EmpireSportFit:id/List'在层次结构中匹配多个 视图。问题视图在下方标有 '**** MATCHES ****'。

下面是一个例子,我发现,希望它有助于:

public void testClickOnFirstAndFifthItemOfLength8() { 
    onData(is(withItemSize(8))) 
     .atPosition(0) 
     .perform(click()); 
    onView(withId(R.id.selection_row_value)) 
     .check(matches(withText("10"))); 
    onData(is(withItemSize(8))) 
     .atPosition(4) 
     .perform(click()); 
    onView(withId(R.id.selection_row_value)) 
     .check(matches(withText("14"))); 
    } 

来源: https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/sample/src/androidTest/java/android/support/test/testapp/AdapterViewTest.java

根据错误信息,您遇到的ID等于R.id.List多个视图。您应该首先检查视图层次结构,并将列表ID(id/List)替换为您要匹配的视图的唯一ID。