Chad Bingham Chad Bingham - 1 year ago 333
Android Question

Espresso: Thread.sleep( );

Espresso claims that there is no need for

, but my code doesn't work unless I include it. I am connecting to an IP. While connecting, a progress dialog is shown. I need a
to wait for the dialog to dismiss. This is my test snippet where I use it:

IP.enterIP(); // fills out an IP dialog (this is done with espresso)

//progress dialog is now shown


I have tried this code with and without the
but it says
doesn't exist. The only way I can get it to work is with sleep.

Also, I have tried replacing
with things like
and still no luck.

Is this the only way to do this? Or am I missing something?

Thanks in advance.

Answer Source

On my mind correct approach will be:

/** Perform action of waiting for a specific view id. */
public static ViewAction waitId(final int viewId, final long millis) {
    return new ViewAction() {
        public Matcher<View> getConstraints() {
            return isRoot();

        public String getDescription() {
            return "wait for a specific view with id <" + viewId + "> during " + millis + " millis.";

        public void perform(final UiController uiController, final View view) {
            final long startTime = System.currentTimeMillis();
            final long endTime = startTime + millis;
            final Matcher<View> viewMatcher = withId(viewId);

            do {
                for (View child : TreeIterables.breadthFirstViewTraversal(view)) {
                    // found view with required ID
                    if (viewMatcher.matches(child)) {

            while (System.currentTimeMillis() < endTime);

            // timeout happens
            throw new PerformException.Builder()
                    .withCause(new TimeoutException())

And then pattern of usage will be:

// wait during 15 seconds for a view
onView(isRoot()).perform(waitId(, Sampling.SECONDS_15));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download