1

I'm writing tests for a jetpack compose screen that has a text field on it. In my test I want to enter some text into the field, then dismiss the soft keyboard, then click a button that was hidden beneath the soft keyboard. I cannot find a way to dismiss a soft keyboard in jetpack compose tests though. I tried "performImeAction" but that is not dismissing the keyboard, even though if you press the IME key on the soft keyboard when actually interacting with this text field it does dismiss the keyboard.

I want to be able to do this, but in a compose test:

onView(withId(R.id.text_field)).perform(typeText("100"), closeSoftKeyboard())

My current compose code, enters "100" in field then throws error:

composeTestRule
    .onNodeWithTag(TEXT_FIELD_TAG)
    .performTextInput("100")
composeTestRule
    .onNodeWithTag(TEXT_FIELD_TAG)
    .performImeAction() <------------- This fails

Error reported:

java.lang.AssertionError: Failed to perform IME action as current node does not specify any.
Semantics of the node:
Node #48 at (l=0.0, t=748.0, r=788.0, b=1141.0)px, Tag: 'TEXT_FIELD_TAG'
ImeAction = 'Default'
EditableText = '100'
TextSelectionRange = 'TextRange(3, 3)'
Focused = 'true'
Actions = [GetTextLayoutResult, SetText, SetSelection, OnClick, OnLongClick, PasteText]
MergeDescendants = 'true'
Has 7 siblings
Alex Egli
  • 1,884
  • 2
  • 24
  • 43
  • 1
    Did you check this [example](https://github.com/androidx/androidx/blob/androidx-main/compose/foundation/foundation/src/androidAndroidTest/kotlin/androidx/compose/foundation/text/KeyboardActionsTest.kt)? – Gabriele Mariotti Mar 17 '22 at 07:36
  • 1
    Thanks, that example showed me the trick was I had to specify an ime action in my keyboardOptions on the TextField when creating the composable. It's a poor dev experience if "performImeAction()" doesn't just perform the default, but you get what you get with compose. – Alex Egli Mar 17 '22 at 13:54

1 Answers1

0

To dismiss the Device's native keyboard try to use Espresso.closeSoftKeyboard()

avariant
  • 2,234
  • 5
  • 25
  • 33