对于在 Jetpack compose 中制作的自定义编辑文本,我想测试键盘上的删除按钮,断言以下内容
但是我找不到合适的
performXxx()
方法来执行此操作。
经过几次探索,我发现了一个可以让我进行测试的技巧。添加下面的代码和注释,以防有人想尝试
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")
保持这个问题开放,看看是否有更好的方法来实现这一目标