如何在jetpack compose测试中关闭软键盘?
我正在为jetpack 撰写屏幕编写测试,该屏幕上有一个文本字段。在我的测试中,我想在字段中输入一些文本,然后关闭软键盘,然后单击隐藏在软键盘下方的按钮。不过,我找不到在 jetpack 撰写测试中关闭软键盘的方法。我尝试了“performImeAction”,但这并没有关闭键盘,即使您在实际与此文本字段交互时按下软键盘上的 IME 键,它也会关闭键盘。
我希望能够做到这一点,但在撰写测试中:
onView(withId(R.id.text_field)).perform(typeText("100"), closeSoftKeyboard())
我当前的撰写代码,在字段中输入“100”,然后抛出错误:
composeTestRule
.onNodeWithTag(TEXT_FIELD_TAG)
.performTextInput("100")
composeTestRule
.onNodeWithTag(TEXT_FIELD_TAG)
.performImeAction() <------------- This fails
报告错误:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要关闭设备的本机键盘,请尝试使用
Espresso.closeSoftKeyboard()
To dismiss the Device's native keyboard try to use
Espresso.closeSoftKeyboard()