繁体   English   中英

是否可以在jetpack compose测试中断言节点可滚动

[英]Is it possible to assert that a node is scrollable in jetpack compose testing

在执行测试时是否可以断言喷气背包组合节点是可滚动的?

class MyTest {
    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun givenEnoughItems_thenAssertListIsScrollable() {
        composeTestRule.setContent {
            BasicTheme {
                ScrollableComponent(items = List(1000) { "$it" })
            }
        }

        composeTestRule.onRoot().fetchSemanticsNode().assertIsScrollable()
    }
}

fun SemanticsNodeInteraction.assertIsScrollable() : SemanticsNodeInteraction {
    // What would go here? 
}

恐怕无法验证可滚动行为,我认为这可以通过 UI/Instrumentation Test 来完成。

在单元测试中,我认为您只能验证它是否是 ScrollableComponent 实例

使用hasScrollAction()

例子

import androidx.compose.ui.test.hasScrollAction
import androidx.compose.ui.test.assert

private fun assertNodeIsScrollable() {
    findNode().assert(hasScrollAction())
}

findNode()将类似于

private fun findNode(): SemanticsNodeInteraction {
    return composeTestRule.onNodeWithTag(
        testTag = "test_tag",
    )
}

暂无
暂无

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

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