13

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.

screenshot

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"
Abhimanyu
  • 11,351
  • 7
  • 51
  • 121

6 Answers6

23

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 青春版.

You can find these settings on Settings->Apps->Manage Apps ->Your-Application->Permissions

zmx0142857
  • 331
  • 1
  • 5
8

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.

Mansour
  • 445
  • 1
  • 4
  • 14
5

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,

  1. Enable autostart in app setting
  2. Grant the permission to "Display pop_up windows while running in the background" from app permission settings
Pratik Mhatre
  • 779
  • 7
  • 12
1

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.

V. Aixalà
  • 19
  • 1
  • 2
0

My problem was that I had an update awaiting me on my Android 11 Redmi Note 10s, after updating and doing the other suggested answers it worked!

MercifulSory
  • 337
  • 1
  • 14
0

Check whether you call getIntent().setAction() on the onCreate method and remove it if so.

That's how I "solved" it in my case.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
kandrit
  • 44
  • 4