如何将重点放在浓缩咖啡中的页面元素上



我试图在我的浓缩咖啡测试中检查/取消选中一个复选框:

 termsAndConditionsCheckbox.check(matches(isChecked()));
 termsAndConditionsCheckbox.perform(scrollTo()).perform(click());
 termsAndConditionsCheckbox.check(matches(isNotChecked()));

但是出现错误:

Error performing 'scroll to' on view
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
          (view has effective visibility=VISIBLE and is descendant of a: (is assignable from class: class android.widget.ScrollView or is assignable from class: class android.widget.HorizontalScrollView))
          Target view: "AppCompatCheckBox{id=2131689839, res-name=tnc_checkbox, visibility=VISIBLE, width=96, height=96, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-conn

通过删除scrollto并仅使用click()尝试尝试。但是仍然无法执行单击。

您收到的错误消息指出,您的CheckBox必须是VISIBLE,也必须是ScrollViewHorizontalScrollView的孩子。您的CheckBox确实是VISIBLE,但不是ScrollView的孩子。因此,如果您有类似的布局:

<LinearLayout ...>
    <TextView android:id="@+id/lbl_license_text" ... />
    <CheckBox android:id="@+id/chk_accept" ... />
</LinearLayout>

您需要将其包装在ScrollView中,以便视图可以在较小屏幕设备上滚动:

<ScrollView ...>
    <LinearLayout ...>
        <TextView android:id="@+id/lbl_license_text" ... />
        <CheckBox android:id="@+id/chk_accept" ... />
    </LinearLayout>
</ScrollView>

(显然,用该元素的android:属性代替...的实例)

这将允许浓缩咖啡在运行测试时向下滚动到CheckBox

我能够通过实现自定义查看功能来解决此问题(在stackoverflow上的答案之一中看到了它)

 public static ViewAction setChecked(final boolean checked) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return new Matcher<View>() {
                @Override
                public boolean matches(Object item) {
                    return isA(Checkable.class).matches(item);
                }
                @Override
                public void describeMismatch(Object item, Description mismatchDescription) {}
                @Override
                public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {}
                @Override
                public void describeTo(Description description) {}
            };
        }
        @Override
        public String getDescription() {
            return null;
        }
        @Override
        public void perform(UiController uiController, View view) {
            Checkable checkableView = (Checkable) view;
            checkableView.setChecked(checked);
        }
    };
}

然后致电:

termsAndConditionsCheckbox.perform(Helper.setChecked(false));

取消选中并传递true以检查复选框。

最新更新