'android.support.test.espresso.NoActivityResumedException: No activities in stage RESUMED

I'm trying to write test cases for my activities. I have several activities and there is no issue for one of them while I'm getting following error when I try to run tests over other ActivityTest classes.

android.support.test.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?

This is my class that all my test cases fails:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class LocatingActivityTest
{
    @Rule
    public ActivityTestRule<LocatingActivity> mActivityTestRule = new ActivityTestRule<>(LocatingActivity.class);

    private LocatingActivity mLocatingActivity;

    @Before
    public void setup()
    {
        mLocatingActivity = mActivityTestRule.getActivity();
    }

    @Test
    public void viewsMustBeVisible()
    {
        onView(withId(R.id.locating_text)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.sonarView)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.locating_cancel_booking)).check(matches(isCompletelyDisplayed()));

        onView(withId(R.id.locating_list_view)).check(matches(isDisplayed()));
    }

    @Test
    public void viewsMustBeEnabled()
    {
        onView(withId(R.id.tvNoDriverFound)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.tvNextSearch)).check(matches(not(isCompletelyDisplayed())));
    }
}

However this is my another class that all of its test cases passes:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class BookingActivityTest
{
    @Rule
    public IntentsTestRule<BookingTaxiActivity> mActivityTestRule = new IntentsTestRule<>(BookingTaxiActivity.class);

    private BookingTaxiActivity mBookingTaxiActivity;

    @Before
    public void setup()
    {
        mBookingTaxiActivity = mActivityTestRule.getActivity();
    }

    @Test
    public void viewsMustBeVisible()
    {
        onView(withId(R.id.booking_pick_up_layout)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.booking_drop_off_layout)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.fab_booking)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.booking_estimated_fare)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.ibMenu)).check(matches(isCompletelyDisplayed()));
        onView(withId(R.id.booking_toolbar)).check(matches(isCompletelyDisplayed()));

        onView(withId(R.id.booking_taxi_type_picker)).check(matches(isDisplayed()));
    }

    @Test
    public void viewsMustBeEnabled()
    {
        // These Views are off the screen
        onView(withId(R.id.tag_widget)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.payment_btn)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.current_pickup_view)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.advance_pickup_view)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.booking_notes_btn)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.promo_code_btn)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.taxi_warning)).check(matches(not(isCompletelyDisplayed())));
        onView(withId(R.id.booking_book_now)).check(matches(not(isCompletelyDisplayed())));
    }
}

I have no idea why tests of above class passes while other classes fails.



Solution 1:[1]

If the run device is in lock mode, and/or activity inactive, will trigger this error. Make sure your device is on and app/test is able to run in foreground! The easiest fix ever(at least for me)!

Solution 2:[2]

OK, I just found a painful fact that Espresso is not able to run an Activity from somewhere in happy path.

Lets say my happy path contains Activities A, B and C. I was thinking I'm able to run tests of Activity B (or C) without calling Activity A. So this is impossible and leads to above error. What you should do is click on a button you have on Activity A, The Activity B displays so you are able to perform your tests then click on a button (or a logic that goes to next activity) that calls Activity C and perform your tests.

This is super painful :( Particularly the fact that I spent a week to achieve it. Documentation is not subject to tell it clearly?!!!

Solution 3:[3]

Running Espresso Tests in parallel might be an issue. If the tests fail when running multiple but don't fail when running them individually, then one possible cause is the tests are executed in parallel.

Try with adding --no-parallel at the end of your command. Use --no-parallel.

Example --> gradlew connectedLiveDebugAndroidTest --no-parallel

Two different Espresso tests running on the same device at the same time make them flaky and prone to fail.

Solution 4:[4]

If this issue happens when doing Espresso testing on a device, then you may add the following code in the onCreate of the activity which you are testing. This will keep the screen on while performing the testing

    if(BuildConfig.DEBUG){
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

Solution 5:[5]

Check the dependencies in the build.gradle file (Module: App)

androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'

Solution 6:[6]

I get these errors on my Jenkins server sometimes. Two options:

  1. Usually re-triggering the build would get these to pass. Try running this test a few times, and check if they pass.

  2. Use UI automator to turn the screen on. Espresso test fails with NoActivityResumedException often

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sleethma
Solution 2 Hesam
Solution 3 Sotti
Solution 4 Isa Vell
Solution 5 Swarnava Chakraborty
Solution 6 Community