JetPack组成UI测试按钮ONCLICK

发布于 2025-01-18 05:06:09 字数 1146 浏览 0 评论 0原文

我正在尝试测试 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-01-25 05:06:09

我解决了这个问题!

我的仿真器正在运行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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文