如何从Android Oreo中禁用espresso测试的新自动填充功能

Dan*_*ico 19 testing android android-testing android-espresso android-autofill-manager

在Android设备上运行测试sdk 26会导致它们失败,因为新的自动填充功能会在espresso尝试点击它们时隐藏字段.

我在firebase测试实验室运行我的测试,所以我不能在我的测试设备上手动禁用它们.

一些图片:

1.单击用户名字段前,密码可见.

在此输入图像描述

2.单击用户名字段后,此自动填充对话框将隐藏密码字段:

在此输入图像描述

3.登录后显示另一个"填充"对话框:

在此输入图像描述

Espresso无法点击现在的密码字段,因为自动填充对话框隐藏了我的字段和fail.

AutofillManager#disableAutofillServices()仅使用禁用#2.对话但#3.还在那里.

如何在测试设备上禁用自动填充?

UCZ*_*UCZ 10

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

这应该禁用自动填充服务。与在系统设置中手动关闭自动填充服务相同。它至少适用于模拟器。但这需要root访问权限。

另一种禁用自动填充服务的方法是更改autofill_service设置。

adb shell settings put secure autofill_service null

  • 最后一个 `adb shell settings put secure autofill_service null` 起作用了!谢啦 (2认同)

azi*_*ian 7

根据文档,您可以使用AutofillManager#disableAutofillServices()API 禁用自动填充服务:

如果调用此API的应用启用了自动填充服务,则会禁用它们.

用法:


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

您可以在@Before测试步骤中执行此操作.


Lui*_*dez 5

基于@Alan K 解决方案的替代代码组织。

创建类 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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

而且,在您的代码中,当您需要为 editTextPassword 禁用自动填充时...

editTextPassword.perform(..., ViewActions.closeSoftKeyboard(), DisableAutofillAction())
Run Code Online (Sandbox Code Playgroud)