7

I wrote an Espresso test that writes some text to a TextView, performs an action and then checks whether the text in the TextView is still the same.

The test fails on one of the test devices (Huawei P20, Android 8.1.0) because the entered text is auto-corrected (from 1234 5678 to 12th 5678). And this fails my test. The text is not auto-corrected when I manually enter the same numbers.

This is how I input the text in my Espresso test:

onView(withId(R.id.reference_value))
            .perform(scrollTo(), click())
            .check(matches(isDisplayed()))
            .perform(typeText("1234 5678"));
        closeSoftKeyboard();

I know I could just change the input text to something that won't be auto-corrected. But I would like to have a solution that generally makes sure that the entered text is not modified to something else. Ideally without having to manually change the configuration of my test device.

Do any of you guys have an idea how I could accomplish this?

arne.z
  • 3,242
  • 3
  • 24
  • 46

2 Answers2

5

One way that works for me is to use replaceText() instead, although this still seems to be a bit of a hack.

Another option may be to disable autocorrect through some Android API call or manually through the UI like for animations.

Lee Kang
  • 729
  • 9
  • 19
2

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.

Mayur Gajra
  • 8,285
  • 6
  • 25
  • 41