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());
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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With