Я новичок в инфраструктуре тестирования Espresso и для начала я вытащил случайный проект на Github под названием SeriesGuide .
Я использую Android Studio, и там я использую встроенный Test Recorder для создания простых тестов пользовательского интерфейса. Но проблема в том, что я получаю следующую ошибку
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));
}
};
}
}
Если бы кто-то мог направить меня в правильном направлении для проведения теста, я был бы очень признателен.