Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save LOG-TAG/79e0731fe69c7ab333b6 to your computer and use it in GitHub Desktop.
Save LOG-TAG/79e0731fe69c7ab333b6 to your computer and use it in GitHub Desktop.
Espresso utils for TextInputLayout
class EspressoTextInputLayoutUtils{
/*
* Use this method to find the EditText within the TextInputLayout. Useful for typing into the TextInputLayout
*/
public static ViewInteraction onEditTextWithinTilWithId(@IdRes int textInputLayoutId) {
//Note, if you have specified an ID for the EditText that you place inside
//the TextInputLayout, use that instead - i.e, onView(withId(R.id.my_edit_text));
return onView(allOf(isDescendantOfA(withId(textInputLayoutId)), isAssignableFrom(EditText.class)));
}
/*
* Use this method to find the error view within the TextInputLayout. Useful for asseting that certain errors are displayed to the user
*/
public static ViewInteraction onErrorViewWithinTilWithId(@IdRes int textInputLayoutId) {
return onView(allOf(isDescendantOfA(withId(textInputLayoutId)), not(isAssignableFrom(EditText.class)), isAssignableFrom(TextView.class)));
}
//Example usage
@Test
public void testUsernameTooShortShowsError(){
onEditTextWithinTilWithId(R.id.tilUsername).perform(typeText("user123"), closeSoftKeyboard());
onView(withId(R.id.sign_up)).perform(click());//Assume there is a button that you click
onErrorViewWithinTilWithId(R.id.tilUsername).check(matches(withText("Username too short")));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment