This is not a duplicate question.
I have already looked at similar questions. I have added the questions I have referred to at the last.
My problem is that the test executes, but I have to open the app manually for every test.
You can see that I had been waiting for 18 seconds before opening the app. It waits till I open the app manually or it shows this error after a timeout.
Activity never becomes requested state "[DESTROYED, RESUMED, STARTED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")
From this post, I assume the issue is with the device as I am using Redmi Note 5
.
I have disabled animations
as well as the MIUI optimization
option.
Any help is appreciated to run the tests on Xiaomi devices automatically.
No point in running automated UI tests manually.
Update 1
It executes only for launcher / main activity.
It is not working for other activities.
Update 2
I also happen to have a Redmi 4. The tests work on that device as expected without any code changes.
Referred SO questions
- AndroidX.Test ActivityScenario: java.lang.AssertionError: Activity never becomes requested state "[RESUMED]" (last lifecycle transition = "STOPPED")
- Android device doesn't launch activities on the screen while espresso testing
- Activity never becomes requested state in UI Testing
- java.lang.AssertionError: Activity never becomes requested state
- Android instrumented test throws exception: Activity never becomes requested state "[RESUMED, DESTROYED, STARTED, CREATED]"
- How do I fix this error java.lang.AssertionError: Activity never becomes requested state "[DESTROYED]" (last lifecycle transition = "RESUMED")
And related issues,
- https://github.com/android/android-test/issues/143
- https://github.com/android/android-test/issues/496
Adding code snippets and dependencies for anyone who wants to have a look,
Test code
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTest {
@Test
fun test_isActivityInView() {
val activityScenario = ActivityScenario.launch(MainActivity::class.java)
onView(withId(R.id.layout_activity_main)).check(matches(isDisplayed()))
}
}
Dependencies
testImplementation "junit:junit:4.13.2"
androidTestImplementation "androidx.test:core:1.4.0"
androidTestImplementation "androidx.test:runner:1.4.0"
androidTestImplementation "androidx.test:rules:1.4.0"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"