title: accessing children in custom expandablelist using android espresso
body: <p>I am trying to access the children in my custom expandablelist. The list looks like this :</p>
  
  <p><a href="http://foodaversions.com/aversions_cropped.png" rel="noreferrer">Custom ExpandableList</a></p>
  
  <p>I can successfully access the groups with :</p>
  
  <pre><code>Espresso.onView(allOf(withId(R.id.groupRowTextView1), withText(startsWith("Candida")))).perform(click());
  </code></pre>
  
  <p>When I try to access the child EditText like :</p>
  
  <pre><code>    Espresso.onData(is(instanceOf(CustomExpandableListAdapter.class)))
              .inAdapterView(withId(R.id.aversionsListView))
              .onChildView(allOf(withId(R.id.childEditText), isDisplayed()))
              .atPosition(1)
              .perform(click());
  </code></pre>
  
  <p>I get this error :</p>
  
  <pre><code>11-30 16:47:45.733    2479-2492/com.foodaversions.people I/TestRunner﹕ ----- begin exception -----
  11-30 16:47:45.733    2479-2492/com.foodaversions.people I/TestRunner﹕ com.google.android.apps.common.testing.ui.espresso.PerformException: Error performing 'load adapter data' on view 'with id: is &lt;2131099682&gt;'.
          at com.google.android.apps.common.testing.ui.espresso.PerformException$Builder.build(PerformException.java:67)
          at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:57)
          at com.google.android.apps.common.testing.ui.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:40)
          at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:159)
          at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.doPerform(ViewInteraction.java:90)
          at com.google.android.apps.common.testing.ui.espresso.ViewInteraction.perform(ViewInteraction.java:73)
          at com.google.android.apps.common.testing.ui.espresso.DataInteraction.load(DataInteraction.java:135)
          at com.google.android.apps.common.testing.ui.espresso.DataInteraction.perform(DataInteraction.java:112)
          at com.foodaversions.people.ModifyAversionsTest.selectGlutenIntolerance(ModifyAversionsTest.java:49)
          at com.foodaversions.people.ModifyAversionsTest.test(ModifyAversionsTest.java:20)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:511)
          at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
          at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
          at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
          at junit.framework.TestCase.runBare(TestCase.java:134)
          at junit.framework.TestResult$1.protect(TestResult.java:115)
          at junit.framework.TestResult.runProtected(TestResult.java:133)
          at junit.framework.TestResult.run(TestResult.java:118)
          at junit.framework.TestCase.run(TestCase.java:124)
          at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
          at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
          at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
          at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
          at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
   Caused by: java.lang.RuntimeException: No data found matching: is an instance of com.foodaversions.people.adapters.CustomExpandableListAdapter contained values: &lt;[Data: GroupStruct{id=6, name='Alcohol Intolerance', sourceTable=AVERSION_NAME, modifierEnum=EDITABLE, childData=
  ChildStruct{id=-1, name='&lt;Add Ingredient&gt;', sourceTable=null, joinTable=null, modifierEnum=ADD_TEXT_VIEW}
  }
  (class: class com.foodaversions.people.data.ExpandableGroupData$GroupStruct) token: 0, Data: GroupStruct{id=8, name='Candida', sourceTable=AVERSION_NAME, modifierEnum=EDITABLE, childData=
  ChildStruct{id=13, name='Sugar', sourceTable=AVERSION, joinTable=null, modifierEnum=DELETE}
  ChildStruct{id=-1, name='&lt;Add Ingredient&gt;', sourceTable=null, joinTable=null, modifierEnum=ADD_TEXT_VIEW}
  }
  (class: class com.foodaversions.people.data.ExpandableGroupData$GroupStruct) token: 1, Data: ChildStruct{id=13, name='Sugar', sourceTable=AVERSION, joinTable=null, modifierEnum=DELETE} (class: class com.foodaversions.people.data.ChildStruct) token: 2, Data: ChildStruct{id=-1, name='&lt;Add Ingredient&gt;', sourceTable=null, joinTable=null, modifierEnum=ADD_TEXT_VIEW} (class: class com.foodaversions.people.data.ChildStruct) token: 3, Data: GroupStruct{id=3, name='Dairy Intolerance', sourceTable=AVERSION_NAME, modifierEnum=EDITABLE, childData=
  ChildStruct{id=-1, name='&lt;Add Ingredient&gt;', sourceTable=null, joinTable=null, modifierEnum=ADD_TEXT_VIEW}
  }
  (class: class com.foodaversions.people.data.ExpandableGroupData$GroupStruct) token: 4, Data: GroupStruct{id=10, name='Egg Allergy', sourceTable=AVERSION_NAME, modifierEnum=EDITABLE, childData=
  ChildStruct{id=-1, name='&lt;Add Ingredient&gt;', sourceTable=null, joinTable=null, modifierEnum
  11-30 16:47:45.733    2479-2492/com.foodaversions.people I/TestRunner﹕ ----- end exception -----
  </code></pre>
  
  <hr>
  
  <h2>Expanding the Group using onData</h2>
  
  <p>I had trouble getting the child click to work, I found it easier to work out how to expand the list first :</p>
  
  <pre><code>    Espresso.onData(allOf(is(instanceOf(ExpandableGroupData.GroupStruct.class)), withGroupName("Candida")))
              .inAdapterView(withId(R.id.aversionsListView))
              .perform(click());
  </code></pre>
  
  <p>I thought it might help towards the solution for clicking the child view.</p>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>Thanks to @Anna for providing the direction I needed to solve this:</p>
  
  <pre><code>    // aversionsListView is the id of the ExpandableListView containing the expandable list
      Espresso.onData(allOf(is(instanceOf(ChildStruct.class)), withChildName("&lt;Add Ingredient&gt;")))
              .inAdapterView(withId(R.id.aversionsListView))
              .check(matches(isDisplayed()))
              .perform(click());
  </code></pre>
  
  <p>matcher :</p>
  
  <pre><code>public static Matcher&lt;Object&gt; withChildName(String name) {
      checkNotNull(name);
      return withChildName(equalTo(name));
  }
  
  public static Matcher&lt;Object&gt; withChildName(final Matcher&lt;String&gt; name) {
      checkNotNull(name);
      // ChildStruct is the Class returned by BaseExpandableListAdapter.getChild()
      return new BoundedMatcher&lt;Object, ChildStruct&gt;(ChildStruct.class){
  
          @Override
          public boolean matchesSafely (final ChildStruct childStruct){
              return name.matches(childStruct.name);
          }
  
          @Override
          public void describeTo (Description description){
              name.describeTo(description);
          }
      } ;
  }
  </code></pre>