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

发布于 2025-01-15 03:35:55 字数 1015 浏览 3 评论 0原文

我正在为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2025-01-22 03:35:55

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文