Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set current tab in viewpager with tablayout

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

like image 662
user2759617 Avatar asked Nov 10 '15 22:11

user2759617


People also ask

Can I use TabLayout without ViewPager?

These methods have a features: always attaching the "tab widget" with a ViewPager , and in order to make this requirement, we must disable swipe feature of the ViewPager . Now, with Material design, we now use TabLayout widget, which can "stand alone" to build a tab bar and do not need a ViewPager anymore.


1 Answers

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

like image 98
jeprubio Avatar answered Sep 30 '22 18:09

jeprubio