Compose Android 测试惰性列未检测到所有子项

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

我在 Composable 中有一个 LazyColumn,我想检查 LazyColumn 中的每个项目是否存在。

这是我的可组合项: (注意:实际上在我的真实案例中,每个项目都有不同的组件,我只是为了简化问题而使其相似)

@Composable
fun HomeScreen(){
    LazyColumn(){
        item {
            HomeBanner()
            Text("Test1",Modifier.testTag("tag1"))
        }
        item {
            HomeBanner()
            Text("Test2",Modifier.testTag("tag2"))
        }
        item {
            HomeBanner()
            Text("Test3",Modifier.testTag("tag3"))
        }
        item {
            HomeBanner()
            Text("Test4",Modifier.testTag("tag4"))
        }
        item {
            HomeBanner()
            Text("Test5",Modifier.testTag("tag5"))
        }
        item {
            HomeBanner()
            Text("Test6",Modifier.testTag("tag6"))
        }

    }
}

这就是它的样子:

这是我的测试课:


@ExperimentalCoroutinesApi
@ExperimentalMaterialApi
@ExperimentalPagerApi
@HiltAndroidTest
@RunWith(AndroidJUnit4::class)
class HomeScreenTest {
    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @ExperimentalMaterialApi
    @get:Rule(order = 1)
    val composeTestRule = createAndroidComposeRule<MainActivity>()

    @Inject
    lateinit var mainRepository: MainRepository

    @Before
    fun setup() {
        hiltRule.inject()
    }

    @Test
    fun property_list_is_displayed() = runTest {
        composeTestRule.onRoot().printToLog("GALIH")
        composeTestRule.onNodeWithTag("tag1").assertExists()
        composeTestRule.onNodeWithTag("tag2").assertExists()
        composeTestRule.onNodeWithTag("tag3").assertExists()
        composeTestRule.onNodeWithTag("tag4").assertExists()
        composeTestRule.onNodeWithTag("tag5").assertExists()
    }
}

断言 Exist the

tag5
:

时会抛出错误
java.lang.AssertionError: Failed: assertExists.
Reason: Expected exactly '1' node but could not find any node that satisfies: (TestTag = 'tag5')

我看日志的时候,显示view hierarchy中没有tag5

