This issue occurs with some keyboards. For me it was with Microsoft SwiftKey Keyboard was set as my default keyboard.
Two solutions that worked for me:
Solution 1: Change input type to TYPE_TEXT_FLAG_NO_SUGGESTIONS
during test if using typeText
which disables the suggestions & corrections for that view.
Example:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {
@Rule
public ActivityScenarioRule<MainActivity> activityScenarioRule
= new ActivityScenarioRule<>(MainActivity.class);
@Before
public void setUp() {
// get the view & set the input type
activityScenarioRule.getScenario().onActivity(activity ->
((EditText) activity.findViewById(R.id.etHello))
.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS));
}
@Test
public void testText() {
onView(withId(R.id.etHello)).perform(typeText("Smoth go"));
onView((withId(R.id.etHello))).check(matches(withText("Smoth go")));
}
}
Solution 2: Using my own ViewAction
Helper.java
-> This file is placed inside the same test package with test.
public class Helper {
public static ViewAction setTextInEt(final String value){
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return allOf(isDisplayed(), isAssignableFrom(EditText.class));
}
@Override
public void perform(UiController uiController, View view) {
((EditText) view).setText(value);
}
@Override
public String getDescription() {
return "set text";
}
};
}
}
Test class:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {
@Rule
public ActivityScenarioRule<MainActivity> activityScenarioRule
= new ActivityScenarioRule<>(MainActivity.class);
@Test
public void testText() {
onView(withId(R.id.etHello)).perform(Helper.setTextInEt("Smoth go"));
onView((withId(R.id.etHello))).check(matches(withText("Smoth go")));
}
}
So far solution 2 has worked very well for me. Before that smoth
text was auto-corrected to smooth
every time.
This can be done for the TextView
as well just replace EditText
to TextView
in helper method.