Focus of this document is to represent goals and ways how to write android UI and integration tests.
Espresso and UIAutomator are provided by Google so focus should be around these tools and their respective wrappers e.g. Appium, Spoon etc.
String getName() - Returns the name of the idling resource(used for logging and idempotency of registration).
boolean isIdleNow() - Returns true if resource is currently idle.
void registerIdleTransitionCallback(IdlingResource.ResourceCallback callback) - Registers the given IdlingResource.ResourceCallback with the resource
As you can see in MockWebServer example and ActivityTestRule they all fall under category of JUnit rules which you can create yourself which then should be executed for each test defining its behaviour @see:
Since parameters have some issues placing them here until documentation bug is resolved:
true if the Activity should be launched once per Test method. It will be launched before the first Before method, and terminated after the last After method.
The power of idling resources lies in not having to wait for some app's processing (networking, calculations, animations, etc.) to finish with sleep(), which brings flakiness and/or prolongs the tests run. The official documentation can be found here.
There are three things that you need to do when implementing IdlingResource interface:
getName() - Returns the name of your idling resource.
isIdleNow() - Checks whether your xyz object, operation, etc. is idle at the moment.
registerIdleTransitionCallback (IdlingResource.ResourceCallback callback) - Provides a callback which you should call when your object transitions to idle.
Now you should create your own logic and determine when your app is idle and when not, since this is dependant on the app. Below you will find a simple example, just to show how it works. There are other examples online, but specific app implementation brings to specific idling resource implementations.
There have been some Google examples where they put IdlingResources in the code of the app. Do not do this. They presumably placed it there just to show how they work.
Keeping your code clean and maintaining single principle of responsibility is up to you!
Let us say that you have an activity which does weird stuff and takes a long time for the fragment to load and thus making your Espresso tests fail by not being able to find resources from your fragment (you should change how your activity is created and when to speed it up). But in any case to keep it simple, the following example shows how it should look like.
Our example idling resource would get two objects:
The tag of the fragment which you need to find and waiting to get attached to the activity.
A FragmentManager object which is used for finding the fragment.
Now that you have your IdlingResource written, you need to use it somewhere right?
Let us skip the entire test class setup and just look how a test case would look like:
Combination with JUnit rule
This is not to hard; you can also apply the idling resource in form of a JUnit test rule. For example, let us say that you have some SDK that contains Volley in it and you want Espresso to wait for it. Instead of going through each test case or applying it in setup, you could create a JUnit rule and just write:
Now since this is an example, don't take it for granted; all code here is imaginary and used only for demonstration purposes:
In case your activities, fragments and UI require some background processing a good thing to use is a MockWebServer which runs localy on an android device which brings a closed and testable enviroment for your UI.