Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso test for bottomsheet

I am creating an espresso test for bottom sheet where the sheet has 5 linearly arranged textview and the first textview is visible and clicking the first textview will expand the entire bottom sheet revealing all the items, and then the third item is needed to be clicked for the test to pass.

The problem is that espresso doesn't click the textview of the bottom sheet after expanding until I put a Thread.sleep(1000).

I even tried IdlingResource, I set it to false once the click is registered on the first item and after the onStateChanged is called on bottomSheetBehaviour I set it to true again but still no help.

This doesn't work :

onView(withId(R.id.toolsItem1)).perform(click());
onView(withId(R.id.toolsItem3)).check(matches(isDisplayed()));
onView(withId(R.id.toolsItem3)).perform(click());

This does :

onView(withId(R.id.toolsItem1)).perform(click());
Thread.sleep(1000);
onView(withId(R.id.toolsItem3)).check(matches(isDisplayed()));
onView(withId(R.id.toolsItem3)).perform(click());
like image 704
Nikhil Soni Avatar asked Nov 20 '17 05:11

Nikhil Soni


People also ask

What is Espresso UI Test?

Espresso is an open source android user interface (UI) testing framework developed by Google. The term Espresso is of Italian origin, meaning Coffee. Espresso is a simple, efficient and flexible testing framework.


1 Answers

I had the same problem. From my point of view the Thread.sleep() solution sadly seems like the way to go.

But I used a third party library called https://github.com/awaitility/awaitility to wait until the state of the bottomsheet changes rather than a fixed period. I used it like this:

await().atMost(5, SECONDS).until(() -> fragment.bottomSheetBehavior.getState()==BottomSheetBehavior.STATE_EXPANDED);

Where fragment is the fragment to test which you can get for example like this:

MyFragment fragment = (MyFragment) activityTestRule.getActivity().getSupportFragmentManager().findFragmentByTag("Tag");
like image 99
KaRa Avatar answered Oct 24 '22 20:10

KaRa