Alex Egli
Alex Egli

Reputation: 2064

How to dismiss soft keyboard in jetpack compose test?

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

Upvotes: 5

Views: 3096

Answers (1)

Hayk Amirjanyan
Hayk Amirjanyan

Reputation: 21

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

Upvotes: 1

Related Questions