繁体   English   中英

如何使用 recyclerview 单击项目操作为片段容器编写 espresso 测试用例

[英]How to write an espresso test case for a fragment container with recyclerview click item action

我有一个活动,它有一个片段容器,它用 recyclerview 加载片段。 单击任何回收站视图项目时,它将打开一个新活动。 我想为这个场景写一个浓缩咖啡测试用例。

我的活动:

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background">

<FrameLayout
    android:id="@+id/fragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:layout_weight="1">

</FrameLayout>

我的片段 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

我的测试用例:

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

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

    @Before
    public void init() {
        mActivityTestRule.getActivity()
                .getSupportFragmentManager().beginTransaction();
    }

    @Test
    public void recyclerview_clickTest() {
       /* onView(withId(R.id.fragmentContainer)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
        onView(withText("Alpha")).perform(click());
        onView(allOf(instanceOf(TextView.class), withParent(withResourceName("action_bar"))))
                .check(matches(withText("Alpha")));*/

        onData(allOf(is(new BoundedMatcher<Object, MyModel>(MyModel.class) {
            @Override
            public void describeTo(Description description) {
            }
            @Override
            protected boolean matchesSafely(MyModel abc) {
                return onView(allOf(instanceOf(TextView.class), withParent(withResourceName("action_bar"))))
                        .check(matchesSafely(withResourceName(abc)));
            }
        })));
    }
}

TIA。

您可以使用 ViewMatchers 执行此操作

例子

onView(withId(R.id.your_recycler_view)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

尝试以下

onView(allOf(isDisplayed(), withId(R.id.your_recycler_view))).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

如果以上仍然不起作用,并且您确定回收站视图正在显示,那么启动 UIAutomator 并尝试在您的回收站视图上方突出显示,检查它突出显示的内容是否是您的回收站视图,您的回收站视图可能有一些东西在它上面,这使得它不可点击(或者它甚至可能不会显示)。

另一件要检查的事情是,如果有过渡并且没有动画,测试可能正在寻找你的回收视图,而它还没有显示。 您可能需要查看https://developer.android.com/training/testing/espresso/idling-resource

为了更快地检查,只需在单击回收器视图之前让线程休眠

Thread.sleep(timeMs, timeout)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM