-1

I'm hoping some could help shed some light custom view machers for me. I ran the Record Espresso Test and android-studio generated the following code.

    ViewInteraction textInputEditText = onView(
            allOf(withId(R.id.edit_text_last_name),
                    childAtPosition(
                            allOf(withId(R.id.layout_item_viewpager),
                                    childAtPosition(
                                            withId(R.id.layout_my_activity),
                                            0)),
                            1),
                    isDisplayed()));
    textInputEditText.perform(replaceText("a"), closeSoftKeyboard());
}

private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}

The code generated by Record Espresso Test works, I'm not sure how the child views work or what the 0 and 1 do in this code block.

I've read a fair amount of information on Hamcrest matchers and how to creat custom Matchers but I'm having a hard time finding material that relates to the android generated code. Any help here would be greatly appreciated.

Shawn
  • 1,222
  • 1
  • 18
  • 41

1 Answers1

0

The 0 and 1 are indices of multiple returned matches for a given ID. Let's work your code from the inside out.

  1. Find an activity with your ID layout_my_activity.
  2. childAtPosition with an index of 0 says to use the first match returned by that (indices are 0-based). This probably isn't necessary as it's unlikely (impossible?) you have multiple activities running with the same ID, but the test recorder likes to be as explicit as possible.
  3. Inside that, look for IDs matching layout_item_viewpager.
  4. childAtPosition with an index of 1 says to use the second one.
  5. Inside that second view pager, find something with the ID of edit_text_last_name, which is the field you want to put an a in.
Mike Collins
  • 4,108
  • 1
  • 21
  • 28
  • I'll have to check this again but I'm pretty sure when I ran the espresso test I got 1 from the EditTextOne and 2 from EditText2 all in the same ViewPager. – Shawn Oct 21 '19 at 19:10
  • Ironically, I have similar code to this that I'm currently working on. It worked flawlessly yesterday, but not today. Ugh. – Mike Collins Oct 22 '19 at 23:06
  • And my code is working again today :P Love this stuff. Don't for a second wish I had stuck with Appium. Nope, not a chance. – Mike Collins Oct 23 '19 at 20:50