Android Marshmallow: Test permissions with Espresso?

With the new release of the Android Testing Support Library 1.0, there’s a GrantPermissionRule that you can use in your tests to grant a permission before starting any tests. @Rule public GrantPermissionRule permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION); Kotlin solution @get:Rule var permissionRule = GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION) @get:Rule must be used in order to avoid java.lang.Exception: The @Rule ‘permissionRule’ must … Read more

In Espresso, how to avoid AmbiguousViewMatcherException when multiple views match

EDIT: Someone mentioned in the comments that withParentIndex is now available, give that a try first before using the custom solution below. I was amazed that I couldn’t find a solution by simply providing an index along with a matcher (i.e. withText, withId). The accepted answer only solves the problem when you’re dealing with onData … Read more

Conflict with dependency ‘com.android.support:support-annotations’. Resolved versions for app (23.1.0) and test app (23.0.1) differ

You can force the annotation library in your test using: androidTestCompile ‘com.android.support:support-annotations:23.1.0’ Something like this: // Force usage of support annotations in the test app, since it is internally used by the runner module. androidTestCompile ‘com.android.support:support-annotations:23.1.0’ androidTestCompile ‘com.android.support.test:runner:0.4.1’ androidTestCompile ‘com.android.support.test:rules:0.4.1’ androidTestCompile ‘com.android.support.test.espresso:espresso-core:2.2.1’ androidTestCompile ‘com.android.support.test.espresso:espresso-intents:2.2.1’ androidTestCompile ‘com.android.support.test.espresso:espresso-web:2.2.1’ Another solution is to use this in the top … Read more

Espresso – How can I check if an activity is launched after performing a certain action?

You can use: intended(hasComponent(YourExpectedActivity.class.getName())); Requires this gradle entry: androidTestCompile (“com.android.support.test.espresso:espresso-intents:$espressoVersion”) The import for the intended() and hasComponent() import static android.support.test.espresso.intent.Intents.intended; import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent; as mentioned by Shubam Gupta please remember to call Intents.init() before calling intended(). You can eventually call it in the @Before method.

Espresso: Thread.sleep( )

On my mind correct approach will be: /** Perform action of waiting for a specific view id. */ public static ViewAction waitId(final int viewId, final long millis) { return new ViewAction() { @Override public Matcher<View> getConstraints() { return isRoot(); } @Override public String getDescription() { return “wait for a specific view with id <” + … Read more

Checking toast message in android espresso

This slightly long statement works for me: import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.RootMatchers.withDecorView; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; …. onView(withText(R.string.TOAST_STRING)).inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView())))).check(matches(isDisplayed()));