kotlindesktopgui-testingcompose-multiplatform

Test a slider on Compose Multiplatform UI


I have to test a Compose Multiplatform desktop application. In the code I have a composable item:

Slider(modifier = Modifier.testTag("myslider"))

How can I change its value in my test? I tried an example with swipe:

@Test
fun testSwipeHorizontal() {
    launchContent()
    composeTestRule.onNodeWithTag("myslider")
        .performGesture {
            swipeLeft()
         }
}

which the compiler proposes to replace with performTouchInput(). What is the next I couldn't find.

Also I tried to use key press because Page Up / Page Down could change the slider's position.

composeTestRule.onNodeWithTag("myslider")
    .performKeyPress(KeyEvent.VK_PAGE_DOWN)

But VK_PAGE_DOWN has an Int type and not a KeyEvent one.

Is there some way to test a slider in this moment?


Solution

  • I found the simple solution. If I know the width of my slider and its maxvalue I can write:

    val width = 240
    val delta = width / maxvalue
    for (i in 0..< maxvalue){
        with(rule.onNodeWithTag("myslider")) {
            performMouseInput {
                click(Offset((i * delta + 1).toFloat(), 0f))
            }
        }
        rule.onNodeWithTag("mytext").assertTextEquals((i + 1).toString())
    }
    

    In such a way I can check if a mytext has been changed by my click on the slider.