14

I'm trying to type some text inside an EditText:


    public void testSearch() {
          onView(withId(R.id.titleInput)).perform(typeText("Engineer"));
          onView(withId(R.id.titleInput)).check(matches(withText("Engineer")));
    }

I see the EditText is getting focus, but nothing happens. No text is typed.
I tried this on a phone and an emulator - same result.

10 Answers 10

17

Looks like I figured out the issue. It had to do with hardware vs software keyboard.

For Emulators:

Go to Settings -> Language & Input -> switch the Default Input to Sample Soft Keyboard.

For Phones:

Install a software keyboard from the Play store and switch to it. It appears that the native keyboards of some phones do not work.

It works now.

1
  • Thanks! This worked for me. Finally, after an hour trying to figure out what could be wrong with my espresso tests :) Just had to change the keyboard.
    – Carmen
    Jun 10, 2017 at 13:37
15

Had the same issue using Espresso 2. As a workaround I'm using replaceText instead of typeText.

public void testSearch() {
      onView(withId(R.id.titleInput)).perform(click(), replaceText("Engineer"));
      onView(withId(R.id.titleInput)).check(matches(withText("Engineer")));
}
1
  • 1
    Using replaceText worked for me. There is definitely some bug in typeText Nov 7, 2018 at 20:00
14

If the EditText does not has the focus yet, you should click on it first. If this solves your problem, then there is no bug.

onView(withId(R.id.titleInput)).perform(click()).perform(typeText("Engineer"));
3
  • Nope. It was the first thing I tried. Same issue even if you click on it first.
    – Eduard
    Dec 16, 2013 at 18:03
  • Thanks, it solved my issue because I listen to focus changes.
    – Vince
    Apr 9, 2015 at 20:51
  • 5
    Not sure how Espresso was working in 2013, when you answered, but now the click/focus is taken care of by ViewActions.typeText()
    – tir38
    May 2, 2017 at 19:27
11
+50

You can bypass the problem by calling setText on the EditText.

   final EditText titleInput = (EditText) activity.findViewById(R.id.titleInput);
   getInstrumentation().runOnMainSync(new Runnable() {
        public void run() {
            titleInput.setText("Engineer");
        }
    });
4
  • Thank you. This definitely unblocks me, so I'll award you the bounty. However, I still would like to know why typeText is not working in this case.
    – Eduard
    Dec 12, 2013 at 22:48
  • 2
    The solution might work fine but I think ethically this is not the right way. When Espresso has revealed an API that lets you do this, then I think we should concentrate on the bug. Why not open a bug on their site or discuss on google groups.
    – Khushboo
    Dec 13, 2013 at 9:44
  • hi Eduard, I agree with Khushboo , you should file a bug on Espresso site. Perhaps, another widget in the screen has gained the focus.
    – skyleecm
    Dec 13, 2013 at 13:07
  • I had a test case where I have to include multiple spaces after the username and using Espresso's typeText was including a dot (default if you tap double spaces) so this solved my issue. Thanks @skyleecm Aug 8, 2019 at 16:27
5

Same issue resolved with the following:

editText.perform(scrollTo(), click(), clearText(), typeText(myInput), closeSoftKeyboard())

Interestingly, I only ever had a problem when my machine was working hard.

2
  • Thank you Mike, this work for me: myField.perform(scrollTo(), click(), clearText(), typeText(textToBeSet), closeSoftKeyboard()) Jul 23, 2019 at 11:14
  • Perfect answer, only one that reliably worked for me!
    – Jeehut
    Feb 9, 2020 at 15:30
3

You can include it with in the code like this,

onView(withId(R.id.titleInput))
     .perform(click(), replaceText("Engineer"), closeSoftKeyboard());
1

I fixed this issue by setting layout_height="wrap_content" on the View I wanted to click(). Maybe it can help someone here.

0

If you're using Genymotion, you may need to switch the default keyboard in Genymotion Configuration (it's an app on the emulator).

  1. Go to Apps -> Genymotion Configuration -> Keyboard -> Virtual keyboard (click "Yes" when you're prompted to reboot)

NOTE: These changes do not persist after you close the emulator. You will need to set this every time you start the emulator.

0

for me I was annotated my test method with @UiThreadTest. I removed that and it solved.

1
  • it doesn't help in this case you should receive another error if the issue was in threading Jul 28, 2020 at 17:45
0

Adding closeSoftKeyboard() after typeText() worked for me.

CODE:

onView(withId(R.id.editTextUserInput))
            .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());

This is how it is documented in the Android docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.