0

I have a ui test and I need to test text for toast. I tried to implement validation with expresso but it didn't work. I also tried all the methods suggested here, none of them worked. In code i use standard Toast, compileSdk 33, targetSdk 33, i run the test on the emulator with 12 android.

how i tried

first way

@Test
    fun testToast() {

        onView(withId(R.id.button1)).perform(click())

        onView(withText("Test text")).inRoot(withDecorView(not(`is`(activity.window?.decorView))))
            .check(
                matches(
                    isDisplayed()
                )
            )

    }

second way (from the article)

@Test
    fun testToast() {

        onView(withId(R.id.button1)).perform(click())

        onView(withText("Test toast")).inRoot(ToastMatcher()).check(matches(isDisplayed()))
    }`

`class ToastMatcher : TypeSafeMatcher<Root?>() {

    override fun describeTo(description: Description?) {
        description?.appendText("is toast")
    }

    override fun matchesSafely(item: Root?): Boolean {
        val type: Int? = item?.windowLayoutParams?.get()?.type
        if (type == WindowManager.LayoutParams.TYPE_TOAST) {
            val windowToken: IBinder = item.decorView.windowToken
            val appToken: IBinder = item.decorView.applicationWindowToken
            if (windowToken === appToken) { // means this window isn't contained by any other windows.
                return true
            }
        }
        return false
    }
}
Slavikwin
  • 1
  • 1

0 Answers0