Espresso – Click by text in List view

The problem is, that you try to match the list view itself with the instanceOf(ListView.class) as argument for onData(). onData() requires a data matcher that matches the adapted data of the ListView, not the ListView itself, and also not the View that Adapter.getView() returns, but the actual data. If you have something like this in … Read more

Android testing: Waited for the root of the view hierarchy to have window focus

This error can happen when a system dialog is displayed — like “Power Off” or “Unfortunately, Launcher has stopped” (a background app crashed) — and you try to run an Espresso unit test whilst that dialog is visible. Image credit: Android 4.0 emulator always has a crashing Launcher? You can workaround it in code by … Read more

Android Espresso – assert text on screen against string in resources

Use this function: private String getResourceString(int id) { Context targetContext = InstrumentationRegistry.getTargetContext(); return targetContext.getResources().getString(id); } You just have to call it with the id of the string and perform your action: String myTextFromResources = getResourceString(R.string.main_ent_mil_new_mileage); onView(allOf(withId(R.id.my_text_on_screen), withText(myTextFromResources)) .check(matches(isDisplayed())); *EDIT for new Espresso version: With new version of Espresso, you should be able to call directly … Read more

Android Espresso: PerformException

Animations or transitions are enabled on the target device. Espresso doesn’t work well with animations due to the visual state delays they introduce. You need to disable animations on your device. Firstly, enable developer options: Open the Settings app. Scroll to the bottom and select About phone. Scroll to the bottom and tap Build number … Read more

Testing ViewPager with Espresso. How perfom action to a button of an Item?

FirstVal, ViewPager is not an AdapterView, it directly extends from ViewGroup. So the method onData() cannot be used on a ViewPager. Solution 1 As it’s a ViewGroup, each items are direct children of its ViewPager. So the process is to reference the first view child using a custom matcher (like this onefirstChildOf()) and playing with … Read more

Selecting child view at index using Espresso

public static Matcher<View> nthChildOf(final Matcher<View> parentMatcher, final int childPosition) { return new TypeSafeMatcher<View>() { @Override public void describeTo(Description description) { description.appendText(“with “+childPosition+” child view of type parentMatcher”); } @Override public boolean matchesSafely(View view) { if (!(view.getParent() instanceof ViewGroup)) { return parentMatcher.matches(view.getParent()); } ViewGroup group = (ViewGroup) view.getParent(); return parentMatcher.matches(view.getParent()) && group.getChildAt(childPosition).equals(view); } }; } To … Read more