Android 如何通过UI测试在Jetpack compose文本字段中输入文本?
在Jetpack compose中,我有一个文本字段,我正在尝试编写浓缩咖啡UI测试。我找不到如何在文本字段中输入文本,有什么想法吗Android 如何通过UI测试在Jetpack compose文本字段中输入文本?,android,android-espresso,android-jetpack,ui-testing,android-jetpack-compose,Android,Android Espresso,Android Jetpack,Ui Testing,Android Jetpack Compose,在Jetpack compose中,我有一个文本字段,我正在尝试编写浓缩咖啡UI测试。我找不到如何在文本字段中输入文本,有什么想法吗 TextField( value = textState.value, modifier = Modifier.fillMaxWidth(), onValueChange = { textState.value = it
TextField(
value = textState.value,
modifier = Modifier.fillMaxWidth(),
onValueChange = {
textState.value = it
apiServiceCall(textState.value.text)
},
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences)
)
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()
@Test
fun enterTextAndMakeServiceCall() {
ActivityScenario.launch(MainActivity::class.java)
//TODO: Enter text inside the TextField
composeTestRule.onNode(hasText(getString(R.string.result)))
}
TextField(
value=textState.value,
modifier=modifier.fillMaxWidth(),
onValueChange={
textState.value=it
apiServiceCall(textState.value.text)
},
键盘选项=键盘选项(大写=键盘大写。句子)
)
@获取:规则
val composeTestRule=createandroidcomposeule()
@试验
有趣的enterTextAndMakeServiceCall(){
ActivityScenario.launch(MainActivity::class.java)
//TODO:在文本字段内输入文本
onNode(hasText(getString(R.string.result)))
}
我首先在要测试的组件上设置testTag
修饰符:
const val MY_TEXTFIELD_TAG = "myTextFieldTag"
TextField(
value = textState.value,
modifier = Modifier.fillMaxWidth().testTag(MY_TEXTFIELD_TAG),
onValueChange = {
textState.value = it
},
keyboardOptions = KeyboardOptions(capitalization = KeyboardCapitalization.Sentences),
)
然后通过测试,您可以设置并检查如下值:
@Test
fun setAndCheckTheTextFieldValue() {
ActivityScenario.launch(MainActivity::class.java)
val resultText = "result"
// Sets the TextField value
composeTestRule.onNodeWithTag(MY_TEXTFIELD_TAG).performTextInput(resultText)
// Asserts the TextField has the corresponding value
composeTestRule.onNodeWithTag(MY_TEXTFIELD_TAG).assert(hasText(resultText))
}