Autogenerator test Espresso
I created a simple test using espresso recording, the textview value will change when the "Hello World" button is clicked on "Button Clicked!" The generated code looks like this:
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void mainActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.tv), withText("Hello World!"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
1),
isDisplayed()));
textView.check(matches(withText("Hello World!")));
ViewInteraction button = onView(
allOf(withId(R.id.button), withText("Button"), isDisplayed()));
button.perform(click());
ViewInteraction textView2 = onView(
allOf(withId(R.id.tv), withText("Button Clicked!!"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
1),
isDisplayed()));
textView2.check(matches(withText("Button Clicked!!")));
}
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));
}
};
}
}
This test will fail
android.support.test.espresso.NoMatchingViewException: no views in found hierarchy: (with id: com.mysampleapps.mytestproject: id / tv and with text: "Hello World!" and Child at position 1 in parent Child at position 0 at parent with id: android: id / content and is displayed on the screen to the user)
If I remove the "child" from the test case as
public void mainActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.tv), withText("Hello World!"),
isDisplayed()));
textView.check(matches(withText("Hello World!")));
ViewInteraction button = onView(
allOf(withId(R.id.button), withText("Button"), isDisplayed()));
button.perform(click());
ViewInteraction textView2 = onView(
allOf(withId(R.id.tv), withText("Button Clicked!!"),
isDisplayed()));
textView2.check(matches(withText("Button Clicked!!")));
}
It will work fine now.
Is this an espresso bug?
source to share
I believe this is a bug in the code generator for Espresso in Android Studio. I see the same thing. Although I was unable to locate the pattern, I think it might only be counting specific elements in the layout (not all of them). For example, I included three guides in my layout (and they were located at the top of the layout) and the score generated for the first view was disabled by 3.
What I am doing is not using this matcher when I know I don’t need it, but in cases where it’s useful, I map the score based on 0 to the Nth element in a particular layout. This seems to work (although fragile), all I have to do is change the order in the layout file, and now it breaks).
source to share