My utility repeats the functions that are executed or called until it passes without errors or generates a throwable after a timeout. It works great for espresso tests!
Suppose that the last type of interaction (pressing a button) activates some background threads (network, database, etc.). As a result, a new screen should appear, and we want to check it in the next step, but we do not know when the new screen will be ready for testing.
The recommended approach is to get your application to send thread status messages to your test. Sometimes we can use built-in mechanisms such as OkHttp3IdlingResource. In other cases, you should embed code fragments in different places of the source code of your application (you should know the application logic!) Only to support testing. Moreover, we must disable all your animations (although this is part of the user interface).
Another approach awaits, for example, SystemClock.sleep (10000). But we do not know how long to wait, and even long delays cannot guarantee success. On the other hand, your test will last a long time.
My approach is to add a time condition to view the interaction. For example. we check that a new screen should appear within 10,000 ms (timeout). But we do not wait and check it as fast as we want (for example, every 100 ms) Of course, we block the test stream in this way, but usually this is exactly what we need in such cases.
Usage: long timeout=10000; long matchDelay=100; //(check every 100 ms) EspressoExecutor myExecutor = new EspressoExecutor<ViewInteraction>(timeout, matchDelay); ViewInteraction loginButton = onView(withId(R.id.login_btn)); loginButton.perform(click()); myExecutor.callForResult(()->onView(allOf(withId(R.id.title),isDisplayed())));
This is my source in the class:
package com.skb.goodsapp; import android.os.SystemClock; import android.util.Log; import java.util.Date; import java.util.concurrent.Callable; public class EspressoExecutor<T> { private static String LOG = EspressoExecutor.class.getSimpleName(); public static long REPEAT_DELAY_DEFAULT = 100; public static long BEFORE_DELAY_DEFAULT = 0; private long mRepeatDelay;
https://gist.github.com/alexshr/ca90212e49e74eb201fbc976255b47e0
alexshr May 2 '17 at 5:36 2017-05-02 05:36
source share