如何使用 tablayout 在 vi​​ewpager 中设置当前选项卡

How to set current tab in viewpager with tablayout

我有一个使用 tablayout 的自定义 viewpager(由于某些原因禁用了滑动)。内容根据选择的选项卡而变化。我想用浓缩咖啡测试一下: 1) 单击特定选项卡 2)在tab的特定页面查看一些数据 我怎样才能做到这一点。我是espresso新手

当您清楚地知道 UI 中的预期内容时,应该使用 Espresso,这样您就可以验证相同的内容。

在您的情况下,要转到任何选项卡,您可以使用查看操作点击该选项卡。 要验证数据,首先您应该知道该选项卡上预期有哪些数据,然后只需使用匹配器检查它们是否显示。

有几种方法可以做到这一点,一个简单的方法是 select 标签标题的元素,我使用这个代码:

Matcher<View> matcher = allOf(withText("TAB TITLE"),
            isDescendantOfA(withId(R.id.customTab)));
onView(matcher).perform(click());
SystemClock.sleep(800); // Wait a little until the content is loaded

您只需调整匹配器以适应您的布局。 然后,查看内容。

例如:

onView(withId(R.id.someId)).check(matches(isCompletelyDisplayed()));

或:

onView(withText(R.string.someText)).check(matches(isCompletelyDisplayed()));

您可以在此处的指定视图匹配器部分找到示例:http://developer.android.com/intl/es/training/testing/ui-testing/espresso-testing.html

如果您使用 RecyclerView 来显示选项卡的内容,请查看以下内容: Espresso RecyclerView inside ViewPager