Hi! I'm struggling with UI tests... I'm trying to ...
# compose-android
c
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
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
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
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
isDisplayed()
doesn't seem to exist
c
Yeah, it was just a „placeholder“ for a matcher that should exist.
c
That's the whole problem 😄
c
There’s also a
fetchSemanticNodes
which return the list of nodes that you can definitely assert with `isDisplayed`()
c
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
👍🏻
c
Thanks for your help 👍
s
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
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
393 Views