使用测试记录器创建测试时,Android Espresso提供NoMatchingViewException

时间:2019-06-10 18:31:05

标签: android android-studio android-espresso ui-testing android-espresso-recorder

我是Espresso测试框架的新手,要开始使用,我在Github上拉了一个名为SeriesGuide的随机项目。

我正在使用Android Studio,在那里我使用内置的Test Recorder进行简单的UI测试。但是问题是我得到了以下错误

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching:

我要创建的测试是将影片添加到用户的监视列表,然后导航到所述监视列表。这是我目前拥有的

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

@Rule
public ActivityTestRule<ShowsActivity> mActivityTestRule = new ActivityTestRule<>(
        ShowsActivity.class);

@Test
public void showsActivityTest3() {
    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(7000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction appCompatImageButton = onView(
            allOf(withContentDescription("Open navigation drawer"),
                    childAtPosition(
                            allOf(withId(R.id.sgToolbar),
                                    childAtPosition(
                                            withClassName(
                                                    is("com.google.android.material.appbar.AppBarLayout")),
                                            0)),
                            2),
                    isDisplayed()));
    appCompatImageButton.perform(click());

    ViewInteraction navigationMenuItemView = onView(
            allOf(childAtPosition(
                    allOf(withId(R.id.design_navigation_view),
                            childAtPosition(
                                    withId(R.id.navigation),
                                    0)),
                    3),
                    isDisplayed()));
    navigationMenuItemView.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction cardView = onView(
            allOf(childAtPosition(
                    allOf(withId(R.id.recyclerViewMoviesDiscover),
                            childAtPosition(
                                    withId(R.id.swipeRefreshLayoutMoviesDiscover),
                                    0)),
                    5),
                    isDisplayed()));
    cardView.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction appCompatButton = onView(
            allOf(withId(R.id.buttonMovieWatchlisted), withText("Add to watchlist"),
                    childAtPosition(
                            childAtPosition(
                                    withId(R.id.containerMovieButtons),
                                    0),
                            1),
                    isDisplayed()));
    appCompatButton.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(160);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction appCompatImageButton2 = onView(
            allOf(withContentDescription("Navigate up"),
                    childAtPosition(
                            allOf(withId(R.id.sgToolbar),
                                    childAtPosition(
                                            withClassName(is("android.widget.FrameLayout")),
                                            1)),
                            0),
                    isDisplayed()));
    appCompatImageButton2.perform(click());

    // Added a sleep statement to match the app's execution delay.
    // The recommended way to handle such scenarios is to use Espresso idling resources:
    // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
    try {
        Thread.sleep(700);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    ViewInteraction linearLayout = onView(
            allOf(childAtPosition(
                    childAtPosition(
                            withId(R.id.tabLayoutTabs),
                            0),
                    1),
                    isDisplayed()));
    linearLayout.perform(click());
}

private static Matcher<View> childAtPosition(
        final Matcher<View> parentMatcher, final int position) {

    return new TypeSafeMatcher<View>() {
        @Override
        public void describeTo(Description description) {
            description.appendText("Child at position " + position + " in parent ");
            parentMatcher.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent parent = view.getParent();
            return parent instanceof ViewGroup && parentMatcher.matches(parent)
                    && view.equals(((ViewGroup) parent).getChildAt(position));
        }
    };
}

}

如果有人能指出正确的方向来进行测试,将不胜感激。

0 个答案:

没有答案