6

This answer demonstrates how to embed a link within an annotated string and make it clickable. This works great and triggers the on click with the correct URL. However, I can't seem to write a test that clicks the annotated text to open the link. Has anyone had success writing a test like this? My production code is very similar to what is in the answer. Below is my test code:

@Test
fun it_should_open_terms_of_service_link() {
    val termsOfServiceText = getString(R.string.settings_terms)
    try {
        Intents.init()
        stubAnyIntent()
        composeTestRule.onNode(hasText(termsOfServiceText, substring = true)).performClick()
        assertLinkWasOpened(getString(R.string.settings_terms_link))
    } finally {
        Intents.release()
    }
}

It looks like hasText(termsOfServiceText, substring = true) fetches the entire annotated string node opposed to just the substring, "Terms of Service". Thus, the on click method does get triggered, just not at the correct position in the annotated string. Happy to provide more info if needed. Thanks!

John A Qualls
  • 739
  • 1
  • 6
  • 17

2 Answers2

2

You can perform the click at an offset. This performs the click at the tail end horizontally, and in the middle vertically.

composeTestRule.onNode(hasText(termsOfServiceText, substring = true))
    .performTouchInput { click(percentOffset(.9f, .5f)) }

The disadvantage is that it makes the test more fragile.

Thomas Keller
  • 5,933
  • 6
  • 48
  • 80
Saša Tarbuk
  • 509
  • 7
  • 8
0

Have you tried adding composeTestRule.mainClock.advanceTimeBy(5000) after the click? This is sometimes needed to force a state change on automated tests in Compose.