How to disable the new Autofill feature from Android Oreo for espresso tests How to disable the new Autofill feature from Android Oreo for espresso tests android android

How to disable the new Autofill feature from Android Oreo for espresso tests


Based on documentation, you can disable Autofill services using AutofillManager#disableAutofillServices() API:

If the app calling this API has enabled autofill services they will be disabled.

Usage:

    val autofillManager: AutofillManager = context.getSystemService(AutofillManager::class.java)    autofillManager.disableAutofillServices()

You can do this in @Before step of your test.


adb shell pm disable com.google.android.gms/com.google.android.gms.autofill.service.AutofillService

This should disable the autofill service. It is same as turning off autofill service in the system settings manually. It at least worked on the emulator. But this needs root access.

Another way to disable the autofill service is to change the autofill_service settings.

adb shell settings put secure autofill_service null


An alternative code organisation based on @Alan K's solution.

Create the class DisableAutofillAction:

public class DisableAutofillAction implements ViewAction {    @Override    public Matcher<View> getConstraints() {        return Matchers.any(View.class);    }    @Override    public String getDescription() {        return "Dismissing autofill picker";    }    @Override    public void perform(UiController uiController, View view) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {            AutofillManager autofillManager = view.getContext().getSystemService(AutofillManager.class);            if (autofillManager != null) {                autofillManager.cancel();            }        }    }}

And, in your code when you need to disable AutoFill for editTextPassword...

editTextPassword.perform(..., ViewActions.closeSoftKeyboard(), DisableAutofillAction())