printToLog:
                                                                                                    Printing with useUnmergedTree = 'false'
                                                                                                    Node #1 at (l=0.0, t=58.0, r=720.0, b=1510.0)px
                                                                                                     |-Node #6 at (l=0.0, t=58.0, r=720.0, b=1510.0)px
                                                                                                     | IsContainer = 'true'
                                                                                                     |  |-Node #45 at (l=0.0, t=163.0, r=720.0, b=1510.0)px
                                                                                                     |  | VerticalScrollAxisRange = 'ScrollAxisRange(value=0.0, maxValue=6.0, reverseScrolling=false)'
                                                                                                     |  | CollectionInfo = 'androidx.compose.ui.semantics.CollectionInfo@aa9f76d'
                                                                                                     |  | Actions = [IndexForKey, ScrollBy, ScrollToIndex]
                                                                                                     |  |  |-Node #48 at (l=45.0, t=208.0, r=675.0, b=583.0)px
                                                                                                     |  |  | ContentDescription = '[]'
                                                                                                     |  |  | Role = 'Image'
                                                                                                     |  |  |-Node #49 at (l=75.0, t=455.0, r=645.0, b=553.0)px
                                                                                                     |  |  | Text = '[Temukan Hunian Impian Anda Hanya Disini!]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #50 at (l=45.0, t=583.0, r=120.0, b=626.0)px, Tag: 'tag1'
                                                                                                     |  |  | Text = '[Test1]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #53 at (l=45.0, t=626.0, r=675.0, b=1001.0)px
                                                                                                     |  |  | ContentDescription = '[]'
                                                                                                     |  |  | Role = 'Image'
                                                                                                     |  |  |-Node #54 at (l=75.0, t=873.0, r=645.0, b=971.0)px
                                                                                                     |  |  | Text = '[Temukan Hunian Impian Anda Hanya Disini!]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #55 at (l=45.0, t=1001.0, r=124.0, b=1044.0)px, Tag: 'tag2'
                                                                                                     |  |  | Text = '[Test2]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #58 at (l=45.0, t=1044.0, r=675.0, b=1419.0)px
                                                                                                     |  |  | ContentDescription = '[]'
                                                                                                     |  |  | Role = 'Image'
                                                                                                     |  |  |-Node #59 at (l=75.0, t=1291.0, r=645.0, b=1389.0)px
                                                                                                     |  |  | Text = '[Temukan Hunian Impian Anda Hanya Disini!]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #60 at (l=45.0, t=1419.0, r=125.0, b=1462.0)px, Tag: 'tag3'
                                                                                                     |  |  | Text = '[Test3]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #63 at (l=45.0, t=1462.0, r=675.0, b=1837.0)px
                                                                                                     |  |  | ContentDescription = '[]'
                                                                                                     |  |  | Role = 'Image'
                                                                                                     |  |  |-Node #64 at (l=75.0, t=1709.0, r=645.0, b=1807.0)px
                                                                                                     |  |  | Text = '[Temukan Hunian Impian Anda Hanya Disini!]'
                                                                                                     |  |  | Actions = [GetTextLayoutResult]
                                                                                                     |  |  |-Node #65 at (l=45.0, t=1837.0, r=125.0, b=1880.0)px, Tag: 'tag4'
                                                                                                     |  |    Text = '[Test4]'
                                                                                                     |  |    Actions = [GetTextLayoutResult]
                                                                                                     |  |-Node #30 at (l=0.0, t=58.0, r=720.0, b=163.0)px
                                                                                                     |    IsContainer = 'true'
                                                                                                     |     |-Node #33 at (l=31.0, t=89.0, r=76.0, b=134.0)px
                                                                                                     |     | Role = 'Button'
                                                                                                     |     | Focused = 'false'
                                                                                                     |     | ContentDescription = '[]'
                                                                                                     |     | Actions = [OnClick, RequestFocus]
                                                                                                     |     | MergeDescendants = 'true'
                                                                                                     |     |-Node #36 at (l=136.0, t=73.0, r=712.0, b=148.0)px
                                                                                                     |       ContentDescription = '[]'
                                                                                                     |       Role = 'Image'
                                                                                                     |-Node #9 at (l=-720.0, t=58.0, r=-105.0, b=1510.0)px
                                                                                                       PaneTitle = 'Navigation menu'
                                                                                                       IsContainer = 'true'
                                                                                                        |-Node #13 at (l=-720.0, t=94.0, r=-105.0, b=180.0)px
                                                                                                        | Focused = 'false'
                                                                                                        | Text = '[Home]'
                                                                                                        | Actions = [OnClick, RequestFocus, GetTextLayoutResult]
                                                                                                        | MergeDescendants = 'true'
                                                                                                        |-Node #16 at (l=-720.0, t=180.0, r=-105.0, b=266.0)px
                                                                                                        | Focused = 'false'
                                                                                                        | Text = '[Properti]'
                                                                                                        | Actions = [OnClick, RequestFocus, GetTextLayoutResult]
                                                                                                        | MergeDescendants = 'true'
                                                                                                        |-Node #19 at (l=-720.0, t=266.0, r=-105.0, b=352.0)px
                                                                                                        | Focused = 'false'
                                                                                                        | Text = '[Project]'
                                                                                                        | Actions = [OnClick, RequestFocus, GetTextLayoutResult]
                                                                                                        | MergeDescendants = 'true'
                                                                                                        |-Node #22 at (l=-720.0, t=352.0, r=-105.0, b=438.0)px
                                                                                                        | Focused = 'false'
                                                                                                        | Text = '[Agent]'
                                                                                                        | Actions = [OnClick, RequestFocus, GetTextLayoutResult]
                                                                                                        | MergeDescendants = 'true'
                                                                                                        |-Node #25 at (l=-720.0, t=438.0, r=-105.0, b=524.0)px
                                                                                                          Focused = 'false'
                                                                                                          Text = '[Developer]'
                                                                                                          Actions = [OnClick, RequestFocus, GetTextLayoutResult]
                                                                                                          MergeDescendants = 'true'

我是如何在我的惰性专栏上测试其余孩子的?

android android-jetpack-compose android-testing lazycolumn android-jetpack-compose-testing
1个回答
0
投票

我有一个类似的问题,直到现在,我发现解决的唯一方法是使用

performTouchInput{ swipeUp() }
向上滚动,然后使组件可见以断言它。

在你的情况下,你可能需要为你的 lazycolumn 设置标签,并且在执行

swipeup()
之后你可以检查 lazycolumn 的最终子项,看起来像这样:

 @Test
    fun property_list_is_displayed() = runTest {
        composeTestRule.onRoot().printToLog("GALIH")
        composeTestRule.onNodeWithTag("tag1").assertExists()
        composeTestRule.onNodeWithTag("tag2").assertExists()
        composeTestRule.onNodeWithTag("tag3").assertExists()
        composeTestRule.onNodeWithTag("tag4").assertExists()
        composeTestRule.onNodeWithTag("lazycolumn")
            .performTouchInput{ swipeUp() }
            .onChildren()
            .onLast()
            .assertTextContains("Test5")
    }
© www.soinside.com 2019 - 2024. All rights reserved.