使用Espresso检查RecyclerView的每个项目中的TextView

时间:2019-05-16 16:35:33

标签: android-recyclerview android-espresso

有一个RecyclerView(R.id.recycler_view)。每个项目(R.id.movi​​e_card)包含两个视图:ImageView和TextView(R.id.movi​​e_title_text_view)。目标是使用Espresso确认recyclerview的所有项目均不包含空文本视图。请帮助正确编写代码。

还有闲置。因此,开始测试RecyclerView的时间正确(在加载所有数据之后)。

目前尚不清楚如何断言所有R.id.movi​​e_title_text_view都不为空。 完整的测试方法如下:

@Rule
public ActivityTestRule activityTestRule = new ActivityTestRule(MainActivity.class);

@Test
public void GridFragmentRecycleViewTest(){
    final int TESTED_VIEWHOLDERS_QUANTITY = 10;

    IdlingResource componentIdlingResource = getIdlingResource();
    Espresso.registerIdlingResources(componentIdlingResource);
    Log.d(TAG, "GridFragmentRecycleViewTest()");
    onView(withId(R.id.action_search)).perform(click());
    onView(isAssignableFrom(SearchView.class)).perform(typeSearchViewText("lord")).perform(pressKey(KeyEvent.KEYCODE_ENTER));
    EspressoIdlingResource.increment();
    onView(withId(R.id.recycler_view)).check(new RecyclerViewItemCountAssertion(TESTED_VIEWHOLDERS_QUANTITY));

    //up to here all works good
    onView(allOf(withId(R.id.movie_title_text_view))).check(matches(withText(not(isEmptyString()))));
}

public static ViewAction typeSearchViewText(final String text){
    return new ViewAction(){
        @Override
        public Matcher<View> getConstraints() {
            //Ensure that only apply if it is a SearchView and if it is visible.
            return allOf(isDisplayed(), isAssignableFrom(SearchView.class));
        }

        @Override
        public String getDescription() {
            return "Change view text";
        }

        @Override
        public void perform(UiController uiController, View view) {
            ((SearchView) view).setQuery(text,false);
        }


    };
}

该行:

onView(allOf(withId(R.id.movi​​e_title_text_view)))。check(matches(withText(not(isEmptyString()))));

产生以下异常: androidx.test.espresso.AmbiguousViewMatcherException:'(ID:com.example.myapplication3:id / movie_title_text_view)'匹配层次结构中的多个视图。 问题视图下方带有“ **** MATCHES ****”标记。

查看层次结构: +> DecorView {id = -1,可见性=可见,宽度= 1440,高度= 2560,has-focus = true,has-focusable = true,has-window-focus = true,is-clickable = false,已启用= true,is-focused = false,is-focusable = false,is-layout-requested = false,is-selected = false,layout-params = {(0,0)(fillxfill)ty = BASE_APPLICATION wanim = 0x10302f8 fl = LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED DRAWS_SYSTEM_BAR_BACKGROUNDS pfl = FORCE_DRAW_STATUS_BAR_BACKGROUND},标记=空,root-is-layout-requested = false,has-input-connection = false,x = 0.0,y = 0.0,child-count = 3} ...

1 个答案:

答案 0 :(得分:0)

这还不能立即使用答案,但是如果您没有其他答案,则有望帮助您朝正确的方向前进。

  1. 您需要依靠RecyclerView查找视图持有者的功能,并进行构建 断言( final KTable<String, Order> orderTable = builder.table("order-topic"); Don't know how to do this further..... final KTable<String,Long> orderByIdTable = ?
  2. 使用RecyclerView.ViewHolder holder = recyclerViewInstance.findViewHolderForAdapterPosition(index);或例如holder.itemView.findViewsWithText(listOfViews, text, FIND_VIEWS_WITH_TEXT);
  3. 查找相关的视图持有者视图
  4. 遍历您拥有的项目,并对每个项目执行以上操作 项目

您可能要为此构建一些视图断言:

holder.itemView.findViewById(yourViewId);