【问题标题】:Grant ACCESS_MOCK_LOCATION permission for testing using adb fails in Marshmallow授予 ACCESS_MOCK_LOCATION 权限以在棉花糖中使用 adb 进行测试失败
【发布时间】:2017-04-05 15:45:25
【问题描述】:

这是问题的根源:

  1. 在调试清单<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/> 中添加模拟位置权限
  2. 在手机中安装调试版本的应用程序(在 Gradle 中:debug { debuggable true })
  3. 尝试使用 adb 授予权限:adb shell pm grant com.myapp.name android.permission.ACCESS_MOCK_LOCATION

命令的结果是:

不允许操作:java.lang.SecurityException: Permission android.permission.ACCESS_MOCK_LOCATION is not a changeable permission type"

如果我在手机上打开开发者选项并设置Setting >> Developer Option >> Select Mock location app,它就可以工作。

我需要这个来进行自动化测试,所以很明显进入手机设置的选项是无效的,因为它在每次安装应用程序时都会重置,所以我需要 adb 选项才能工作。

【问题讨论】:

    标签: android permissions automated-tests


    【解决方案1】:

    我在 Calabash 中找到了解决同样问题的解决方案: https://github.com/calabash/calabash-android/commit/b31be97953325383b9295ff52234a0121cc27e27

    adb shell appops set com.myapp.name 58 allow
    

    要从 gradle 自动执行此操作,您可以将命令添加到安装任务:

    def adb = android.getAdbExe().toString()
    tasks.whenTaskAdded { task ->
        if (task.name.startsWith('install')) {
            task.doLast {
                android.applicationVariants.all { variant ->
                    "${adb} devices".execute().text.eachLine {
                        if (it.endsWith("device")) {
                            def device = it.split()[0]
                            println "Granting test permissions on device ${device}\n"
                            "${adb} shell appops set ${variant.applicationId} 58 allow".execute()
                        }
                    }
                }
            }
        }
    }
    

    但你必须在 connectedTest 任务之前显式调用 install 任务,例如:

    gradlew installMyAppDebug connectedMyAppDebugAndroidTest
    

    【讨论】:

      【解决方案2】:

      或者,您可以使用测试管家插件来管理测试权限https://github.com/linkedin/test-butler

      我配置和使用测试管家的示例项目:https://github.com/sebaslogen/Blendletje/

      public class TestRunner extends AndroidJUnitRunner {
          @Override
          public void onStart() {
              TestButler.setup(InstrumentationRegistry.getTargetContext());
              super.onStart();
          }
      
          @Override
          public void finish(final int resultCode, final Bundle results) {
              TestButler.teardown(InstrumentationRegistry.getTargetContext());
              super.finish(resultCode, results);
          }
      }
      
      @Before
      public void setUp() throws Exception {
          final Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
          TestButler.verifyAnimationsDisabled(targetContext);
      }
      

      您可以在运行测试时看到它是如何自动安装的:https://github.com/sebaslogen/Blendletje/blob/master/app/build.gradle#L107

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        • 1970-01-01
        • 2016-02-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多