Skip to content

Instantly share code, notes, and snippets.

@maydin
Last active September 28, 2016 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maydin/246b7b3bb528ce4f9d546c8269e2ba86 to your computer and use it in GitHub Desktop.
Save maydin/246b7b3bb528ce4f9d546c8269e2ba86 to your computer and use it in GitHub Desktop.
@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void mainActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.textview), withText("Hello World"),
childAtPosition(
allOf(withId(R.id.text_switcher),
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.RelativeLayout.class),
1)),
0),
isDisplayed()));
textView.check(matches(withText("Hello World")));
ViewInteraction appCompatButton = onView(
allOf(withId(R.id.button), withText("Click"), isDisplayed()));
appCompatButton.perform(click());
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
ViewInteraction textView2 = onView(
allOf(withId(R.id.textview), withText("Bye World"),
childAtPosition(
allOf(withId(R.id.text_switcher),
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.RelativeLayout.class),
1)),
0),
isDisplayed()));
textView2.check(matches(withText("Bye World")));
}
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));
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment