如何在jetpack compose的自动化测试中执行键盘删除按钮

问题描述 投票:0回答:1

对于在 Jetpack compose 中制作的自定义编辑文本,我想测试键盘上的删除按钮,断言以下内容

  1. 如果左侧有字符,则单击键盘上的删除按钮将从当前光标位置删除左侧字符
  2. 如果没有字符,则可编辑文本不会发生任何变化

但是我找不到合适的

performXxx()
方法来执行此操作。

unit-testing android-jetpack-compose
1个回答
0
投票

经过几次探索,我发现了一个可以让我进行测试的技巧。添加下面的代码和注释,以防有人想尝试

CustomTextField(
    value = value,
    onValueChange = onValueChange,
)

// Performing the text selection at character 2
onNode(hasSetTextAction()).performTextInputSelection(TextRange(2, 3))

// Setting the selected character to empty which kind of replicates the functionality of the keyboard delete button.
// This doesn't press the keyboard delete button but good workaround to test the rest of the code
onNode(hasSetTextAction()).performTextInput("")

// Since the character at the 2nd index is deleted now we can assert that the text is "12"
onNode(hasSetTextAction(), useUnmergedTree = true).assertTextEquals("12")

保持这个问题开放,看看是否有更好的方法来实现这一目标

© www.soinside.com 2019 - 2024. All rights reserved.