Skip to content

Check if a button is displayed on the screen

Devrath edited this page Mar 4, 2024 · 2 revisions

Code

@Composable
fun DisplayButtonDemo() {
    Button(
        modifier = Modifier.semantics { contentDescription = "Hello World" },
        onClick = {}) {
        Text(text = "Hello World!")
    }
}

TestClass

class DisplayButtonDemoKtTest {

    // This rule is used to test if there os just a composable we are testing without activity
    @get:Rule
    val composeTestRule = createComposeRule()


    @Test
    fun myTest() {

        // <------------------------ ASSIGN ------------------------>
        val textToBeMatched = "Hello World"

        // Set the content
        composeTestRule.setContent {
            DisplayButtonDemo()
        }

        // <------------------------ ACT -------------------------->
        // Find the composable on the UI tree
        composeTestRule.onNodeWithText(textToBeMatched).isDisplayed()

    }



}