JetPack组成UI测试按钮ONCLICK
我正在尝试测试 ButtonClick 如何更改用户界面。
测试设置如下:
composeRule.setContent {
var i by remember { mutableStateOf(0) }
Button(modifier = Modifier.testTag("TEST"), onClick = { i++ }) {
Text(text = i.toString())
}
}
我的实际测试如下所示:
val button = composeRule.onNodeWithTag("TEST")
button.assertTextEquals("0")
button.performClick()
button.printToLog("Test")
button.assertTextEquals("1")
第一个断言通过,但是在检查文本应等于 1
时失败:
java.lang.AssertionError: Failed to assert the following: (Text + EditableText = [1])
Semantics of the node:
Node #2 at (l=0.0, t=66.0, r=176.0, b=165.0)px, Tag: 'TEST'
Role = 'Button'
Text = '[0]'
Actions = [OnClick, GetTextLayoutResult]
MergeDescendants = 'true'
Selector used: (TestTag = 'TEST')
单击后的 printToLog() 如下所示:
Printing with useUnmergedTree = 'false'
Node #2 at (l=0.0, t=66.0, r=176.0, b=165.0)px, Tag: 'TEST'
Role = 'Button'
Text = '[0]'
Actions = [OnClick, GetTextLayoutResult]
MergeDescendants = 'true'
因此,看起来,当执行单击时,内容不会重新组合,或者由于某种原因,单击实际上并未发生。
有谁知道这里会发生什么?
I'm trying to test how a buttonClick would change the UI.
The test is setup like this:
composeRule.setContent {
var i by remember { mutableStateOf(0) }
Button(modifier = Modifier.testTag("TEST"), onClick = { i++ }) {
Text(text = i.toString())
}
}
And my actual test looks like this:
val button = composeRule.onNodeWithTag("TEST")
button.assertTextEquals("0")
button.performClick()
button.printToLog("Test")
button.assertTextEquals("1")
The first assertion passes, however it fails when checking that the text should equal 1
:
java.lang.AssertionError: Failed to assert the following: (Text + EditableText = [1])
Semantics of the node:
Node #2 at (l=0.0, t=66.0, r=176.0, b=165.0)px, Tag: 'TEST'
Role = 'Button'
Text = '[0]'
Actions = [OnClick, GetTextLayoutResult]
MergeDescendants = 'true'
Selector used: (TestTag = 'TEST')
The printToLog() after the click looks like this:
Printing with useUnmergedTree = 'false'
Node #2 at (l=0.0, t=66.0, r=176.0, b=165.0)px, Tag: 'TEST'
Role = 'Button'
Text = '[0]'
Actions = [OnClick, GetTextLayoutResult]
MergeDescendants = 'true'
So it appears that when the click is performed, either the content is not recomposed, or for some reason, the click isn't actually happening.
Does anyone know what might be going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我解决了这个问题!
我的仿真器正在运行API级别32 ..看来组成的交互库还不能完全使用32。
降低API级别后,代码无需任何更改而无法进行。
I resolved this issue!
My emulator was running API level 32.. Looks like the Compose Interaction Library does not fully work with 32 yet.
Code worked without any changes after downgrading API level.