Zhelyazko Atanasov
02/08/2022, 6:11 PMLazyColumn
. We have many tests where we show a list of items, scroll to a particular index and perform actions on the item at that index - e.g. click it. But I can't find a way to do that using only an item index. There's a method performScrollToIndex(index) that does the scrolling, but after that it's not possible to get the semantics node at that index. If you do something like:
val index = 1000
onNodeWithTag(COUNTRY_LIST).performScrollToIndex(index)
onNodeWithTag(COUNTRY_LIST).onChildAt(index).performClick()
it doesn't work, because the LazyColumn (it has a Modifier.testTag(COUNTRY_LIST)
) only has "_<number_of_visible_items> + 2"_ number of children (nodes) at a given time.
Ideally I'd like to do something like onNodeWithTag(COUNTRY_LIST).performScrollToIndex(index).performClick()
, but performScrollToIndex
returns the semantics node on which the method was called 😞 If you have a test tag, text or anything else that can be used to match the item at index
, then you can perform actions/assertions on it, but do you know of a way to do that having only an index/position?mattinger
02/08/2022, 6:31 PMrule.onAllNodesWithTag("foobar")[0].assert(hasText("bizzbuzz"))
The key is that you need to assign the same test tag to every row in your LazyColumn.
Your other option is to assign a test tag with some sort of prefix along with a suffix that indicates the row, and then you can select each row directly.mattinger
02/08/2022, 6:35 PMLazyColumn {
itemsIndexed(listOf(1,2,3,4)) { index, item ->
Box(modifier =
Modifier.testTag("row:${index}")) {
}
}
}
rule.onNodeWithTag("row:0").assert(...)
Zhelyazko Atanasov
02/08/2022, 6:40 PMmattinger
02/08/2022, 7:13 PMZach Klippenstein (he/him) [MOD]
02/09/2022, 8:02 PMJelle Fresen [G]
03/17/2022, 5:48 PMhasText("United States")
), you could use onNodeWithTag(COUNTRY_LIST).performScrollToNode(<matcher>)
and continue with onNode(<matcher>)
.
And instead of including the row index in the test tag, you could also assign a custom key to the item (LazyColumn { items(data, key = { it.toKey() }) { .. } }
), use performScrollToKey(aKey)
to scroll to the key, and search for the key and... hmm... I don't think we have a matcher like hasKey(aKey)
. I'll file a bug so I can look into that.Zhelyazko Atanasov
03/21/2022, 10:18 AM