0

I am writing my test cases for my app however have come into some minor problems. Many of my test cases have SystemClock.Sleep calls in them, in order for the view to load all the data and display it on the screen. However the number of sleeps to do this has increasingly grown causing the time of these tests to be even longer.

Here is an example of one of these tests

@Test
public void testSearch() {

    ExtensionForExpresso.signUp();

    SystemClock.sleep(17000);

    onView(withId(R.id.menu_offer_search)).check(matches(isDisplayed())).perform(click());

    SystemClock.sleep(5000);

    onView(withId(R.id.menu_search)).check(matches(isDisplayed())).perform(typeText("Pizza"));

    SystemClock.sleep(17000);

    onView(withId(R.id.searchSectionSeeAllButton)).check(matches(isDisplayed())).perform(click());

    SystemClock.sleep(15000);

    onView(withId(R.id.searchResultsRecyclerView)).check(matches(isDisplayed())).perform(RecyclerViewActions.actionOnItemAtPosition(1, click()));
}

Is there an alternative to sleep that will wait for view to appear? Or are there any methods or functions I can add in to reduce the amount of SystemClock.sleep calls?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Billy Boyo
  • 819
  • 2
  • 12
  • 22

2 Answers2

0

First, try to disable animation on your device/emulator using adb shell or implement the Espresso Idling Resource in your Android project: http://matgom.com/tutorial/2016/02/21/IdlingResource.html

0

I would recommend you to implement an IdlingResource and Disable the system animations first. The easiest way to disable system animations is this.

There is no need to use the SystemClock.sleep() method. Instead you could implement a custom sleep method like this:

public void waitFor(int seconds) {
        seconds = seconds < 0 ? 0 : seconds;
            while (--seconds >= 0) {
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                     e.printStackTrace();
                }
            }
}

Then you could call it in your test like: waitFor(2); // 2 second wait

mark w.
  • 1,047
  • 9
  • 16