28

Running tests on android devices with sdk 26 causes them to fail because of the new Autofill feature that hides the fields when espresso are trying to click them.

Im running my tests on firebase test lab so I cannot disable them manually on my tests devices.

Some images:

1. Password is visible before clicking username field.

enter image description here

2. After clicking username field password field is hidden by this Autofill dialog:

enter image description here

3. After login it shows another Fill dialog:

enter image description here

Espresso cant click now password field since the autofill dialog is hiding my field and fail.

Using AutofillManager#disableAutofillServices() only disabled the #2. dialog but #3. is still there.

How to disable Autofill on test devices?

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162

8 Answers8

14

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.

azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • 1
    It removed the "Tap to let Google" dialog but it still shows "Save password to AUTOFILL with GOOGLE" dialog. – Daniel Gomez Rico Oct 24 '17 at 16:54
  • 1
    I do not have O device and I cannot find "Autofill with Google" app inside emulator. I think you can try to disable that application through adb shell command ([how?](https://stackoverflow.com/a/35157119/1083957)) when performing instrumented test: `adb shell pm disable package.name.of.autofill.with.google`. – azizbekian Oct 25 '17 at 03:07
  • you can find it in the `Settings` for an Android O emulator. – Daniel Gomez Rico Oct 25 '17 at 14:27
12

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

UCZ
  • 136
  • 1
  • 6
  • 3
    The last one `adb shell settings put secure autofill_service null` worked! thanks man – Daniel Gomez Rico Jan 29 '18 at 19:43
  • Can you add code to enable it again :) after "... autofill_service null" – Anton Shkurenko Sep 07 '18 at 10:16
  • I enabled it changing "null" for "com.google.android.gms/com.google.android.gms.autofill.service.AutofillService", thanks :) – Anton Shkurenko Sep 07 '18 at 11:02
  • As of 5/25/2023, this no longer works: sudo adb shell settings put secure autofill_service null Exception occurred while executing 'put': java.lang.SecurityException: Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS – FractalBob May 25 '23 at 14:38
7

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())
4

The following snippet can be used to ignore the new android suggestions:

getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
MaartenDev
  • 5,631
  • 5
  • 21
  • 33
DoronK
  • 4,837
  • 2
  • 32
  • 37
3

I've had luck disabling autofill during Espresso tests with custom ViewActions applied whenever text is entered.

            .onView(...)
            .perform(
                    new ViewAction() {
                        @Override
                        public Matcher<View> getConstraints() {
                            return Matchers.any(View.class);
                        }

                        @Override
                        public String getDescription() {
                            return "Marking view not important for autofill";
                        }

                        @Override
                        public void perform(UiController uiController, View view) {
                            // Required to disable autofill suggestions during tests on API 26+
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                view.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
                            }
                        }
                    })
            .perform(click())
            .perform(clearText())
            .perform(typeText(textToType))
            .perform(
                    new 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) {
                            // Required to dismiss the autofill picker during tests on API 26+
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                AutofillManager autofillManager =
                                        view.getContext()
                                                .getSystemService(AutofillManager.class);
                                if (autofillManager != null) autofillManager.cancel();
                            }
                        }
                    });
Alan K.
  • 217
  • 1
  • 7
-1

Disable autofill when testing. Define a TestRunner class in gradle

defaultConfig {
    testInstrumentationRunner "com.cover.android.TestRunner"
}

then

public class TestRunner extends AndroidJUnitRunner {

  @Override
  public void onCreate(Bundle arguments) {
      super.onCreate(arguments);
      CustomEditText.TESTING = TRUE;
  }
}

then use a custom version of EditText

public class CustomEditText extends AppCompatEditText {
public static boolean TESTING = false;
public CustomEditText(Context context) {
    super(context);
}

@Override
public int getAutofillType() {
    return TESTING? AUTOFILL_TYPE_NONE : super.getAutofillType();
}
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
sbaar
  • 1,429
  • 1
  • 17
  • 33
-1

According To Documentation : when the view is focused on and is part of a dataset. The application can be notified when the affordance is shown by registering an AutofillManager.AutofillCallback through registerCallback(AutofillCallback). When the user selects a dataset from the affordance, all views present in the dataset are autofilled, through calls to autofill(AutofillValue) or autofill(SparseArray).

The context is then finished when one of the following occurs:

  1. commit() is called or all savable views are gone.
  2. cancel() is called.

It is safe to call into its methods from any thread.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument AutofillManager.class.

Use : disableAutofillServices() method to disible the service .

-1

Within your device, go to this route Settings > System > Languages & input > Advanced > AutoFill Service and cancel it

Saul_programa
  • 341
  • 1
  • 4
  • 13
  • 1
    This works if you have access to your device but not for a test device farm and something more automated, please read the question text. – Daniel Gomez Rico Nov 09 '20 at 14:33