How to set current tab in viewpager with tablayout

Deadly 提交于 2019-12-09 08:04:15

问题


I have a custom viewpager (with swiping disabled for reasons) working with a tablayout. The content changes based on which tab is selected. I want to test this using espresso: 1) Click on a particular tab 2) Check some data in a particular page of the tab How can I do that. I am a novice to espresso


回答1:


There are several ways of doing this, an easy one is to select the element by the tab title, I use this code:

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

You only have to adapt the matcher to your layout. Then, check the content.

For example:

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

or:

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

You can find examples here in the Specifying a View Matcher section: http://developer.android.com/intl/es/training/testing/ui-testing/espresso-testing.html

If you are using a RecyclerView to show the content of the tabs have a look at this: Espresso RecyclerView inside ViewPager




回答2:


Espresso should be used when you clearly know what is expected in the UI and so you can verify the same.

In your case, to go to any tab, you can use view action to tap on that tab. To verify the data, first you should know what data is expected to be there on that tab and then just use matchers to check if they are displayed or not.



来源:https://stackoverflow.com/questions/33640635/how-to-set-current-tab-in-viewpager-with-tablayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!