Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing progress bar on Android with Espresso

The workflow should be the following:

  1. Activity starts
  2. Progress bar is visible
  3. Network request fires (idling resource is already registered so espresso knows how to wait for it).
  4. Progress bar is hidden
  5. Text from network is shown.

Up to this point, I have written assertions for steps 1, 3, 5 and it works perfectly:

onView(withText("foo 1"))
    .check(matches(isDisplayed()));

Problem is, I have no idea how to let espresso know to verify the visibility of progress bar before the request is made and after the request is made.

Consider the onCreate() method is the following:

super.onCreate(...);
setContentView(...);

showProgressBar(true);
apiClient.getStuff(new Callback() {
    public void onSuccess() {
        showProgressBar(false);
    }
});

I have tried the following but it doesn't work:

// Activity is launched at this point.
activityRule.launchActivity(new Intent());

// Up to this point, the request has been fired and response was 
// returned, so the progress bar is now GONE.
onView(withId(R.id.progress_bar))
   .check(matches(isDisplayed()));

onView(withId(R.id.progress_bar))
    .check(matches(not(isDisplayed())));

The reason this is happening is because, since the client is registered as an idling resource, espresso will wait until it is idle again before running the first onView(...progressbar...)... so I need a way to let espresso know to run that BEFORE going to idle.

EDIT: this doesn't work either:

idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
        @Override
        public void onTransitionToIdle() {
            onView(withId(R.id.progress_bar))
                    .check(matches(isDisplayed()));
        }
    });
like image 984
Christopher Francisco Avatar asked Feb 03 '16 20:02

Christopher Francisco


People also ask

What is the use of espresso in Android?

Espresso is a testing framework that helps developers write automation test cases for user interface (UI) testing. It has been developed by Google and aims to provide a simple yet powerful framework. It allows both black-box testing as well as testing of individual components during development cycles.

How do you check espresso visibility?

One simple way to check for a View or its subclass like a Button is to use method getVisibility from View class. I must caution that visibility attribute is not clearly defined in the GUI world. A view may be considered visible but may be overlapped with another view, for one example, making it hidden.

What is Espresso UI testing?

Espresso is an open source android user interface (UI) testing framework developed by Google. The term Espresso is of Italian origin, meaning Coffee. Espresso is a simple, efficient and flexible testing framework.


1 Answers

Espresso has problems with the animation. You can just set the drawable of the progress bar to something static just for the test and it works as expected.

Drawable notAnimatedDrawable = ContextCompat.getDrawable(getActivity(), R.drawable.whatever);
((ProgressBar) getActivity().findViewById(R.id.progress_bar)).setIndeterminateDrawable(notAnimatedDrawable);

onView(withId(R.id.progress_bar)).check(matches(isDisplayed()));
like image 65
Matthias Robbers Avatar answered Sep 20 '22 16:09

Matthias Robbers