Skip to content

Instantly share code, notes, and snippets.

@curioustechizen
Last active April 10, 2019 14:36
Show Gist options
  • Save curioustechizen/096ad82763669acc0b55 to your computer and use it in GitHub Desktop.
Save curioustechizen/096ad82763669acc0b55 to your computer and use it in GitHub Desktop.
DEPRECATED
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")));
}
}
@stMayhem
Copy link

onErrorViewWithinTilWithId doesn't workr for me

@curioustechizen
Copy link
Author

@stMayhem what problem do you see? Is the error view not found at all?
I have not updated this gist in 2 years and it is possible that the implementation of TextInputLayout has changed recently thus causing this code to break (you might have noted that it depends on implementation details of TIL).

@rogers2602
Copy link

here also does not work, it asks to create the class matches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment