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

随声附和 提交于 2019-12-21 06:10:00

问题


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.


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




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




回答3:


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




回答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);
    }



回答5:


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.



来源:https://stackoverflow.com/questions/35419824/android-support-test-espresso-noactivityresumedexception-no-activities-in-stage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!