Espresso onView doesn't wait for element

9.4k Views Asked by At

I just started to work with Espresso recorder. I made my first test, and from what I can see function onView, that's suppose to wait for object to continue doesn't do the work. It always return :

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching.

Is there any function that would work as wait for that I could use instead?

package com.mytest;


import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;



import org.junit.Rule;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;

import com.mytest.R;



import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class Test1 {

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    private IdlingResource mIdlingResource;

    @Before
    public void registerIdlingResource() {
        mIdlingResource = mActivityRule.getActivity().getIdlingResource();
        Espresso.registerIdlingResources(mIdlingResource);
    }

    @Test
    public void test1() {
        ViewInteraction recyclerView = onView(
                allOf(withId(R.id.recycler_view), isDisplayed()));
        recyclerView.perform(actionOnItemAtPosition(0, click()));

        ViewInteraction relativeLayout = onView(
                allOf(withId(R.id.capture_layout), isDisplayed()));
        relativeLayout.perform(click());

        ViewInteraction relativeLayout2 = onView(
                allOf(withId(R.id.like_layout),
                        withParent(allOf(withId(R.id.cameraLayout),
                                withParent(withId(android.R.id.content)))),
                        isDisplayed()));
        relativeLayout2.perform(click());

        ViewInteraction relativeLayout3 = onView(
                allOf(withId(R.id.exit_layout), isDisplayed()));
        relativeLayout3.perform(click());

    }
}
3

There are 3 best solutions below

0
On

I just adapted MiguelSlv's solution to return a ViewInteraction as onView does.

So now we have an onDelayedView equivalent to onView but it does wait for the view to exist in the hierarchy

/**
 * Special version of `onView`.
 *
 * Sometimes onView produces an exception because the view isn't ready on the hierarchy.
 * It will wait for the view to exist and then resolve using `onView`.
 */
fun onDelayedView(viewMatcher: Matcher<View?>, millis: Long = 500): ViewInteraction {
    val latch = CountDownLatch(1)

    val action: ViewAction = object : ViewAction {
        override fun getConstraints(): Matcher<View> {
            return isRoot()
        }

        override fun getDescription(): String {
            return "Wait for a view with id <$viewMatcher> to exist, during $millis millis."
        }

        override fun perform(uiController: UiController, view: View) {
            uiController.loopMainThreadUntilIdle()

            val startTime = System.currentTimeMillis()
            val endTime = startTime + millis

            do {
                for (child in TreeIterables.breadthFirstViewTraversal(view)) {
                    if (viewMatcher.matches(child)) {
                        // Found
                        latch.countDown()
                        return
                    }
                }
                uiController.loopMainThreadForAtLeast(100)
            } while (System.currentTimeMillis() < endTime)

            // Not found
            throw PerformException.Builder()
                .withActionDescription(description)
                .withViewDescription(HumanReadables.describe(view))
                .withCause(TimeoutException())
                .build()
        }
    }

    onView(isRoot()).perform(action)
    latch.await()

    return onView(viewMatcher)
}

It doesn't use a "found" flag anymore as it throws a PerformException if the view doesn't exist after the waiting (500ms by default).

Usage:

onDelayedView(withId(R.id.your_view_id_here))
            .perform(/* your preferred view actions */)
0
On

Update: Looks like you are using RecyclerView, Try below code.

Also if you making any network calls, You need to implement RecyclerView/Network IdlingResource to tell Espresso to wait for data to populate before executing test steps.

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, true);

    private MainActivity mMainActivity = null;
    private IdlingResource mIdlingResource;

    @Before
    public void registerIdlingResource() {
        mMainActivity = mActivityRule.getActivity();
    }

    @Test
    public void test1() {

        mIdlingResource = mActivityRule.getActivity().getIdlingResource();
        Espresso.registerIdlingResources(mIdlingResource);

        mActivityRule.launchActivity(MainActivity.createIntent(getTargetContext()));

        ViewInteraction recyclerView = onView(
                allOf(withId(R.id.recycler_view), isDisplayed()));
        recyclerView.perform(actionOnItemAtPosition(0, click()));

        ViewInteraction relativeLayout = onView(
                allOf(withId(R.id.capture_layout), isDisplayed()));
        relativeLayout.perform(click());

        ViewInteraction relativeLayout2 = onView(
                allOf(withId(R.id.like_layout),
                        withParent(allOf(withId(R.id.cameraLayout),
                                withParent(withId(android.R.id.content)))),
                        isDisplayed()));
        relativeLayout2.perform(click());

        ViewInteraction relativeLayout3 = onView(
                allOf(withId(R.id.exit_layout), isDisplayed()));
        relativeLayout3.perform(click());

        Espresso.unregisterIdlingResources(mIdlingResource);

    }

Using ActivityTestRule you need to set initialTouchMode and launchActivity.

Use this

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, true);

Also don't forget to unregister IdlingResources in @After test method.

0
On

One way is to loop with the matcher until it happens. I use the following helper when i need to wait for async tasks to complete.

public class ViewSynchronizer {
    private static final String TAG = "MC_Synchronizer";

    public static boolean viewExists(final Matcher<View> viewMatcher, final long millis) throws InterruptedException {
        final Boolean[] found = new Boolean[1];

        final CountDownLatch latch = new CountDownLatch(1);
        ViewAction action = new ViewAction() {
            @Override
            public Matcher<View> getConstraints() {
                return isRoot();
            }

            @Override
            public String getDescription() {
                return "wait for a specific view with id <" + viewMatcher.toString() + "> during " + millis + " millis.";
            }

            @Override
            public void perform(final UiController uiController, final View view) {
                uiController.loopMainThreadUntilIdle();
                final long startTime = System.currentTimeMillis();
                final long endTime = startTime + millis;


                do {
                    for (View child : TreeIterables.breadthFirstViewTraversal(view)) {

                        if (viewMatcher.matches(child)) {
                            Log.d(TAG, "perform: found match");
                            found[0] = true;
                            latch.countDown();
                            return;
                        }
                    }

                    uiController.loopMainThreadForAtLeast(50);
                }
                while (System.currentTimeMillis() < endTime);

                found[0] = false;
                latch.countDown();
            }
        };
        onView(isRoot()).perform(action);

        latch.await();
        return found[0];
    }
}

Usage:

 Assert.assertTrue(viewExists(allOf(withId(R.id.account_sign_out_btb),withEffectiveVisibility(ViewMatchers.Visibility.GONE)),2000)); //wait 2 seconds