简体   繁体   English

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

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

I have an activity which has a fragment container which loads a fragment with recyclerview.我有一个活动,它有一个片段容器,它用 recyclerview 加载片段。 On clicking any of the recycler view items, it will open a new activity.单击任何回收站视图项目时,它将打开一个新活动。 I want to write an espresso test case for this scenario.我想为这个场景写一个浓缩咖啡测试用例。

My Activity:我的活动:

<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>

My Fragment xml:我的片段 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>

MyTestCase:我的测试用例:

@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. TIA。

You can use ViewMatchers for this action您可以使用 ViewMatchers 执行此操作

Example例子

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

Try the following尝试以下

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

If the above still does not work and you're sure that the recycler view is on display then launch UIAutomator and try to highlight above your recycler view, check if the thing it highlights is your recycler view, chances are your recycler view might have something on top of it which makes it not clickable (or it might not even be on display).如果以上仍然不起作用,并且您确定回收站视图正在显示,那么启动 UIAutomator 并尝试在您的回收站视图上方突出显示,检查它突出显示的内容是否是您的回收站视图,您的回收站视图可能有一些东西在它上面,这使得它不可点击(或者它甚至可能不会显示)。

Another thing to check is if there is a transition and no animation is in place, the test might be looking for your recycler view while it is not yet on display.另一件要检查的事情是,如果有过渡并且没有动画,测试可能正在寻找你的回收视图,而它还没有显示。 You might want to check on https://developer.android.com/training/testing/espresso/idling-resource您可能需要查看https://developer.android.com/training/testing/espresso/idling-resource

For a quicker check, just make the thread sleep prior to clicking on the recycler view为了更快地检查,只需在单击回收器视图之前让线程休眠

Thread.sleep(timeMs, timeout)

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

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