7

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Hesam
  • 52,260
  • 74
  • 224
  • 365

6 Answers6

27

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

sleethma
  • 371
  • 3
  • 6
5

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

Hesam
  • 52,260
  • 74
  • 224
  • 365
  • 1
    You CAN run tests on Activity B or C without first starting Activity A. If those activities use data from an intent to start, you'll have to use `ActivityTestRule` instead of `IntentsTestRule` and start the activity with `myActivityRule.launchActivity(myIntent)`, where `myIntent` has the initialization info needed to start the activity. – tronman Jul 26 '18 at 21:57
  • @tronman, `IntentsTestRule` extends `ActivityTestRule` so it would be fine to leave that unaltered. You are correct about needing to use `launchActivity(Intent)` though. – vaughandroid Aug 17 '18 at 12:55
2

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);
    }
Isa Vell
  • 89
  • 1
  • 5
2

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.

Sotti
  • 14,089
  • 2
  • 50
  • 43
  • This would only be relevant for multi module builds with instrumentation tests in multiple modules, right? – StefanTo Jun 11 '21 at 06:23
0

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'
-1

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

Community
  • 1
  • 1
Mayank Kapoor
  • 41
  • 2
  • 4