仪器测试无法在 Android 中找到标记有用于测试的字符串的节点

问题描述 投票:0回答:1

我正在尝试使用

.testTag
测试可组合项,该可组合项位于给定可组合项调用结构的树的叶级别。代码的风格是:

顶级可组合

@Composable
fun TopLevelComposable() {
    AComposable()
}

子可组合

@Composable
fun AComposable() {
    LeafComposable()
}

叶可组合

@Composable
fun LeafComposable() {
    Text(text = "stringOfText", modifier = Modifier.testTag("Text of AComposable"))
}

MainActivity.kt

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    TopLevelComposable()
            }
        }
    }
}

但是,我收到错误:

java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (TestTag = 'Text of AComposable')
However, the unmerged tree contains '1' node that matches. Are you missing `useUnmergedNode = true` in your finder?

问题

如何使用可组合项让 androidTest 接受嵌套

.testTag

android android-testing
1个回答
0
投票

我没有看到你在示例中调用

.assertExists()
,但错误消息有一个有用的建议。在您的仪器测试中,添加
useUnmergedTree = true
,如下所示:

    composeTestRule.onNodeWithTag("Text of AComposable", useUnmergedTree = true)
        .assertExists()
        .assertIsDisplayed()
© www.soinside.com 2019 - 2024. All rights reserved.