简体   繁体   中英

No views in hierarchy found matching : JetPack Compose

I am trying to run a test using Espresso in jetpack compose. Everytime I run it, it gives this error.

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: an instance of android.widget.TextView and view.getText() with or without transformation to match: is "Count"

My MainActivity.kt

package com.example.testing

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.example.testing.ui.theme.TestingTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TestingTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Greeting()
                }
            }
        }
    }
}

@Composable
fun Greeting() {
    var counter by remember{
        mutableStateOf(0)
    }
    Column(
        modifier = Modifier
            .fillMaxSize()
            .wrapContentSize(Alignment.Center)
    ) {
        Text(text = stringResource(id = R.string.Count),
            modifier = Modifier
                .padding(8.dp)
                .testTag(stringResource(id =  R.string.)))

        Button(onClick = { counter++ }) {
            Text(text = stringResource(id = R.string.Increment))
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    TestingTheme {
        Greeting()
    }
}

My test.kt

package com.example.testing


import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.microsoft.appcenter.espresso.ReportHelper;
import androidx.lifecycle.Lifecycle
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.microsoft.appcenter.espresso.Factory
import org.junit.After

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule

/**
 * Instrumented test, which will execute on an Android device.
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
@RunWith(AndroidJUnit4::class)
@LargeTest
class ExampleInstrumentedTest {

    @Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)  //espresso

    @Test
    fun testClick() {
        onView(withText("Count")).check(matches(isDisplayed()))
    } 
}

I have Count in string.xml as Count. I have emulator up and running fine. It seems like it is not able to detect the Activity.

In @Test, if I skip

.check(matches(isDisplayed()))

part from

onView(withText("Count")).check(matches(isDisplayed()))

it passes.

Also I have tried junit4

val composeTestRule = createAndroidComposeRule<MainActivity>()

And it works but espresso isn't working

You can use:

@get:Rule
val composeRule = createComposeRule()

@Test
fun testClick() {
    composeRule.setContent {
        TestingTheme {
            Greeting()
        }
    }
    composeRule.onNodeWithText("Count").assertIsDisplayed()
}

As alternative you can also use:

@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
@get:Rule
val composeTestRule = createAndroidComposeRule<MainActivity>()

@OptIn(ExperimentalFoundationApi::class, ExperimentalComposeUiApi::class)
@Test
fun testClick() {
    composeTestRule.onNodeWithText("Count").assertIsDisplayed()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM