【问题标题】:Jetpack Compose UI Testing Button onClickJetpack Compose UI 测试按钮 onClick
【发布时间】:2022-11-10 07:45:51
【问题描述】:

我正在尝试测试 buttonClick 如何更改 UI。

测试设置如下:

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'

因此,似乎在执行点击时,内容没有重新组合,或者由于某种原因,点击实际上并没有发生。

有谁知道这里可能发生了什么?

【问题讨论】:

    标签: android kotlin android-jetpack-compose ui-testing


    【解决方案1】:

    我解决了这个问题!

    我的模拟器运行的是 API 级别 32.. 看起来 Compose 交互库还不能完全与 32 一起使用。

    降级 API 级别后,代码无需任何更改即可工作。

    【讨论】:

      【解决方案2】:

      您在模拟器中使用了哪个 API 级别。面临同样的问题点击操作不会发生

      【讨论】:

        猜你喜欢
        • 2021-12-05
        • 2022-10-02
        • 2022-08-09
        • 1970-01-01
        • 2022-09-29
        • 1970-01-01
        • 2022-10-05
        • 2020-07-30
        • 1970-01-01
        相关资源
        最近更新 更多