16

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.

tomrozb
  • 25,773
  • 31
  • 101
  • 122
Eduard
  • 3,482
  • 2
  • 27
  • 45

10 Answers10

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.

Eduard
  • 3,482
  • 2
  • 27
  • 45
  • 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 '17 at 13:37
17

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")));
}
dorr
  • 1,509
  • 1
  • 11
  • 8
16

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"));
skyleecm
  • 437
  • 2
  • 4
  • Nope. It was the first thing I tried. Same issue even if you click on it first. – Eduard Dec 16 '13 at 18:03
  • Thanks, it solved my issue because I listen to focus changes. – Vince Apr 09 '15 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 02 '17 at 19:27
11

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");
        }
    });
skyleecm
  • 437
  • 2
  • 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 '13 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 '13 at 09: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 '13 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 – Viraj Gaikwad Aug 08 '19 at 16:27
5

You can include it with in the code like this,

onView(withId(R.id.titleInput))
     .perform(click(), replaceText("Engineer"), closeSoftKeyboard());
MatPag
  • 41,742
  • 14
  • 105
  • 114
Shainu Thomas
  • 360
  • 3
  • 12
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.

Jeehut
  • 20,202
  • 8
  • 59
  • 80
Mike Collins
  • 4,108
  • 1
  • 21
  • 28
1

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

sonique
  • 4,539
  • 2
  • 30
  • 39
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.

Maxwell
  • 6,532
  • 4
  • 37
  • 55
0

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

Hadi
  • 544
  • 1
  • 8
  • 28
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.

Izak
  • 909
  • 9
  • 24