https://kotlinlang.org logo
#compose-android
Title
# compose-android
c

Christophe Dongieux

10/24/2023, 8:11 AM
Hi! I'm struggling with UI tests... I'm trying to get all nodes with a certain content description, and I want to check all these nodes are displayed. I don't know in advance how many nodes there can be, so I can't assert on the count.
Copy code
composeTestRule
    .onAllNodesWithContentDescription("Foobar")
    .assertAreDisplayed() // <-- I want this kind of function
Any idea?
c

Chrimaeon

10/24/2023, 11:33 AM
1. strange test if you don’t know the expected number of nodes. 2. What is hindering you on using a foreach loop in the nodes?
c

Christophe Dongieux

10/24/2023, 11:42 AM
1. server-driven UI, anything is possible 🤷 2. what do you suggest? AFAIK, there are 2 options to get nodes a. semantics nodes by doing something like that
Copy code
composeTestRule
  .onAllNodesWithContentDescription("Foobar")
  .fetchSemanticsNodes()
But I get a
List<SemanticsNode>
, I don't know what to do with that to call
assertIsDisplayed()
b.
SemanticsNodeInteractionCollection
by doing this
Copy code
composeTestRule
  .onAllNodesWithContentDescription("Foobar")
But there is nothing interesting I can do with
SemanticsNodeInteractionCollection
if I don't expect any particular size.
c

Chrimaeon

10/24/2023, 11:44 AM
Ah, okay. But on the collection there is an
assertAll
that you can pass a matcher. Which in your case will be the
isDisplayed
c

Christophe Dongieux

10/24/2023, 11:46 AM
isDisplayed()
doesn't seem to exist
c

Chrimaeon

10/24/2023, 11:49 AM
Yeah, it was just a „placeholder“ for a matcher that should exist.
c

Christophe Dongieux

10/24/2023, 11:51 AM
That's the whole problem 😄
c

Chrimaeon

10/24/2023, 11:57 AM
There’s also a
fetchSemanticNodes
which return the list of nodes that you can definitely assert with `isDisplayed`()
c

Christophe Dongieux

10/24/2023, 11:58 AM
Oh, I just had an idea
Copy code
private fun SemanticsNodeInteractionCollection.assertAreDisplayed(): SemanticsNodeInteractionCollection {
        val semNodes = fetchSemanticsNodes()
        semNodes.forEachIndexed { index, _ ->
            val semanticsNodeInteraction = get(index)
            semanticsNodeInteraction.assertIsDisplayed()
        }
        return this
    }
It should work, I guess
c

Chrimaeon

10/24/2023, 11:59 AM
👍🏻
c

Christophe Dongieux

10/24/2023, 11:59 AM
Thanks for your help 👍
s

Severiano Jaramillo

10/24/2023, 6:57 PM
Looks like you solved your issue, but I am wondering about this:
server-driven UI, anything is possible 🤷
We use our own flavor of server-driven UI, but tests are still deterministic. The server responses are hardcoded and thus we know the number of elements that a particular test should have. Why is that not the case for you? Are you using a real server for your UI tests? Does your test involve testing for a random number of elements?
c

Christophe Dongieux

10/25/2023, 6:43 AM
What I meant by "anything is possible" is that the server can tell the frontend to display 3 times the same image in different places. In my tests I'm not able to determine this count, but when I build the UI I will give the same content description to each image, as they are the same.
👍 1
3 Views