Test loader displayed before data with Espresso

2019-07-28 05:57发布

问题:

I have a fragment, which when loaded should display a loading spinner which disappears once then the fields are populated when a network request completes. For testing I can mock the request.

In every case it is very fast and You won't usually see the Loading spinner. Is it possible with Espresso to test that this view is shown first? Even when it disappears so quickly?

What are my options to delay the load long enough to make it testable? (Is there some thing in the framework, rather than sleeping the thread?)

This is the result of my test currently:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'is displayed on the screen to the user' doesn't match the selected view.
Expected: is displayed on the screen to the user
Got: "AppCompatImageView{id=2131624534, res-name=loading_spinner, visibility=GONE, width=126, height=126, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=true, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=477.0, y=383.0}"

回答1:

You must disable the loader animations or espresso will wait for them to finish before continuing with the test.

You can replace the progressbar views with plain views for testing: https://blog.entwicklerbier.org/2015/05/test-your-ui-on-android-with-espresso-damn-you-progressbar/

Or you can change the progressbar drawable:

ProgressBars and Espresso Or Testing progress bar on Android with Espresso

And maybe you should also delay the load as you said.