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

Hesam picture Hesam · Feb 15, 2016 · Viewed 10.4k times · Source

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.

Answer

sleethma picture sleethma · Jul 3, 2017

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)!