I am writing the test cases for the Fragment. I can able to set a text to the Edittext but am unable to setText to the text view from testing. Please, anyone, help me to resolve this issue.Bellow is my code for edit text.
onView(withId(R.id.editText))
.perform(typeText("my text"), closeSoftKeyboard());
Please help me how to set a text to the text view.
It's happening due to the precondition of typeText i.e.
View preconditions:
must be displayed on screen
must support input methods
Input method is not supported by TextView
(user cannot input values into it via keyboard or something else) so hence the issue
Solution : you can implement your own view ViewAction
From how to create View action to set text on TextView
public static ViewAction setTextInTextView(final String value){
return new ViewAction() {
@SuppressWarnings("unchecked")
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(TextView.class));
// ^^^^^^^^^^^^^^^^^^^
// To check that the found view is TextView or it's subclass like EditText
// so it will work for TextView and it's descendants
}
@Override
public void perform(UiController uiController, View view) {
((TextView) view).setText(value);
}
@Override
public String getDescription() {
return "replace text";
}
};
}
then you can do
onView(withId(R.id.editText))
.perform(setTextInTextView("my text"));