android – Robolectric/Espresso: How to perform typing a newline in EditText?-ThrowExceptions

Exception or error:

I use Robolectric (4.3.1) with Espresso (3.2.0) for activity tests.

I want to perform the following actions on my EditText view:

  • Type some text
  • Press enter
  • Type some other text

I tried doing this using:

                    typeText("Some text."),
                    typeTextIntoFocusedView("Other text."));

(the tested activity is launched via an ActivityScenario)

The resulting string is “Some text.Other text” but I was expecting “Some text.\nOther text”.

(I also tried pressImeActionButton() with the same result)

When I convert my test to a real instrumentation test without Robolectric I get the newline character as expected.

Is this a known issue in Robolectric? Or am I doing something wrong?

EDIT: I created a Github repo which reproduces the issue (along with a similar problem that I encountered when I tried to use closeSoftKeyboard()):

How to solve:

Unless you changed the behavior of KEYCODE_enter / and you are using the default keyboard it should write the other text to next line. I tried this myself and it seems to work.

  public void testNew2() {
        typeText("Some text."),
        typeTextIntoFocusedView("Other text."));;
    assertDisplayed("Some text. \nOther text. "); //Use your own matcher

Realize that even if your app is writing the text correctly to next line, your assertion will still be wrong, since there is an extra space before \n.
This is the edittext I used. To be safe and not care about spaces you can also use Contains


As long as the behavior of keyboard your edittext has isn’t changed or it is from a vendor I don’t know this should work. Did you check if text is written to one line or did you just assume it is in one line because your assertion didn’t work?

Leave a Reply

Your email address will not be published. Required fields are marked *