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
And related issues,
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"
For more information about how the lifecycle of a process is tied to the states of the activities in it, see the Process Lifecycle section of that page. A user expects an activity’s UI state to remain the same throughout a configuration change, such as rotation or switching into multi-window mode.
Rather, the process of starting the second one overlaps with the process of stopping the first one. The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process (app) and one is starting the other. Here's the order of operations that occur when Activity A starts Activity B:
Note: onSaveInstanceState () is not called when the user explicitly closes the activity or in other cases when finish () is called. To save persistent data, such as user preferences or data for a database, you should take appropriate opportunities when your activity is in the foreground.
To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate () , onStart () , onResume () , onPause () , onStop (), and onDestroy (). The system invokes each of these callbacks as an activity enters a new state. Figure 1 presents a visual representation of this paradigm.
This is a problem of permissions. Just allow autostart and pop-up window for this app on your android phone settings.
Then start USB debugging and allow chain-start for the test app.
It worked for me on XiaoMi 11 青春版.
I had the same problem. The problem sometimes arises because of using
androidTestImplementation 'androidx.fragment:fragment-testing:1.3.6'
instead of
debugImplementation 'androidx.fragment:fragment-testing:1.3.6'
If you have fragments in your test scenarios add this dependency with debugImplementation
and check it again.
Thanks to this post for the answer.
I was getting following error on my Android 11 device
java.lang.AssertionError: Activity never becomes requested state "[STARTED, RESUMED, CREATED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")
I wasted a good amount of time on it and found the solution when I switched my device to Android 10.
If anybody facing such issue, please do following things to resolve it,
I was testing in a virtual Pixel 2 with API 30. No option for "autostart" or "pop-up" in Settings. In short, what this message says is that can't open the activity, so if you open manually your app, either in virtual device or real one, the test will run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With