Rainmaker
Rainmaker

Reputation: 11090

Wait for asynchronous operation inside Espresso test

I have espresso test for Android:

        @Test
        public void ExternalBankAccountDetailsFragment_InfoShown() throws InterruptedException {

            // SETUP
            mockRequest();
            clickOnItem();
mActivityRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

            Thread.sleep(100);

            // CHECKS
    onView(withId(R.id.view)).check(matches(hasDescendant(withText("test"))));
        }

And I need to use Thread.sleep to wait the async call. But I know that this is not a good practice. I'm familiar with IdlingResources, but I can not use it because I have to change my code to use it (for ex. CountingIdlingResource usage inside callback etc). How can I avoid usage of Thread.sleep()?

Upvotes: 0

Views: 570

Answers (1)

Kaskasi
Kaskasi

Reputation: 1340

In the end all solutions will do something like Thread.sleep. The question is: How can you only wait for the minimum required amount of time?

One alternative to Idling Resources is this tiny ConditionWatcher library:https://github.com/AzimoLabs/ConditionWatcher

You can create an Instruction that waits checks if the current activity can find a view example

Upvotes: 1

Related Questions