我正在尝试以我的形式测试EditTexts,该表单位于NestedScrollView中。我正在运行以下代码:
onView(withId(R.id.register_scroll_view)).perform(scrollTo()).perform(click());register_scroll_view是我的NestedScrollView。然而,我得到了一个例外:
使用id: com.eazyigz.myapp:id/ android.support.test.espresso.PerformException:_scroll执行‘滚动到’on view‘时出错。由: java.lang.RuntimeException: Action引起的操作将不会执行,因为目标视图不匹配以下一个或多个约束:(视图具有有效的visibility=VISIBLE,并且是a:(可从类: android.widget.ScrollView分配或从类:android.widget.HorizontalScrollView分配)的后代)
如何正确地设计这个测试,以便测试需要滚动才能显示的EditTexts?
发布于 2015-07-25 18:47:37
我对NestedScrollView没有任何经验,但似乎requestRectangleOnScreen(),也就是普通ScrollView中的浓缩咖啡卷轴,应该与NestedScrollView一起工作。
唯一的问题是ScrollView约束被硬编码到scrollTo()操作中,而NestedScrollView没有继承常规的ScrollView。
我认为这里唯一的解决方案是将整个ScrollToAction类复制并粘贴到您自己的操作实现中,并替换讨厌的约束。
发布于 2017-05-30 23:42:13
我编写了一个ViewAction,用于处理滚动到NestedScrollView子视图的视图。它还考虑到CoordinatorLayout可能是根-所以您不需要害怕工具栏改变它的大小。
有一些密码。您需要将这个类复制到某个项目中。然后您可以使用它,例如:
onView(withId(R.id.register_scroll_view))
.perform(CustomScrollActions.nestedScrollTo, click());重要:它不是scrollTo()的替代品,而是在处理NestedScrollView时应该使用的另一个滚动ViewAction。
所以我说的是一个ViewAction:
public class CustomScrollActions {
public static ViewAction nestedScrollTo() {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return Matchers.allOf(
isDescendantOfA(isAssignableFrom(NestedScrollView.class)),
withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE));
}
@Override
public String getDescription() {
return "Find parent with type " + NestedScrollView.class +
" of matched view and programmatically scroll to it.";
}
@Override
public void perform(UiController uiController, View view) {
try {
NestedScrollView nestedScrollView = (NestedScrollView)
findFirstParentLayoutOfClass(view, NestedScrollView.class);
if (nestedScrollView != null) {
CoordinatorLayout coordinatorLayout =
(CoordinatorLayout) findFirstParentLayoutOfClass(view, CoordinatorLayout.class);
if (coordinatorLayout != null) {
CollapsingToolbarLayout collapsingToolbarLayout =
findCollapsingToolbarLayoutChildIn(coordinatorLayout);
if (collapsingToolbarLayout != null) {
int toolbarHeight = collapsingToolbarLayout.getHeight();
nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
}
}
nestedScrollView.scrollTo(0, view.getTop());
} else {
throw new Exception("Unable to find NestedScrollView parent.");
}
} catch (Exception e) {
throw new PerformException.Builder()
.withActionDescription(this.getDescription())
.withViewDescription(HumanReadables.describe(view))
.withCause(e)
.build();
}
uiController.loopMainThreadUntilIdle();
}
};
}
private static CollapsingToolbarLayout findCollapsingToolbarLayoutChildIn(ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof CollapsingToolbarLayout) {
return (CollapsingToolbarLayout) child;
} else if (child instanceof ViewGroup) {
return findCollapsingToolbarLayoutChildIn((ViewGroup) child);
}
}
return null;
}
private static View findFirstParentLayoutOfClass(View view, Class<? extends View> parentClass) {
ViewParent parent = new FrameLayout(view.getContext());
ViewParent incrementView = null;
int i = 0;
while (parent != null && !(parent.getClass() == parentClass)) {
if (i == 0) {
parent = findParent(view);
} else {
parent = findParent(incrementView);
}
incrementView = parent;
i++;
}
return (View) parent;
}
private static ViewParent findParent(View view) {
return view.getParent();
}
private static ViewParent findParent(ViewParent view) {
return view.getParent();
}
}发布于 2018-09-06 14:17:41
使用
onView(withId(R.id.register_scroll_view))
.perform(swipeUp(), click())https://stackoverflow.com/questions/31614937
复制相似问题