Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot enter Unicode characters in Espresso

Here's a simple command to match a text field and type in a variable name.

private final String name = "foo";
onView(withId(R.id.editTextName)).perform(typeText(name));

It works fine in my application with "foo". However:

private final String name = "á";
private final String name = "\u00E1";

In both of these cases, the onView line fails, blaming inability to find a view within the previous activity's view:

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: <myappid>:id/listViewAccounts

It seems that Espresso cannot handle Unicode characters and will fail if asked to enter them. This is running on an AVD with either the "English (US) Android Keyboard (AOSP)" or the "English (United States) Sample Soft Keyboard". The latter also flattens double letters, which is probably a separate problem with how fast Espresso enters them.

Has anyone run into this before? Is it Espresso, or the keyboard used, or something else?

I've changed the Android soft keyboard to the US International one which can long-press a to get á, but Espresso fails identically.

Update:

Using á does leave this in the stack trace (but \u00E1 doesn't):

java.lang.RuntimeException: Failed to get key events for string á (i.e. current IME does not understand how to translate the string into key events). As a workaround, you can use replaceText action to set the text directly in the EditText field.

Might be worth just doing that, although it's slightly decoupled from how the real user interaction would go.

like image 780
David Lord Avatar asked Mar 10 '16 00:03

David Lord


People also ask

How do I enter a Unicode character code?

Inserting Unicode characters To insert a Unicode character, type the character code, press ALT, and then press X. For example, to type a dollar symbol ($), type 0024, press ALT, and then press X. For more Unicode character codes, see Unicode character code charts by script.

How do you write espresso text?

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

How do I enable Unicode in Windows?

In Microsoft Windows Unicode characters can then be entered by holding down Alt , and typing + on the numeric keypad, followed by the hexadecimal code – using the numeric keypad for digits from 0 to 9 and letter keys for A to F – and then releasing Alt .


1 Answers

onView(withId(R.id.editTextName)).perform(replaceText(name));

Switching out typeText() for replaceText() allows other characters to be entered. I'm not sure this is an ideal solution, as there may be events on keypress which are not triggered, but if you just want to test how these strings are handled internally then it's probably fine.

Espresso does explicitly advise the workaround.

like image 59
David Lord Avatar answered Sep 19 '22 19:09

David Lord