Espresso, scrolling not working when NestedScrollView or RecyclerView is in CoordinatorLayout

This is happening because the Espresso scrollTo() method explicitly checks the layout class and only works for ScrollView & HorizontalScrollView. Internally it’s using View.requestRectangleOnScreen(…) so I’d expect it to actually work fine for many layouts. My workaround for NestedScrollView was to take ScrollToAction and modify that constraint. The modified action worked fine for NestedScrollView with … Read more

Testing multiple activities with espresso

Yes, it is possible. In one of the samples they have demoed this here https://github.com/googlesamples/android-testing/blob/master/ui/espresso/BasicSample/app/src/androidTest/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorTest.java @Test public void changeText_newActivity() { // Type text and then press the button. onView(withId(R.id.editTextUserInput)).perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard()); onView(withId(R.id.activityChangeTextBtn)).perform(click()); // This view is in a different Activity, no need to tell Espresso. onView(withId(R.id.show_text_view)).check(matches(withText(STRING_TO_BE_TYPED))); } Read the inline comment. Waiting for the new activity … Read more

Click home icon with Espresso

To not depend on the app locale, you can use the code from Matt Logan by replacing “Navigate up” with R.string.abc_action_bar_up_description: onView(withContentDescription(R.string.abc_action_bar_up_description)).perform(click()); This helped me a lot because I have an app in more than 5 languages and I had to act like this.

Testing Snackbar show with Espresso

This worked for me, please try. onView(allOf(withId(android.support.design.R.id.snackbar_text), withText(“My text”))) .check(matches(isDisplayed())); If you use AndroidX, please use the following: onView(withId(com.google.android.material.R.id.snackbar_text)) .check(matches(withText(R.string.whatever_is_your_text)))

Espresso: return boolean if view exists

Conditional logic in tests is undesirable. With that in mind, Espresso’s API was designed to guide the test author away from it (by being explicit with test actions and assertions). Having said that, you can still achieve the above by implementing your own ViewAction and capturing the isDisplayed check (inside the perform method) into an … Read more

Get Current Activity in Espresso android

In Espresso, you can use ActivityLifecycleMonitorRegistry but it is not officially supported, so it may not work in future versions. Here is how it works: Activity getCurrentActivity() throws Throwable { getInstrumentation().waitForIdleSync(); final Activity[] activity = new Activity[1]; runTestOnUiThread(new Runnable() { @Override public void run() { java.util.Collection<Activity> activities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); activity[0] = Iterables.getOnlyElement(activities); }}); return activity[0]; … Read more