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());
}
}
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.
Using
ActivityTestRuleyou need to set initialTouchMode and launchActivity.Use this
Also don't forget to unregister IdlingResources in @After test method.