Tonnie
Tonnie

Reputation: 8102

Printing Compose Semantics Tree to the Log

I am following Testing in Jetpack Compose Codelab but I am unable to print the Semantics tree using the printToLog function on a node. I am using this code inside the androidTest package.

import androidx.compose.ui.test.printToLog 

...

    //testing function
    @Test
    fun rallyTopAppBarTest() {
    
        val allScreen = RallyScreen.values().toList()
        testRule.setContent {
            //the component be tested in isolation goes here
            RallyTopAppBar(
                allScreens = allScreen,
                onTabSelected = {},
                currentScreen = RallyScreen.Accounts
            )
        }
        
        testRule.onRoot().printToLog("currentLabelExists")
        
            ....
    }

I have checked my LogCat with both Debug and Verbose filtering using "currentLabelExists" tag but the Semantics Tree is not printing on the log.

Any ideas on what I might be missing out.

Upvotes: 7

Views: 8155

Answers (3)

Rubén Viguera
Rubén Viguera

Reputation: 3584

I know you are asking to log a test inside androidTest, but for anyone looking for a solution for test, the following might help.

If you try to print the compose semantics tree in a local test (a regular unit test that does not use an emulator), the method printToLog won't work as it logs into Logcat, which is Android-specific.

So the solution in my case was to create a new method like the following:

    fun SemanticsNodeInteraction.printToLog(
        maxDepth: Int = Int.MAX_VALUE,
    ) {
        val result = "printToLog:\n" + printToString(maxDepth)
        println(result)
    }

Upvotes: 3

Robert Schröder
Robert Schröder

Reputation: 188

Since I just ran into the same problem: The SemanticsTree seems to appear only when you run your tests in Debug Mode

Upvotes: 3

Abhimanyu
Abhimanyu

Reputation: 14757

I can see the semantics tree.
Check if Android Studio Logcat has "No Filters"

Filter options

Edit
It seems to be a bug as I can sometimes see the logs even if I select "Show only selected application".

Upvotes: 9

Related Questions