Search code examples
androidkotlinautomated-testsandroid-jetpack-compose

Compose TextField UI testing Failed to assert the following: Text + EditableText contains


@Test
fun test() {
    composeRule.mainClock.autoAdvance = false
    composeRule.mainClock.advanceTimeBy(400L)
    composeRule.onNodeWithTag("testing").performTextInput("123")
    composeRule.onNodeWithTag("testing").assert(hasText("123"))
}
Error: 
java.lang.AssertionError: Failed to assert the following: (Text + EditableText contains '123' (ignoreCase: false))
Semantics of the node:
Node #4 at (l=0.0, t=91.0, r=1080.0, b=259.0)px, Tag: 'testing'
Focused = 'false'
ImeAction = 'Done'
EditableText = ''
TextSelectionRange = 'TextRange(0, 0)'
Text = '[Phone No]'
Actions = [RequestFocus, GetTextLayoutResult, SetText, SetSelection, OnClick, OnLongClick, PasteText]
MergeDescendants = 'true'
Selector used: (TestTag = 'testing')

I added 400L because I was using AnimatedVisibility in my composable so I wanted to wait until views are drawn. Tried different ways to assert textfield content


Solution

  • The problem was with

    composeRule.mainClock.autoAdvance = false
    composeRule.mainClock.advanceTimeBy(400L)
    

    Make Sure to call after advanceTimeBy()

    composeRule.mainClock.autoAdvance = true