Espresso:单击 ListView 页脚中按钮的操作

Espresso: Click action on button in ListView footer

ListView 有这样的页脚 (footer.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_cancel"
        style="@style/MyApp.Form.Component.Button"
        android:text="@string/action_cancel"/>

    <Button
        android:id="@+id/btn_confirm"
        style="@style/MyApp.Form.Component.Button"
        android:text="@string/action_next"/>
</RelativeLayout>

在录制脚本时(通过 Android Studio 中的 'Record Espresso Test')我收到以下代码以单击按钮 'Next':

private void clickNext() {
    ViewInteraction appCompatButton2 = onView(
            allOf(withId(R.id.btn_confirm), withText("Next"),
                    withParent(childAtPosition(
                            withId(R.id.lv_products), 5))));
    appCompatButton2.perform(click());
}

效果不错,但是...在低分辨率设备上运行测试时出现错误:

android.support.test.espresso.PerformException: Error performing 'single click' on view '(with id: com.comarch.msc.emulator:id/btn_confirm and with text: is "Next" and has parent matching: Child at position 5 in parent with id: com.comarch.msc.emulator:id/lv_products)'. (...) Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints: at least 90 percent of the view's area is displayed to the user.

如何针对较低的分辨率自定义测试?

问题是整个页脚没有显示在屏幕上(如果显示则只显示部分)。您可以尝试在单击按钮之前执行 swipeUp 操作。或者,如果滑动没有帮助,只需检查为什么在较小的屏幕上看不到它。