Espresso
Litho provides basic support for the
Espresso UI Testing
framework. For the time being, we only provide shallow selectors that will match ComponentHost
s and
LithoView
s to run very high-level tests against. If you find that too limiting, please
reach out to us and describe your use case.
Getting Started
We ship with a bunch of custom matchers, which will hopefully make your life easier.
The matchers exist as their own litho-espresso
package, so make sure to add the
dependencies to your build.gradle
file alongside your Espresso setup:
dependencies {// ...androidTestCompile 'com.facebook.litho:litho-espresso:0.36.0'androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'}
Matching
In order to instruct Litho to record where elements are mounted and give you access to
testKey
s you can set in your Component
s, replace your usual ActivityTestRule
with the specialized LithoActivityTestRule
:
Afterwards, you can check against ComponentHost
s and LithoView
s as you normally would
for standard Android View
s. For instance, let's assume you have a mounted
clickable component like this present in the selected activity:
Then, you could write an Espresso test like this:
Alternatively, you can match against components by using testKey
. testKey
s allow
you to specify a string for a Component, similar to an Android viewTag
, but without
sacrificing view flattening. Instead, Litho maintains a separate data structure to keep
track of the keys and associated metadata, but only if the end-to-end test mode is enabled
through the use of LithoActivityTestRule
:
Next
Either head back to the testing overview or continue with the next section, Running Tests in Android Studio.
Additional Resources
There are plenty of additional matchers available, which you can look up in the API docs: