如何在jetpack compose测试中关闭软键盘?

Ale*_*gli 5 android-jetpack-compose android-jetpack-compose-testing

我正在为jetpack 撰写屏幕编写测试,该屏幕上有一个文本字段。在我的测试中,我想在字段中输入一些文本,然后关闭软键盘,然后单击隐藏在软键盘下方的按钮。不过,我找不到在 jetpack 撰写测试中关闭软键盘的方法。我尝试了“performImeAction”,但这并没有关闭键盘,即使您在实际与此文本字段交互时按下软键盘上的 IME 键,它也会关闭键盘。

我希望能够做到这一点,但在组合测试中:

onView(withId(R.id.text_field)).perform(typeText("100"), closeSoftKeyboard())
Run Code Online (Sandbox Code Playgroud)

我当前的撰写代码在字段中输入“100”然后抛出错误:

composeTestRule
    .onNodeWithTag(TEXT_FIELD_TAG)
    .performTextInput("100")
composeTestRule
    .onNodeWithTag(TEXT_FIELD_TAG)
    .performImeAction() <------------- This fails
Run Code Online (Sandbox Code Playgroud)

报告错误:

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
Run Code Online (Sandbox Code Playgroud)

小智 1

要关闭设备的本机键盘,请尝试使用Espresso.closeSoftKeyboard()