12

I am new to Espresso UI testing.

I am getting this error while running tests (ADT Eclipse IDE ).

The app is already developed and there are lots of request going on while launching the app. it is not possible to rewrite the app. but i need to find the way to test this UI even if there is any delay in the loading of the components.

        java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=com.xx.android/com.yy.core.android.map.MapActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1390913271702 and and now the last time the queue went idle was: 1390913271767. If these numbers are the same your activity might be hogging the event queue.
        at com.google.android.apps.common.testing.testrunner.GoogleInstrumentation.startActivitySync(GoogleInstrumentation.java:277)
        at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:119)
        at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:97)
        at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:104)
        at com.gulesider.android.test.UItest.setUp(UItest.java:25)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
        at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
        at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
        at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1799)
  1. I have one library project called “Core” - it will not generate any .apk
  2. Also i have one Android project called “AA” which will access “Core”. - This is AA.apk
  3. Now i have created a test project called “UItest”

Manifest:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.AA.android.test"
        android:versionCode="1"
        android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" 
            android:targetSdkVersion="18" />
    <instrumentation
      android:name="com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
      android:targetPackage="com.AA.android"/>
    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
    <activity
                android:name="com.core.android.map.MapActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <uses-library android:name="android.test.runner" />
        </application>
    </manifest>

My test:

    public class UItest extends ActivityInstrumentationTestCase2<MapActivity> {
        public UItest() {
            super(MapActivity.class);
        }

        @Override
        public void setUp() throws Exception {
            super.setUp();

            getActivity();

        }

        public void testSearchBox() {

            Espresso.onView(ViewMatchers.withId(R.id.menu_button_logo)).perform(ViewActions.click());

        }

    }
Bolhoso
  • 895
  • 1
  • 7
  • 14
user3241003
  • 121
  • 1
  • 1
  • 4

12 Answers12

12

For Espresso Testing it is highly recommend that you turn off system animations on the virtual or physical device(s) used for testing. So you can follow the steps below to manually turn off the animations:

Under: Settings->
Developer options-> Drawing

  1. Window Animations scale to OFF
  2. Transition animation scale to OFF
  3. Animator duration scale to OFF
Testing Singh
  • 1,347
  • 1
  • 15
  • 26
5

If there is a progress bar running when you create the activity, you get an error like this. You should cause a stop for the progress bar in order to continue running the test.

Murat
  • 3,084
  • 37
  • 55
  • Yes, this is correct - visible `ProgressBar` maybe the reason of this exception. Check that your activity/fragment has no visible `ProgressBar`s (and be careful when using alpha with ProgressBar) – xCh3Dx Apr 19 '16 at 13:04
4

I have stuck into this problem for several hours. Finally, I got the reason.

This works for me.

Here are some different reasons, according to the phenomenon.

  • Activity can't be launched
  • Activity launched, but UI perform actions not work

The first scenario: activity can't be launched

Because of your target Activity maybe already in the activity stack. Add a CLEAR flag and NEW_TASK flag

    @get:Rule
    val activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)

    private lateinit var launchedActivity: MainActivity

    @Before
    fun setUp() {
        val intent = Intent(Intent.ACTION_PICK)
        //this is the key part
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
        //this is the key part
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        launchedActivity = activityRule.launchActivity(intent)
    }

The second scenario: activity launched but UI perform actions not work

In this scenario may be because of

  1. Your code is executing some animation in a long time
  2. Your code is doing a UI logic that you don't notice
    • (like Handler post event or runnable)
    • (register a listener in ViewTreeObserver like OnPreDrawListener and didn't unregister in a proper timing)

These actions may lead to UI thread busy, so the espresso can not do the test.

Jeffery Ma
  • 3,051
  • 1
  • 23
  • 26
  • 1
    Any tips on how to debug the second scenario, beyond breakpoints and println()? ;) i.e. blocking UI logic.... – xavierdominguez Nov 06 '20 at 12:42
  • Actually, it was because the activity could not be launched and your first trick worked, i.e. adding the CLEAR_TASK and NEW TASK flags. Thanks so much! Been struggling for months with this. Although, I was getting the error below, which led me to think it was more along the lines of your second explanation. Posting below should it help others. – xavierdominguez Nov 06 '20 at 13:08
  • `java.lang.RuntimeException(Could not launch intent { flg=0x10000000 cmp=com.fake.package.id.MainActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. ... The last time the event queue was idle before your activity launch request was 1604665354699 and now the last time the queue went idle was: 1604665354766. If these numbers are the same your activity might be hogging the event queue.)` – xavierdominguez Nov 06 '20 at 13:08
3

I experienced this error while running Espresso tests on 6.0 devices but not on 5.1.1 or 7.0 devices. I tracked the cause down to using android:fadeScrollbars within a style. Removing this item from my style resolved the issue.

Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
dev5678
  • 31
  • 1
2

Probably you have animation inside your activity, which blocks espresso execution. You have to disable it - see https://github.com/googlesamples/android-testing/tree/master/ui/espresso/BasicSample

denys
  • 6,834
  • 3
  • 37
  • 36
2

If you are performing this test in MI or XIOMI phone then maybe it will not work so you can change device or you can use bluestack emulator. It will be work

1

In my case a custom view caused this behaviour. It contained a Scroller which was constantly scrolling. Unfortunately, I didn't find a solution for this issue until now except disabling it for the tests...

luckyhandler
  • 10,651
  • 3
  • 47
  • 64
0

At the very first page you will be calling too many request which will be taking time more than 15 seconds, the first page should be very lightwieght. Just try by creating one new welcome page and then calling your original welcome page. Hope this work for you.

ajitksharma
  • 4,523
  • 2
  • 21
  • 40
0

Well, in my case it was caused by a strange thing.

One of my UI tests opened the external intent "android.app.action.CONFIRM_DEVICE_CREDENTIAL" so I decided to stub it. From now on, the MAIN intent didnt launch again until I manually closed the screen oipened by "android.app.action.CONFIRM_DEVICE_CREDENTIAL" from the recent tasks.

No idea why this happened, and have no time now for research. Maybe later I will update this thread.

Ignacio Tomas Crespo
  • 3,401
  • 1
  • 20
  • 13
0

I faced this error when I trying to test the opening of another activity when the user clicked on a given view. What I was doing wrong was not replacing:

@Rule
public ActivityTestRule<MyActivity> myActivityActivityTestRule = new ActivityTestRule<>(MyActivity.class);

Per:

@Rule
public IntentsTestRule<MyActivity> myActivityActivityTestRule =
            new IntentsTestRule<>(MyActivity.class);
Ricardo
  • 1
  • 1
0

I had this problem too, and in the moment I changed physical device to other Android phone the tests were working. Just try to use other device. And use @rule for launching activity

Pesa
  • 241
  • 1
  • 3
  • 8
0

In my case an authorization request returned 401 error. Thats's why a login activity continued to show, while MainActivity didn't start. It cannot be repaired by tests.

CoolMind
  • 26,736
  • 15
  • 188
  • 224