简体   繁体   English

Jetpack Compose UI 测试按钮 onClick

[英]Jetpack Compose UI Testing Button onClick

I'm trying to test how a buttonClick would change the UI.我正在尝试测试 buttonClick 如何更改 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 :第一个断言通过,但是在检查文本是否应等于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:点击后的 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'

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?有谁知道这里可能会发生什么?

I resolved this issue!我解决了这个问题!

My emulator was running API level 32.. Looks like the Compose Interaction Library does not fully work with 32 yet.我的模拟器正在运行 API 级别 32.. 看起来 Compose 交互库还不能完全与 32 一起使用。

Code worked without any changes after downgrading API level.降级 API 级别后,代码无需任何更改即可工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM