エスプレッソ使用してリサイクルビューの子のボタンをクリックしての実行

person101:

活動の中で、私は以下に示すように2 textviewsと2つのボタンを含むカスタムリサイクラービューを持っています。

ここでは、画像の説明を入力します。

リサイクルビンアイコンボタンのidはdeleteButtonです。私はエスプレッソ、私はクリックを模擬することができるようにテスト中に、このボタンにアクセスしたいと思います。

私がしようとして失敗している二つの方法は以下の通りであります:

        onView(withId(R.id.basket))
                .perform(actionOnItemAtPosition(4, click() ));

        onView(withText("Cherry"))
                .perform(
                        RecyclerViewActions.actionOnItem(
                                hasDescendant(withId(R.id.deleteButton)),
                                ViewActions.click()
                        )
                );

事前に任意の助けをありがとうございました。

アーロン :

内部のビューをクリックする一つの方法はRecyclerViewアイテムビューは、カスタムビューのアクションを作成することです。

public static ViewAction actionOnItemView(Matcher<View> matcher, ViewAction action) {

    return new ViewAction() {

        @Override public String getDescription() {
            return String.format("performing ViewAction: %s on item matching: %s", action.getDescription(), StringDescription.asString(matcher));
        }

        @Override public Matcher<View> getConstraints() {
            return allOf(withParent(isAssignableFrom(RecyclerView.class)), isDisplayed());
        }

        @Override public void perform(UiController uiController, View view) {
            List<View> results = new ArrayList<>();
            for (View v : TreeIterables.breadthFirstViewTraversal(view)) {
                if (matcher.matches(v)) results.add(v);
            }
            if (results.isEmpty()) {
                throw new RuntimeException(String.format("No view found %s", StringDescription.asString(matcher)));
            } else if (results.size() > 1) {
                throw new RuntimeException(String.format("Ambiguous views found %s", StringDescription.asString(matcher)));
            }
            action.perform(uiController, results.get(0));
        }
    };
}

その後のいずれかを使用RecyclerViewActionsあなたの上にRecyclerView、その後、actionOnItemView成功した場合に、アイテムビュー上、その後のアクションとして:

ViewAction itemViewAction = actionOnItemView(withId(R.id.deleteButton), click());
onView(withId(your_recycler_view)).perform(actionOnItemAtPosition(4, itemViewAction));

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=403188&siteId=1