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